Java – NullPointerException and nothing is null

NullPointerException and nothing is null… here is a solution to the problem.

NullPointerException and nothing is null

Well, it’s frustrating.

My code is as follows:

accept.setBackgroundResource(R.drawable.circle_navigator_accept_checked);
ViewPager vp = (ViewPager)activity.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = (ViewPagerAdapter)vp.getAdapter();

MyAdapter ma = (MyAdapter)(adapter.finalExamTypeList.getAdapter());

int selected = ma.getSelectedItem();
selected -= adapter.finalExamTypeList.getFirstVisiblePosition();

I get NullPointerException:: in this line

MyAdapter ma = (MyAdapter)(adapter.finalExamTypeList.getAdapter());

It seems easy to find out – adapter or adapter.finalExamTypeList should be null.

But when I check it in debug mode, nothing in this line is true null. Everything is initialized and initialized correctly. Additionally, this adapter.finalExamTypeList.getAdapter() correctly returns the MyAdapter object.

So I’m

a little worried, but anyway, I’m trying to come up with those conditions here:

accept.setBackgroundResource(R.drawable.circle_navigator_accept_checked);
ViewPager vp = (ViewPager)activity.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = (ViewPagerAdapter)vp.getAdapter();

if(adapter != null) {
    if(adapter.finalExamTypeList != null) {
        if(adapter.finalExamTypeList.getAdapter() != null) {
            MyAdapter ma = (MyAdapter)(adapter.finalExamTypeList.getAdapter());

int selected = ma.getSelectedItem();
            selected -= adapter.finalExamTypeList.getFirstVisiblePosition();
        }
    }
}

However, NullPointerException is still in the same place.

Log:

06-23 09:25:28.260: W/dalvikvm(1967): JNI WARNING: JNI method called with exception raised
06-23 09:25:28.260: W/dalvikvm(1967):              in Landroid/os/Process;.setArgV0 (Ljava/lang/String;)V (GetStringCritical)
06-23 09:25:28.260: W/dalvikvm(1967): Pending exception is:
06-23 09:25:28.260: I/dalvikvm(1967): Ljava/lang/reflect/InvocationTargetException; :
06-23 09:25:28.260: I/dalvikvm(1967):   at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967):   at java.lang.reflect.Method.invoke(Method.java:507)
06-23 09:25:28.260: I/dalvikvm(1967):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 09:25:28.260: I/dalvikvm(1967):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 09:25:28.260: I/dalvikvm(1967):   at dalvik.system.NativeStart.main(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): Caused by:
06-23 09:25:28.260: I/dalvikvm(1967): Ljava/lang/NullPointerException; :
06-23 09:25:28.260: I/dalvikvm(1967):   at pl.lodz.uni.myproject.CircleNavigator$1.onClick(CircleNavigator.java:99)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.view.View.performClick(View.java:2485)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.view.View$PerformClick.run(View.java:9080)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.os.Handler.handleCallback(Handler.java:587)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.os.Looper.loop(Looper.java:130)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.app.ActivityThread.main(ActivityThread.java:3683)
06-23 09:25:28.260: I/dalvikvm(1967):   at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967):   at java.lang.reflect.Method.invoke(Method.java:507)
06-23 09:25:28.260: I/dalvikvm(1967):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 09:25:28.260: I/dalvikvm(1967):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 09:25:28.260: I/dalvikvm(1967):   at dalvik.system.NativeStart.main(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): "main" prio=5 tid=1 NATIVE
06-23 09:25:28.260: I/dalvikvm(1967):   | group="main" sCount=0 dsCount=0 obj=0xb5f1bc38 self=0x9240e48
06-23 09:25:28.260: I/dalvikvm(1967):   | sysTid=1967 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-2145907712
06-23 09:25:28.260: I/dalvikvm(1967):   at android.os.Process.setArgV0(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967):   at android.app.ActivityThread.main(ActivityThread.java:3668)
06-23 09:25:28.260: I/dalvikvm(1967):   at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967):   at java.lang.reflect.Method.invoke(Method.java:507)
06-23 09:25:28.260: I/dalvikvm(1967):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 09:25:28.260: I/dalvikvm(1967):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 09:25:28.260: I/dalvikvm(1967):   at dalvik.system.NativeStart.main(Native Method)
06-23 09:25:28.260: E/dalvikvm(1967): VM aborting

Does anyone know what’s going on here?

Solution

But when I checked it in debug mode, nothing in this line is really null.

There are several possibilities that you have not considered.

  • This may be a race condition that you do not observe in the debugger because it does not occur when you step through code.

  • There may be errors in the debugger.

Related Problems and Solutions