Android 应用程序不断因空指针异常而崩溃

Posted

技术标签:

【中文标题】Android 应用程序不断因空指针异常而崩溃【英文标题】:Android Application Keeps Crashing from Null Pointer Exception 【发布时间】:2015-07-04 06:54:05 【问题描述】:

由于某种原因,我的应用程序由于空指针异常而不断崩溃。我正在为我的每个页面使用带有片段的导航抽屉。我还使用 JDBC 连接到我的 oracle 数据库。我遇到的问题是我的两个片段。当我运行程序时,无论我选择第一个片段还是第二个片段,它都可以正常工作。但是如果我再次选择同一个片段或尝试转到另一个片段,那么应用程序就会崩溃。

我能够查明问题所在。它在尝试调用时崩溃:

st = conn.createStatement();

第二次。

这是两个片段的代码:

排名片段:

package com.capstone.hammond.wallstreetfantasyleaguefinal;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


public class StandingsFragment extends Fragment 
    List<Integer> idList = new ArrayList<>();
    List<String> winList = new ArrayList<>();
    List<String> lossList = new ArrayList<>();
    List<String> firstName = new ArrayList<>();
    List<String> lastName = new ArrayList<>();


    int resultID;
    String resultWins;
    String resultLosses;
    String firstNameS;
    String lastNameS;

    Connection conn;
    ResultSet rs, rs1, rs2, rs3, rs4, rs5, rs6;
    Statement st, st1, st2, st3, st4, st5, st6;

    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        rootview = inflater.inflate(R.layout.fragment_standings, container, false);
        return rootview;

    

    public void onViewCreated(View view, Bundle savedInstanceState) 
        super.onViewCreated(view, savedInstanceState);

        try 
            List<List> resultList = new Standings().execute().get();
            firstName = resultList.get(0);
            lastName = resultList.get(1);
            winList = resultList.get(2);
            lossList = resultList.get(3);

            TextView player1;
            TextView player2;
            TextView player3;
            TextView player4;
            TextView player5;
            TextView player6;
            TextView player1Win;
            TextView player2Win;
            TextView player3Win;
            TextView player4Win;
            TextView player5Win;
            TextView player6Win;
            TextView player1Loss;
            TextView player2Loss;
            TextView player3Loss;
            TextView player4Loss;
            TextView player5Loss;
            TextView player6Loss;

            player1 = (TextView) view.findViewById(R.id.player1);
            player1.setText(firstName.get(0) + " " + lastName.get(0));
            player2 = (TextView) view.findViewById(R.id.player2);
            player2.setText(firstName.get(1) + " " + lastName.get(1));
            player3 = (TextView) view.findViewById(R.id.player3);
            player3.setText(firstName.get(2) + " " + lastName.get(2));
            player4 = (TextView) view.findViewById(R.id.player4);
            player4.setText(firstName.get(3) + " " + lastName.get(3));
            player5 = (TextView) view.findViewById(R.id.player5);
            player5.setText(firstName.get(4) + " " + lastName.get(4));
            player6 = (TextView) view.findViewById(R.id.player6);
            player6.setText(firstName.get(5) + " " + lastName.get(5));

            player1Win = (TextView) view.findViewById(R.id.player1_win);
            player1Win.setText(winList.get(0));
            player2Win = (TextView) view.findViewById(R.id.player2_win);
            player2Win.setText(winList.get(1));
            player3Win = (TextView) view.findViewById(R.id.player3_win);
            player3Win.setText(winList.get(2));
            player4Win = (TextView) view.findViewById(R.id.player4_win);
            player4Win.setText(winList.get(3));
            player5Win = (TextView) view.findViewById(R.id.player5_win);
            player5Win.setText(winList.get(4));
            player6Win = (TextView) view.findViewById(R.id.player6_win);
            player6Win.setText(winList.get(5));

            player1Loss = (TextView) view.findViewById(R.id.player1_loss);
            player1Loss.setText(lossList.get(0));
            player2Loss = (TextView) view.findViewById(R.id.player2_loss);
            player2Loss.setText(lossList.get(1));
            player3Loss = (TextView) view.findViewById(R.id.player3_loss);
            player3Loss.setText(lossList.get(2));
            player4Loss = (TextView) view.findViewById(R.id.player4_loss);
            player4Loss.setText(lossList.get(3));
            player5Loss = (TextView) view.findViewById(R.id.player5_loss);
            player5Loss.setText(lossList.get(4));
            player6Loss = (TextView) view.findViewById(R.id.player6_loss);
            player6Loss.setText(lossList.get(5));


         catch(InterruptedException e) 
            e.printStackTrace();
         catch(ExecutionException e) 
            e.printStackTrace();
        




    

    public class Standings extends AsyncTask<Void, Void, List<List>> 

        @Override
        protected List<List> doInBackground(Void... params) 
            try 
                conn = ConnectionManager.getConnection();
                rs = null;
                st = null;
                if (conn != null)
                    st = conn.createStatement();
                if (st != null)
                    rs = st.executeQuery("SELECT * FROM L1_STANDINGS");
                if (rs != null)
                    while(rs.next()) 
                        resultID = rs.getInt("USERID");
                        idList.add(resultID);
                        resultWins = rs.getString("WINS");
                        winList.add(resultWins);
                        resultLosses = rs.getString("LOSSES");
                        lossList.add(resultLosses);
                    

                st1 = null;
                if (conn != null)
                    st1 = conn.createStatement();
                rs1 = null;
                if (st1 != null)
                    rs1 = st1.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(0)));
                if (rs1 != null)
                    while(rs1.next()) 
                        firstNameS = rs1.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs1.getString("LASTNAME");
                        lastName.add(lastNameS);
                    
                st2 = null;
                if (conn != null)
                    st2 = conn.createStatement();
                rs2 = null;
                if (st2 != null)
                    rs2 = st2.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(1)));
                if (rs2 != null)
                    while(rs2.next()) 
                        firstNameS = rs2.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs2.getString("LASTNAME");
                        lastName.add(lastNameS);
                    
                st3 = null;
                if (conn != null)
                    st3 = conn.createStatement();
                rs3 = null;
                if (st3 != null)
                    rs3 = st3.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(2)));
                if (rs3 != null)
                    while(rs3.next()) 
                        firstNameS = rs3.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs3.getString("LASTNAME");
                        lastName.add(lastNameS);
                    
                st4 = null;
                if (conn != null)
                    st4 = conn.createStatement();
                rs4 = null;
                if (st4 != null)
                    rs4 = st4.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(3)));
                if (rs4 != null)
                    while(rs4.next()) 
                        firstNameS = rs4.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs4.getString("LASTNAME");
                        lastName.add(lastNameS);
                    
                st5 = null;
                if (conn != null)
                    st5 = conn.createStatement();
                rs5 = null;
                if (st5 != null)
                    rs5 = st5.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(4)));
                if (rs5 != null)
                    while(rs5.next()) 
                        firstNameS = rs5.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs5.getString("LASTNAME");
                        lastName.add(lastNameS);
                    
                st6 = null;
                if (conn != null)
                    st6 = conn.createStatement();
                rs6 = null;
                if (st6 != null)
                    rs6 = st6.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(5)));
                if (rs6 != null)
                    while(rs6.next()) 
                        firstNameS = rs6.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs6.getString("LASTNAME");
                        lastName.add(lastNameS);
                    

                List<List> resultList = new ArrayList<>();
                resultList.add(firstName);
                resultList.add(lastName);
                resultList.add(winList);
                resultList.add(lossList);

                return resultList;


             catch (SQLException e) 
                e.printStackTrace();
             finally 
                try
                    if(rs!=null)
                        rs.close();
                    if(rs1!=null)
                        rs1.close();
                    if(rs2!=null)
                        rs2.close();
                    if(rs3!=null)
                        rs3.close();
                    if(rs4!=null)
                        rs4.close();
                    if(rs5!=null)
                        rs5.close();
                    if(rs6!=null)
                        rs6.close();
                    if(st!=null)
                        st.close();
                    if(st1!=null)
                        st1.close();
                    if(st2!=null)
                        st2.close();
                    if(st3!=null)
                        st3.close();
                    if(st4!=null)
                        st4.close();
                    if(st5!=null)
                        st5.close();
                    if(st6!=null)
                        st6.close();
                    if(conn!=null)
                        conn.close();
                 catch(SQLException e) 
                    e.printStackTrace();
                

            
            return null;
        


    

