Home » Android » Button Null pointer exception in android studio-Exceptionshub

Button Null pointer exception in android studio-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I try to find a view by id but I get the NullPointerException. Have you any idea where am I doing wrong?

This is the exception I’m getting

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.voicemail, PID: 24209
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.voicemail/com.example.voicemail.ComposeMail}: 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:3162)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1993)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7285)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     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.voicemail.ComposeMail.onCreate(ComposeMail.java:34)
        at android.app.Activity.performCreate(Activity.java:7353)
        at android.app.Activity.performCreate(Activity.java:7344)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3142)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1993) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7285) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

The code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing the views
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);

    editTextSubject = (EditText) findViewById(R.id.editTextSubject);

    editTextMessage = (EditText) findViewById(R.id.editTextMessage);

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

    buttonSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                sendEmail();
            }
        });
}

private void sendEmail() {
   //doSomething
}

Here’s my XML code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg2"
    android:padding="20dp"
    android:gravity="center_horizontal"
    tools:context=".ComposeMail">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="40dp"
        android:textColorHint="@android:color/white"
        android:textColor="@android:color/white"
        android:hint="@string/to"

        android:id="@+id/editTextEmail" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="40dp"
        android:textColorHint="@android:color/white"
        android:textColor="@android:color/white"
        android:hint="@string/subject"
        android:id="@+id/editTextSubject" />

    <EditText
        android:lines="15"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColorHint="@android:color/white"
        android:textColor="@android:color/white"
        android:hint="@string/message"
        android:gravity="top"
        android:id="@+id/editTextMessage" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/round_corner"
        android:text="@string/send"
        android:textColor="#ffffff"/>

</LinearLayout>

I’m getting a null pointer exception on setOnclickListener. Basically I’m sending data from one activity to another using Intent. I have tried almost everything to solve this but it keeps on showing me the same exception. Please help me.

How to&Answers:

You are not calling an appropriate design layout. Please add setContentView(R.layout.ComposeMail); instead of setContentView(R.layout.activity_main); in your class. Your class must call the appropriate design layout.

Answer:

Try this, I think it will work.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText editTextEmail, editTextSubject, editTextMessage;
Button buttonSend;

//Initializing the views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextSubject = (EditText) findViewById(R.id.editTextSubject);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);



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

private void sendEmail() {

    //Getting content for email
    String email = editTextEmail.getText().toString().trim();
    String subject = editTextSubject.getText().toString().trim();
    String message = editTextMessage.getText().toString().trim();
    String e=intent.getStringExtra("Username");
    String p=intent.getStringExtra("Password");

    //Creating SendMail object
    SendMail sm = new SendMail(this, email, subject, message, e, p);
}