Home » Java » java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)

Posted by: admin July 12, 2018 Leave a comment

Questions:

can anyone help me solve this problem I don’t understand what exactly is wrong when I click on activity my app crashing I have already assigned the button still log showing me below error? I am unable to find the exception here. I tried by changing the id of a button. Then also, it didn’t work. In XML file, Button is included.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.niyanta.askforleave/com.example.niyanta.askforleave.Activity.EmployeeLeave}: 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:2911)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2989)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6753)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 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.niyanta.askforleave.Activity.EmployeeLeave.onCreate(EmployeeLeave.java:88)
    at android.app.Activity.performCreate(Activity.java:7026)
    at android.app.Activity.performCreate(Activity.java:7017)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1231)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2864)

Here is my java code…..

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_employee_leave);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    ImageView ivBack = toolbar.findViewById(R.id.ivBack);

    ivBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

    recyclerView = (RecyclerView) findViewById(R.id.employee_leave_list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    Employeeleavelist = new ArrayList<>();

    if (!isConnectd(getApplicationContext())) {
        Toast.makeText(this, "Check your network connection", Toast.LENGTH_SHORT).show();
    } else {

        Log.e("LOGIN", "CALL API");
        getemployeelist();
    }

    btnApprove =(Button)findViewById(R.id.btnApprove) ;
    btnApprove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            approved();
        }
    });



    btnReject =(Button) findViewById(R.id.btnReject);

}

here is my XML code :

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:layout_marginTop="5dp"
                >

                <Button
                    android:id="@+id/btnApprove"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/custom_button3"
                    android:text="Approve"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    />

                <LinearLayout
                    android:layout_width="4dp"
                    android:layout_height="match_parent"
                    android:background="@color/white"></LinearLayout>

                <Button
                    android:id="@+id/btnReject"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/custom_button2"
                    android:text="Reject"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:onClick="ShowDialog"/>
Answers: