简体   繁体   中英

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

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 .

Check if resultList or one of your view (eg player1 ) is null

I think using AsyncTask is not the right way in onViewCreated for database operation. Moreover, you are using get() method, onViewCreated is also running on Thread.

Therefore, you should use just simple function instead of AsyncTask .

Editted
Ok,
First: I don't know I was wrong :D.
Second: I am still not sure it is necessity to connect database in Thread.
Third: I had done in 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();
Check this:
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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