每周匹配片段:

package com.capstone.hammond.wallstreetfantasyleaguefinal;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


public class WeeklyMatchFragment extends Fragment 
    Connection conn;
    ResultSet rs, rs1, rs2;
    Statement st, st1, st2;

    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        rootview = inflater.inflate(R.layout.fragment_weekly_match, container, false);
        return rootview;

    

    public void onViewCreated(View view, Bundle savedInstanceState) 
        super.onViewCreated(view, savedInstanceState);

        try 

            TextView user1;
            TextView user2;
            TextView bank1;
            TextView bank2;

            List<String> resultList = new MatchDetails().execute().get();

            user1 = (TextView) view.findViewById(R.id.player1);
            user1.setText(UserLoginInfo.fName + " " + UserLoginInfo.lName);
            bank1 = (TextView) view.findViewById(R.id.bank1);
            bank1.setText("Bank: $" + resultList.get(0));
            user2 = (TextView) view.findViewById(R.id.user2);
            user2.setText(resultList.get(1) + " " + resultList.get(2));
            bank2 = (TextView) view.findViewById(R.id.bank2);
            bank2.setText("Bank: $" + resultList.get(3));
         catch(InterruptedException e) 
            e.printStackTrace();
         catch(ExecutionException e) 
            e.printStackTrace();
        


    

    public class MatchDetails extends AsyncTask<Void, Void, List<String>> 

        @Override
        protected List<String> doInBackground(Void... params) 
            try 
                List<String> resultList = new ArrayList<>();
                conn = ConnectionManager.getConnection();
                rs = null;
                st = null;
                if (conn != null)
                    st = conn.createStatement();
                if (st != null)
                    rs = st.executeQuery("SELECT * FROM L1_STANDINGS WHERE EMAIL = '" + UserLoginInfo.userEmail + "'");
                if (rs != null)
                    while(rs.next()) 
                        resultList.add(rs.getString("BANK"));
                        UserLoginInfo.currOpp = rs.getInt("CURR_OPP");
                    
                st1 = null;
                if (conn != null)
                    st1 = conn.createStatement();
                rs1 = null;
                if (st1 != null)
                    rs1 = st1.executeQuery("SELECT * FROM USERINFO WHERE USERID = '" + UserLoginInfo.currOpp + "'");
                if (rs1 != null)
                    while(rs1.next()) 
                        resultList.add(rs1.getString("FIRSTNAME"));
                        resultList.add(rs1.getString("LASTNAME"));
                    
                rs2 = null;
                st2 = null;
                if (conn != null)
                    st2 = conn.createStatement();
                if (st2 != null)
                    rs2 = st.executeQuery("SELECT * FROM L1_Standings WHERE USERID = '" + UserLoginInfo.currOpp + "'");
                if (rs2 != null)
                    while(rs2.next()) 
                        resultList.add(rs2.getString("BANK"));
                    
                return resultList;
             catch (SQLException e) 
                e.printStackTrace();
             finally 
                try
                    if(rs!=null)
                        rs.close();
                    if(rs1!=null)
                        rs1.close();
                    if(st!=null)
                        st.close();
                    if(st1!=null)
                        st1.close();
                    if(conn!=null)
                        conn.close();
                 catch(SQLException e) 
                    e.printStackTrace();
                



            
            return null;
        


    

LogCat:

04-25 12:55:55.151: E/AndroidRuntime(2409): FATAL EXCEPTION: main
04-25 12:55:55.151: E/AndroidRuntime(2409): Process: com.capstone.hammond.wallstreetfantasyleaguefinal, PID: 2409
04-25 12:55:55.151: E/AndroidRuntime(2409): java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
04-25 12:55:55.151: E/AndroidRuntime(2409):     at com.capstone.hammond.wallstreetfantasyleaguefinal.StandingsFragment.onViewCreated(StandingsFragment.java:72)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.os.Handler.handleCallback(Handler.java:739)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.os.Looper.loop(Looper.java:135)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at android.app.ActivityThread.main(ActivityThread.java:5221)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at java.lang.reflect.Method.invoke(Native Method)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at java.lang.reflect.Method.invoke(Method.java:372)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-25 12:55:55.151: E/AndroidRuntime(2409):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

任何帮助将不胜感激!!!

【问题讨论】:

似乎问题出在 com.capstone.hammond.wallstreetfantasyleaguefinal.StandingsFragment.onViewCreated(StandingsFragment.java:72) 日志几乎总结了这个问题。 List&lt;List&gt; resultList = new Standings().execute().get(); 在这里,resultListnull,随后对空引用的任何调用都将导致 NullPointerException。如果您显示的异常是日志中唯一的异常,则表示您没有从与您的查询对应的数据库中获得任何结果。 我只是不明白为什么我第一次打开它时它会工作,然后如果我再次尝试打开它就会崩溃。 @MikeHammond 我向您保证,您自己可以更快地找到此问题的根本原因,因为您可以在最后重现它。 【参考方案1】:

StandingsFragment 中重新访问您的方法onViewCreated

检查resultList 或您的视图之一(例如player1)是否为空

【讨论】:

是的 resultList 在我第二次尝试时变为空。【参考方案2】:

我认为在onViewCreated 中使用AsyncTask 不是用于数据库操作的正确方法。此外,您正在使用get() 方法,onViewCreated 也在线程上运行。 因此,您应该只使用简单函数而不是 AsyncTask

已编辑 好的, 第一:我不知道我错了:D。 第二:我仍然不确定是否有必要在 Thread 中连接数据库。 第三:我在 Runnable 中做过 :( 这里是:

public class Items extends Fragment 

    private DBViewOperation mDBViewOp;
    private ArrayList<DashboardItem> items;
    private Runnable runnable;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   mDBViewOp = new DBViewOperation(getActivity());    

   /* It is not funny :/ */ 
   runnable= new Runnable() 
            @Override
            public void run() 
                loadItems();
            
   
   getActivity().runOnUiThread(runnable);



private void loadItems()
        items = mDBViewOp.GetItems(groupID);
        int size = items.size();

        int flashCount = 0;//....

不要使用conn.createStatement(); 检查这个:Android Sqlite getReadableDatabase

【讨论】:

您无法连接到主线程上的数据库。我相信您必须在 AsyncTask 中执行此操作。 不,我最近写了一个在主线程中连接的片段。如果你愿意,我可以与你分享。 那太好了。【参考方案3】:

问题是连接以某种方式关闭并且没有正确重新打开。通过保持连接打开(不调用 conn.close())一切正常。

【讨论】:

以上是关于Android 应用程序不断因空指针异常而崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android列表视图返回null?

ArtifactoryPublish 因空指针异常而失败

可滑动视图空异常错误 - Android Studio [重复]

Android-空指针异常反复崩溃

将 Geopoint 转换为 Location 时出现空指针异常

捕获空指针异常是代码异味吗?