On a null object reference, Attempt to invoke in android studio

  • Post comments:0 Comments
  

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pd.myapplication/com.pd.myapplication.DisplayData}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375)

at android.app.ActivityThread.access$800(ActivityThread.java:148)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)

at android.os.Handler.dispatchMessage(Handler.java:102)

        at android.os.Looper.loop(Looper.java:135)

        at android.app.ActivityThread.main(ActivityThread.java:5240)

        at java.lang.reflect.Method.invoke(Native Method)

        at java.lang.reflect.Method.invoke(Method.java:372)

        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

        at com.pd.myapplication.DisplayData.displayData(DisplayData.java:58)

        at com.pd.myapplication.DisplayData.onCreate(DisplayData.java:28)

        at android.app.Activity.performCreate(Activity.java:6008)

        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)

        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)

        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375) 

        at android.app.ActivityThread.access$800(ActivityThread.java:148) 

        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282) 

        at android.os.Handler.dispatchMessage(Handler.java:102) 

        at android.os.Looper.loop(Looper.java:135) 

        at android.app.ActivityThread.main(ActivityThread.java:5240) 

        at java.lang.reflect.Method.invoke(Native Method) 

        at java.lang.reflect.Method.invoke(Method.java:372) 

        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 

Null object reference error in android studio

so friends, in this error solution tutorial, we’ll discuss the error “null object reference”.

Often, we are fetching this type of error in android studio.

Also Read : one activity to another new activity

What do if you’re getting a null object reference error, however, you can solve it easily or not, although if you know android programming, and also you previously solved error then this will make you solve easily.

When and why does this error occur?

This is not a compile-time error but when you perform some task inactivity in your device or run your app fine, midway your application crashes. For example, you make two buttons, generally, one is submitted and another is displayed. However, clicking on the display button, the app crashes. For the most part of the android studio, this error happens due to a null object, which means you create the object, but you forgot casting, so at that time you getting an error.

How to solve null object reference error in android studio?

In this case, you don’t need to worry about this type of error because here given an accurate solution. Whenever you get this type of issue in your android studio, first, check you don’t forget initializing.  Sometimes you define a pointer, but you forgot to assign it. So check variable then check method that it should be proper initialization. If you don’t understand, watch videos on YouTube.

If you have any queries regarding this post, feel free to contact or comment. Stay tuned.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments