简体   繁体   English

Android应用程序使空指针异常崩溃

[英]Android Application Keeps Crashing from Null Pointer Exception

For some reason my application keeps crashing because of a null pointer exception. 由于某种原因,由于空指针异常,我的应用程序不断崩溃。 I am using a navigation drawer with fragments for each of my pages. 我正在为每个页面使用带有片段的导航抽屉。 I am also using a JDBC to connect to my oracle database. 我还使用JDBC连接到我的oracle数据库。 The issue I have is with two of my fragments. 我的问题是我的两个片段。 When I run the program, whether I choose the first fragment or the second fragment, it works just fine. 运行程序时,无论选择第一个片段还是第二个片段,它都可以正常工作。 But if I choose the same fragment again or try to go to the other fragment, then the application crashes. 但是,如果我再次选择相同的片段或尝试转到另一个片段,则应用程序将崩溃。

I was able to pinpoint the problem. 我能够指出问题所在。 It crashes when it tries to call: 尝试调用时崩溃:

st = conn.createStatement();

the second time. 第二次。

Here is the code for the two fragments: 这是两个片段的代码:

Standings Fragment: 排名碎片:

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;
        }


    }
}

Weekly Match Fragment: 每周比赛片段:

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: 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)

Any help would be greatly appreciated!!! 任何帮助将不胜感激!!!

Revisit your method onViewCreated in StandingsFragment . StandingsFragment重新onViewCreated方法。

Check if resultList or one of your view (eg player1 ) is null 检查resultList或您的视图之一(例如player1 )是否为空

I think using AsyncTask is not the right way in onViewCreated for database operation. 我认为在onViewCreated使用AsyncTask并不是正确的数据库操作方法。 Moreover, you are using get() method, onViewCreated is also running on Thread. 此外,您正在使用get()方法, onViewCreated也在Thread上运行。

Therefore, you should use just simple function instead of AsyncTask . 因此,您应该只使用简单函数而不是AsyncTask

Editted 已编辑
Ok, 好,
First: I don't know I was wrong :D. 第一:我不知道我错了:D。
Second: I am still not sure it is necessity to connect database in Thread. 第二:我仍然不确定是否有必要在Thread中连接数据库。
Third: I had done in Runnable :( 第三:我在Runnable中做过:(
Here it is: 这里是:

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;//....
}

And don't use conn.createStatement(); 并且不要使用conn.createStatement();
Check this: 检查一下:
Android Sqlite getReadableDatabase Android Sqlite getReadableDatabase

The issue was that the connection was becoming closed somehow and not reopening properly. 问题在于连接正以某种方式关闭并且无法正确重新打开。 By leaving the connection open (not calling conn.close()) everything works properly. 通过使连接保持打开状态(不调用conn.close()),一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM