java - Android Application Keeps Crashing from Null Pointer Exception -
for reason application keeps crashing because of null pointer exception. using navigation drawer fragments each of pages. using jdbc connect oracle database. issue have 2 of fragments. when run program, whether choose first fragment or second fragment, works fine. if choose same fragment again or try go other fragment, application crashes.
i able pinpoint problem. crashes when tries call:
st = conn.createstatement();
the second time.
here code 2 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 * 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 * userinfo 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 * userinfo 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 * userinfo 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 * userinfo 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 * userinfo 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 * userinfo 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(); } { 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 * l1_standings 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 * userinfo 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 * l1_standings userid = '" + userlogininfo.curropp + "'"); if (rs2 != null) while(rs2.next()) { resultlist.add(rs2.getstring("bank")); } return resultlist; } catch (sqlexception e) { e.printstacktrace(); } { 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 invoke interface method 'java.lang.object java.util.list.get(int)' on null object reference 04-25 12:55:55.151: e/androidruntime(2409): @ com.capstone.hammond.wallstreetfantasyleaguefinal.standingsfragment.onviewcreated(standingsfragment.java:72) 04-25 12:55:55.151: e/androidruntime(2409): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:971) 04-25 12:55:55.151: e/androidruntime(2409): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1136) 04-25 12:55:55.151: e/androidruntime(2409): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739) 04-25 12:55:55.151: e/androidruntime(2409): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1499) 04-25 12:55:55.151: e/androidruntime(2409): @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:456) 04-25 12:55:55.151: e/androidruntime(2409): @ android.os.handler.handlecallback(handler.java:739) 04-25 12:55:55.151: e/androidruntime(2409): @ android.os.handler.dispatchmessage(handler.java:95) 04-25 12:55:55.151: e/androidruntime(2409): @ android.os.looper.loop(looper.java:135) 04-25 12:55:55.151: e/androidruntime(2409): @ android.app.activitythread.main(activitythread.java:5221) 04-25 12:55:55.151: e/androidruntime(2409): @ java.lang.reflect.method.invoke(native method) 04-25 12:55:55.151: e/androidruntime(2409): @ java.lang.reflect.method.invoke(method.java:372) 04-25 12:55:55.151: e/androidruntime(2409): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 04-25 12:55:55.151: e/androidruntime(2409): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
any appreciated!!!
revisit method onviewcreated
in standingsfragment
.
check if resultlist
or 1 of view (e.g. player1
) null
Comments
Post a Comment