java - Null Pointer Exception: Trying to make an android app for 3x3x3 tic tac toe -
i checked logcat , saw nullpointerexception error. beginner @ programming pls me out. last time had similar problem had not initialised array. cant figure out whats wrong time.
package com.example.test2; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.textview; public class mid extends activity { char[][][] a=new char[3][3][3]; char b=' '; int i=0,j=0,k=0,l=0,o=0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mid); for(j=0;j<3;j++) {for(k=0;k<3;k++) {for(k=0;k<3;k++) { a[j][k][l]=' '; } } } button btnt1 = (button)findviewbyid(r.id.btnt1); button btnt2 = (button)findviewbyid(r.id.btnt2); button btnt3 = (button)findviewbyid(r.id.btnt3); button btnt4 = (button)findviewbyid(r.id.btnt4); button btnt5 = (button)findviewbyid(r.id.btnt5); button btnt6 = (button)findviewbyid(r.id.btnt6); button btnt7 = (button)findviewbyid(r.id.btnt7); button btnt8 = (button)findviewbyid(r.id.btnt8); button btnt9 = (button)findviewbyid(r.id.btnt9); button btnm1 = (button)findviewbyid(r.id.btnm1); button btnm2 = (button)findviewbyid(r.id.btnm2); button btnm3 = (button)findviewbyid(r.id.btnm3); button btnm4 = (button)findviewbyid(r.id.btnm4); button btnm5 = (button)findviewbyid(r.id.btnm5); button btnm6 = (button)findviewbyid(r.id.btnm6); button btnm7 = (button)findviewbyid(r.id.btnm7); button btnm8 = (button)findviewbyid(r.id.btnm8); button btnm9 = (button)findviewbyid(r.id.btnm9); button btnb1 = (button)findviewbyid(r.id.btnb1); button btnb2 = (button)findviewbyid(r.id.btnb2); button btnb3 = (button)findviewbyid(r.id.btnb3); button btnb4 = (button)findviewbyid(r.id.btnb4); button btnb5 = (button)findviewbyid(r.id.btnb5); button btnb6 = (button)findviewbyid(r.id.btnb6); button btnb7 = (button)findviewbyid(r.id.btnb7); button btnb8 = (button)findviewbyid(r.id.btnb8); button btnb9 = (button)findviewbyid(r.id.btnb9); button top = (button)findviewbyid(r.id.top); button mid = (button)findviewbyid(r.id.mid); button bot = (button)findviewbyid(r.id.bot); top.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { setcontentview(r.layout.top); }}); mid.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { setcontentview(r.layout.mid); }}); bot.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { setcontentview(r.layout.bot); }}); btnt1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if(i%2==1) {b='x';} else {b='o';} i++; textview opt1 = (textview)findviewbyid(r.id.btnt1); opt1.settext(string.valueof(b));a[0][0][0]=b;j=0;k=0; check(a,j,k,l,o); }}); btnt2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt2 = (textview)findviewbyid(r.id.btnt2); opt2.settext(string.valueof(b));a[0][0][1]=b;j=0;k=1; check(a,j,k,l,o); }}); btnt3.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt3 = (textview)findviewbyid(r.id.btnt3); opt3.settext(string.valueof(b));a[0][0][2]=b;j=0;k=2; check(a,j,k,l,o); }}); btnt4.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt4 = (textview)findviewbyid(r.id.btnt4); opt4.settext(string.valueof(b));a[0][1][0]=b;j=1;k=0; check(a,j,k,l,o); }}); btnt5.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt5 = (textview)findviewbyid(r.id.btnt5); opt5.settext(string.valueof(b));a[0][1][1]=b;j=1;k=1; check(a,j,k,l,o); }}); btnt6.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt6 = (textview)findviewbyid(r.id.btnt6); opt6.settext(string.valueof(b));a[0][1][2]=b;j=1;k=2; check(a,j,k,l,o); }}); btnt7.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt7 = (textview)findviewbyid(r.id.btnt7); opt7.settext(string.valueof(b));a[0][2][0]=b;j=2;k=0; check(a,j,k,l,o); }}); btnt8.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt8 = (textview)findviewbyid(r.id.btnt8); opt8.settext(string.valueof(b));a[0][2][1]=b;j=2;k=1; check(a,j,k,l,o); }}); btnt9.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opt9 = (textview)findviewbyid(r.id.btnt9); opt9.settext(string.valueof(b));a[0][2][2]=b;j=2;k=2; check(a,j,k,l,o); }}); btnm1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if(i%2==1) {b='x';} else {b='o';} i++; textview opm1 = (textview)findviewbyid(r.id.btnm1); opm1.settext(string.valueof(b));a[1][0][0]=b;j=0;k=0; check(a,j,k,l,o); }}); btnm2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm2 = (textview)findviewbyid(r.id.btnm2); opm2.settext(string.valueof(b));a[1][0][1]=b;j=0;k=1; check(a,j,k,l,o); }}); btnm3.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm3 = (textview)findviewbyid(r.id.btnm3); opm3.settext(string.valueof(b));a[1][0][2]=b;j=0;k=2; check(a,j,k,l,o); }}); btnm4.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm4 = (textview)findviewbyid(r.id.btnm4); opm4.settext(string.valueof(b));a[1][1][0]=b;j=1;k=0; check(a,j,k,l,o); }}); btnm5.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm5 = (textview)findviewbyid(r.id.btnm5); opm5.settext(string.valueof(b));a[1][1][1]=b;j=1;k=1; check(a,j,k,l,o); }}); btnm6.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm6 = (textview)findviewbyid(r.id.btnm6); opm6.settext(string.valueof(b));a[1][1][2]=b;j=1;k=2; check(a,j,k,l,o); }}); btnm7.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm7 = (textview)findviewbyid(r.id.btnm7); opm7.settext(string.valueof(b));a[1][2][0]=b;j=2;k=0; check(a,j,k,l,o); }}); btnm8.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm8 = (textview)findviewbyid(r.id.btnm8); opm8.settext(string.valueof(b));a[1][2][1]=b;j=2;k=1; check(a,j,k,l,o); }}); btnm9.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opm9 = (textview)findviewbyid(r.id.btnm9); opm9.settext(string.valueof(b));a[1][2][2]=b;j=2;k=2; check(a,j,k,l,o); }}); btnb1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if(i%2==1) {b='x';} else {b='o';} i++; textview opb1 = (textview)findviewbyid(r.id.btnb1); opb1.settext(string.valueof(b));a[2][0][0]=b;j=0;k=0; check(a,j,k,l,o); }}); btnb2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb2 = (textview)findviewbyid(r.id.btnb2); opb2.settext(string.valueof(b));a[2][0][1]=b;j=0;k=1; check(a,j,k,l,o); }}); btnb3.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb3 = (textview)findviewbyid(r.id.btnb3); opb3.settext(string.valueof(b));a[2][0][2]=b;j=0;k=2; check(a,j,k,l,o); }}); btnb4.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb4 = (textview)findviewbyid(r.id.btnb4); opb4.settext(string.valueof(b));a[2][1][0]=b;j=1;k=0; check(a,j,k,l,o); }}); btnb5.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb5 = (textview)findviewbyid(r.id.btnb5); opb5.settext(string.valueof(b));a[2][1][1]=b;j=1;k=1; check(a,j,k,l,o); }}); btnb6.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb6 = (textview)findviewbyid(r.id.btnb6); opb6.settext(string.valueof(b));a[2][1][2]=b;j=1;k=2; check(a,j,k,l,o); }}); btnb7.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb7 = (textview)findviewbyid(r.id.btnb7); opb7.settext(string.valueof(b));a[2][2][0]=b;j=2;k=0; check(a,j,k,l,o); }}); btnb8.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb8 = (textview)findviewbyid(r.id.btnb8); opb8.settext(string.valueof(b));a[2][2][1]=b;j=2;k=1; check(a,j,k,l,o); }}); btnb9.setonclicklistener(new view.onclicklistener() { public void onclick(view v) {if(i%2==1) {b='x';} else {b='o';} i++; textview opb9 = (textview)findviewbyid(r.id.btnb9); opb9.settext(string.valueof(b));a[2][2][2]=b;j=2;k=2; check(a,j,k,l,o); }}); } public void check(char[][][] a,int j,int k,int l,int o) { textview txt = (textview)findviewbyid(r.id.txt); txt.settext(string.valueof(j)+string.valueof(k)); if(a[i][j][0]==a[i][j][1]&&a[i][j][1]==a[i][j][2]&&a[i][j][0]!=' ') o+=1; if(a[i][0][k]==a[i][1][k]&&a[i][1][k]==a[i][2][k]&&a[i][0][k]!=' ') o+=1; if(a[0][j][k]==a[1][j][k]&&a[1][j][k]==a[2][j][k]&&a[0][j][k]!=' ') o+=1; if(((a[i][0][0]==a[i][1][1]&&a[i][1][1]==a[i][2][2])||(a[i][0][2]==a[i][1][1]&&a[i][1][1]==a[i][2][0]))&&a[i][1][1]!=' ') o+=1; if(((a[0][j][0]==a[1][j][1]&&a[1][j][1]==a[2][j][2])||(a[0][j][2]==a[1][j][1]&&a[1][j][1]==a[2][j][0]))&&a[1][j][1]!=' ') o+=1; if(((a[0][j][0]==a[1][j][1]&&a[1][j][1]==a[2][j][2])||(a[0][j][2]==a[1][j][1]&&a[1][j][1]==a[2][j][0]))&&a[1][j][1]!=' ') o+=1; if(((a[0][0][0]==a[1][1][1]&&a[0][0][0]==a[2][2][2])||(a[0][0][2]==a[1][1][1]&&a[0][0][0]==a[2][2][0])||(a[0][2][0]==a[1][1][1]&&a[0][0][0]==a[2][0][2])||(a[2][0][0]==a[1][1][1]&&a[0][0][0]==a[0][2][2]))&&a[1][1][1]!=' ') o+=1; if(o!=0) {txt.settext("gg");} } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.mid, menu); return true; } }
here logcat:
03-21 16:01:06.955: d/dalvikvm(4465): late-enabling checkjni 03-21 16:01:07.055: d/androidruntime(4465): shutting down vm 03-21 16:01:07.055: w/dalvikvm(4465): threadid=1: thread exiting uncaught exception (group=0x415a1ba8) 03-21 16:01:07.065: e/androidruntime(4465): fatal exception: main 03-21 16:01:07.065: e/androidruntime(4465): process: com.example.test2, pid: 4465 03-21 16:01:07.065: e/androidruntime(4465): java.lang.runtimeexception: unable start activity componentinfo{com.example.test2/com.example.test2.mid}: java.lang.nullpointerexception 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activitythread.access$800(activitythread.java:135) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-21 16:01:07.065: e/androidruntime(4465): @ android.os.handler.dispatchmessage(handler.java:102) 03-21 16:01:07.065: e/androidruntime(4465): @ android.os.looper.loop(looper.java:136) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activitythread.main(activitythread.java:5017) 03-21 16:01:07.065: e/androidruntime(4465): @ java.lang.reflect.method.invokenative(native method) 03-21 16:01:07.065: e/androidruntime(4465): @ java.lang.reflect.method.invoke(method.java:515) 03-21 16:01:07.065: e/androidruntime(4465): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-21 16:01:07.065: e/androidruntime(4465): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-21 16:01:07.065: e/androidruntime(4465): @ dalvik.system.nativestart.main(native method) 03-21 16:01:07.065: e/androidruntime(4465): caused by: java.lang.nullpointerexception 03-21 16:01:07.065: e/androidruntime(4465): @ com.example.test2.mid.oncreate(mid.java:78) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activity.performcreate(activity.java:5231) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-21 16:01:07.065: e/androidruntime(4465): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 03-21 16:01:07.065: e/androidruntime(4465): ... 11 more 03-21 16:04:05.315: d/androidruntime(4667): shutting down vm 03-21 16:04:05.315: w/dalvikvm(4667): threadid=1: thread exiting uncaught exception (group=0x415a1ba8) 03-21 16:04:05.315: e/androidruntime(4667): fatal exception: main 03-21 16:04:05.315: e/androidruntime(4667): process: com.example.test2, pid: 4667 03-21 16:04:05.315: e/androidruntime(4667): java.lang.runtimeexception: unable start activity componentinfo{com.example.test2/com.example.test2.mid}: java.lang.nullpointerexception 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activitythread.access$800(activitythread.java:135) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-21 16:04:05.315: e/androidruntime(4667): @ android.os.handler.dispatchmessage(handler.java:102) 03-21 16:04:05.315: e/androidruntime(4667): @ android.os.looper.loop(looper.java:136) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activitythread.main(activitythread.java:5017) 03-21 16:04:05.315: e/androidruntime(4667): @ java.lang.reflect.method.invokenative(native method) 03-21 16:04:05.315: e/androidruntime(4667): @ java.lang.reflect.method.invoke(method.java:515) 03-21 16:04:05.315: e/androidruntime(4667): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-21 16:04:05.315: e/androidruntime(4667): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-21 16:04:05.315: e/androidruntime(4667): @ dalvik.system.nativestart.main(native method) 03-21 16:04:05.315: e/androidruntime(4667): caused by: java.lang.nullpointerexception 03-21 16:04:05.315: e/androidruntime(4667): @ com.example.test2.mid.oncreate(mid.java:78) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activity.performcreate(activity.java:5231) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-21 16:04:05.315: e/androidruntime(4667): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 03-21 16:04:05.315: e/androidruntime(4667): ... 11 more
textview opt1 = (textview)findviewbyid(r.id.btnt1);
casting button textview? delete it, do:
btn1.settext("your text");
Comments
Post a Comment