Attempt to invoke virtual method ‘void android.widget.Button.setOnClickListener

Here two buttons are used one is submit button and other is display button but application crashed before start and got error…

java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference

Error log

    Process: com.example.pickgallery, PID: 24404
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pickgallery/com.example.pickgallery.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 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.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.pickgallery.MainActivity.insertData(MainActivity.java:55)
        at com.example.pickgallery.MainActivity.onCreate(MainActivity.java:46)
        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) 

Also checked the button which initialized perfectly but getting same error and rewritten code also same error occurs “android.widget.Button.setOnClickListener(android.view.View$OnClickListener)’ a null object reference Feather”

Solution

First you check button id in xml code and java id which is correct initialize or not. Also You need to check the method. Yes here silly mistake because here created three methods “findid()” is the second method, then “findid()” replace and set the first. Change the first method and solve the problem.

In the end, we found that this error occurred only in a sequence of methods in Android Studio. After sequence change the error is resolved and works perfectly.

So if you are getting this type of error then check the method if you have created.

Leave a Reply