android - Get NullPointerException from method call, but arguments are not null -
my apologies in advance length of post, inclusion of 2 logcat outputs necessary explain problem.
i getting nullpointerexception when call 1 of interface methods of listener custom viewgroup. exception occurs @ line 264 in code below.
260 valuetab valuetab = mtabhost.getcurrentlyselectedvaluetab(); 261 if (this != null && valuetab != null) { 262 log.i("tabbedslider", this.tostring()); 263 log.i("valuetab", valuetab.tostring()); 264 montabbedsliderchangelistener.onslidervaluechanged(this, valuetab); 265 }
i have put method call inside if statement ensure neither of arguments null. have included couple of log statements clarify state of arguments when call made.
this
... instance of custom viewgroup calledtabbedslider
valuetab
... instance of custom child view of tabbedslider of type calledvaluetab
displays title , float value.
the sequence below explains when method call occurs:
- an instance of tabbedslider instantiated.
- its various properties set up.
- it added mainactivity.
- an instance of valuetab instantiated.
- its various properties set including assigning value.
- it added instance of tabbedslider.
- the offending method call made.
i have set break point on problem method call , run debug. when inspect this
, valuetab
, setup expect.
since method call made after adding valuetab tabbedslider, have not yet been measured, laid out , drawn screen (hence 0 values bounds when log tostring()
), these objects not null.
this logcat output:
10-07 10:28:17.952: d/dalvikvm(13357): gc_for_alloc freed 341k, 6% free 7644k/8048k, paused 36ms, total 37ms 10-07 10:28:17.972: i/tabbedslider(13357): com.soundconception.tabbedslider.tabbedslider{42075068 v.e..... ......i. 0,0-0,0 #7f040002 app:id/testslidera} 10-07 10:28:17.972: i/valuetab(13357): com.soundconception.tabbedslider.valuetab{420d8908 vfed..c. ..s...i. 0,0-0,0} 10-07 10:28:17.972: d/androidruntime(13357): shutting down vm 10-07 10:28:17.972: w/dalvikvm(13357): threadid=1: thread exiting uncaught exception (group=0x41911700) 10-07 10:28:17.972: e/androidruntime(13357): fatal exception: main 10-07 10:28:17.972: e/androidruntime(13357): java.lang.runtimeexception: unable start activity componentinfo{com.soundconception.tabbedslider/com.soundconception.tabbedslider.mainactivity}: java.lang.nullpointerexception 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activitythread.access$600(activitythread.java:141) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 10-07 10:28:17.972: e/androidruntime(13357): @ android.os.handler.dispatchmessage(handler.java:99) 10-07 10:28:17.972: e/androidruntime(13357): @ android.os.looper.loop(looper.java:137) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activitythread.main(activitythread.java:5103) 10-07 10:28:17.972: e/androidruntime(13357): @ java.lang.reflect.method.invokenative(native method) 10-07 10:28:17.972: e/androidruntime(13357): @ java.lang.reflect.method.invoke(method.java:525) 10-07 10:28:17.972: e/androidruntime(13357): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 10-07 10:28:17.972: e/androidruntime(13357): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 10-07 10:28:17.972: e/androidruntime(13357): @ dalvik.system.nativestart.main(native method) 10-07 10:28:17.972: e/androidruntime(13357): caused by: java.lang.nullpointerexception 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabbedslider.configuresliderforvaluetab(tabbedslider.java:264) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabbedslider.access$1(tabbedslider.java:256) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabbedslider$2.onvaluetabselectionchange(tabbedslider.java:164) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabhost.selectvaluetab(tabhost.java:88) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabhost.valuetabsetup(tabhost.java:72) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabhost.addvaluetab(tabhost.java:51) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.tabbedslider.addvaluetab(tabbedslider.java:249) 10-07 10:28:17.972: e/androidruntime(13357): @ com.soundconception.tabbedslider.mainactivity.oncreate(mainactivity.java:18) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activity.performcreate(activity.java:5133) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 10-07 10:28:17.972: e/androidruntime(13357): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 10-07 10:28:17.972: e/androidruntime(13357): ... 11 more
i can work around issue putting method call in try/catch block shown below:
valuetab valuetab = mtabhost.getcurrentlyselectedvaluetab(); if (this != null && valuetab != null) { log.i("tabbedslider", this.tostring()); log.i("valuetab", valuetab.tostring()); try { montabbedsliderchangelistener.onslidervaluechanged(this, valuetab); } catch (nullpointerexception e) { log.i("tabbedslider", "caught nullpointerexception"); } }
when run this, nullpointerexception caught first time method called each of 3 tabbedslider instances, subsequent calls after first call work fine without raising exception, can seen in following new logcat output.
10-07 11:36:53.822: d/dalvikvm(14479): gc_for_alloc freed 347k, 6% free 7643k/8052k, paused 36ms, total 38ms 10-07 11:36:53.852: i/tabbedslider(14479): com.soundconception.tabbedslider.tabbedslider{42076c68 v.e..... ......i. 0,0-0,0 #7f040002 app:id/testslidera} 10-07 11:36:53.852: i/valuetab(14479): com.soundconception.tabbedslider.valuetab{420da508 vfed..c. ..s...i. 0,0-0,0} 10-07 11:36:53.852: i/tabbedslider(14479): caught nullpointerexception 10-07 11:36:53.862: i/tabbedslider(14479): com.soundconception.tabbedslider.tabbedslider{420b7768 v.e..... ......i. 0,0-0,0 #7f040003 app:id/testsliderb} 10-07 11:36:53.862: i/valuetab(14479): com.soundconception.tabbedslider.valuetab{4206e0c0 vfed..c. ..s...i. 0,0-0,0} 10-07 11:36:53.862: i/tabbedslider(14479): caught nullpointerexception 10-07 11:36:53.882: i/tabbedslider(14479): com.soundconception.tabbedslider.tabbedslider{420c8378 v.e..... ......i. 0,0-0,0 #7f040004 app:id/testsliderc} 10-07 11:36:53.882: i/valuetab(14479): com.soundconception.tabbedslider.valuetab{42077768 vfed..c. ..s...i. 0,0-0,0} 10-07 11:36:53.882: i/tabbedslider(14479): caught nullpointerexception 10-07 11:36:53.902: d/hardwarerenderer(14479): profiling hardware renderer 10-07 11:36:53.952: d/libegl(14479): loaded /system/lib/egl/libegl_tegra.so 10-07 11:36:53.972: d/libegl(14479): loaded /system/lib/egl/libglesv1_cm_tegra.so 10-07 11:36:53.982: d/libegl(14479): loaded /system/lib/egl/libglesv2_tegra.so 10-07 11:36:54.022: d/openglrenderer(14479): enabling debug mode 0 10-07 11:37:15.252: i/tabbedslider(14479): com.soundconception.tabbedslider.tabbedslider{420b7768 v.e..... ......id 21,127-779,233 #7f040003 app:id/testsliderb} 10-07 11:37:15.262: i/valuetab(14479): com.soundconception.tabbedslider.valuetab{420581e8 vfed..c. ..sp..id 93,0-186,54} 10-07 11:37:29.292: i/tabbedslider(14479): com.soundconception.tabbedslider.tabbedslider{420c8378 v.e..... ......id 21,233-779,339 #7f040004 app:id/testsliderc} 10-07 11:37:29.292: i/valuetab(14479): com.soundconception.tabbedslider.valuetab{42074f48 vfed..c. ..sp..id 186,0-279,54} 10-07 11:37:32.572: i/tabbedslider(14479): com.soundconception.tabbedslider.tabbedslider{42076c68 v.e..... ......id 21,21-779,127 #7f040002 app:id/testslidera} 10-07 11:37:32.572: i/valuetab(14479): com.soundconception.tabbedslider.valuetab{4209bcc8 vfed..c. ..sp..id 188,0-282,54}
it appear boils down views not being drawn yet @ time of first call, not understand why. view objects @ instant not null, why nullpointerexception raised? there hole in understanding.
while can work around problem try/catch block, not understanding why exception occurs in first place going drive me crazy. explanations appreciated, , code monkey keep sanity.
Comments
Post a Comment