-1

Am going through the tutorial in the below link to have a simple Android App with more than one activities.

https://developer.android.com/training/basics/firstapp/starting-activity.html

I have followed the steps as specified in the android tutorial, I run into a NullPointer Exception on the EditText Widget,

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

Below is my MainActivity.java where the exception as editText returns Null though I assume I have wired it in the right way,

public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.my.application.message";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void sendMessage(View view){
    Intent intent = new Intent( this , DisplayMessageActivity.class);
    EditText editText;
    editText = (EditText) view.findViewById(R.id.edit_message);

    System.out.println("the values is "+ editText);

    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);

   }
}

And below is the activity_main.xml ,

<?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="horizontal">
 <EditText android:id="@+id/edit_message"
     android:layout_width="0dp"
     android:layout_weight="1"
     android:layout_height="wrap_content"
     android:hint="@string/edit_message" />
 <Button
     android:id="@+id/button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/do_send"
     android:onClick="sendMessage"/>
</LinearLayout>

Any pointers here to get past this ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ashwin Sridhar
  • 125
  • 1
  • 14

5 Answers5

2

on line of click :

   editText = (EditText) view.findViewById(R.id.edit_message);

"view" is that button. and by using view.findViewById, you are trying to find required view in that button (which is not parent of you EditText). So you should try following ways :

Direct :

editText = (EditText) findViewById(R.id.edit_message);

here findViewById will try to find id in current Activity.

using parentView :

editText = (EditText) (view.getParent().findViewById(R.id.edit_message));

here your button's parent is EditText's parent, so it will find you EditText by this code also.

NehaK
  • 2,639
  • 1
  • 15
  • 31
0

Your function is in an Activity so you don't need to do view.findViewById(). Instead, simply do the following :

EditText editText;
editText = (EditText) findViewById(R.id.edit_message);
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
0

NullPoint address to those objects which has null values, put proper id and code:

public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.my.application.message";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void sendMessage(View view){
    Intent intent = new Intent( this , DisplayMessageActivity.class);
    EditText editText;
    editText = (EditText) findViewById(R.id.edit_message); //here

    System.out.println("the values is "+ editText);

    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);

   }
}
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

You're trying to find an EditText from the clicked button

editText = (EditText) view.findViewById(R.id.edit_message);

You have to find it from the Activity

editText = (EditText) findViewById(R.id.edit_message);

Read that tutorial again, you'll spot the difference

Charuක
  • 12,953
  • 5
  • 50
  • 88
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

As advice, I would first seek that it is not null or empty.

       if(editText != null && !editText.getText.toString.equals("")) {
            ...
       }

After I would follow the advice of ρяσѕρєя K:

EditText is in Activity layout instead of in view (which is instance of clicked View(in your case it is Button)), so just access it without view like :editText = (EditText)findViewById(R.id.edit_message);

Emmanuelguther
  • 1,039
  • 4
  • 19
  • 29