-3

Okay, this is my first post to stack so please bear with me. I have searched and have tried many things. I think another set of eyes might see something I don't. I get the NPE when i call the getText() function. I have included my scaled down version of my code, but the same error is happening. I have even looked that I have the right View any help will be appreciated.

MainActivity.java

public class MainActivity extends AppCompatActivity {
   private ArrayList<Task> taskList;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LayoutInflater inflater2 = (LayoutInflater)getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);

            View customView2 = inflater2.inflate(R.layout.task_add_operations,null);

            final PopupWindow mPopupWindow2 = new PopupWindow(customView2,
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT, true);


            Button closeButton2 = (Button)customView2.findViewById(R.id.closeButton2);
            closeButton2.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopupWindow2.dismiss();
                }
            });

            Button addButton = (Button)customView2.findViewById(R.id.addButton);
            addButton.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View customView2) {
                    EditText text = customView2.findViewById(R.id.editText);

                    //This is where my java.lang.NullPointerException pops
                    String title = text.getText().toString();

                    taskList = Task.addTask(taskList, title);

                    mPopupWindow2.dismiss();

                }
            });
            mPopupWindow2.showAtLocation(findViewById(R.id.activityMain),
                    CENTER,0, 0);
        }
    });
}

task_add_operations.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp"
        android:text="Add Task"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/editText"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="text"
        android:text="Title" />

    <Button
        android:id="@+id/closeButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText"
        android:layout_below="@+id/editText"
        android:layout_marginTop="28dp"
        android:text="Cancel" />

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/closeButton2"
        android:layout_alignBottom="@+id/closeButton2"
        android:layout_alignEnd="@+id/editText"
        android:layout_alignRight="@+id/editText"
        android:text="Add Task" />
</RelativeLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
LittleJoe
  • 9
  • 2
  • try this line EditText text = (EditText) customView2.findViewById(R.id.editText); – Vidhi Dave Nov 30 '17 at 08:50
  • can u please share error log. – coder Nov 30 '17 at 08:50
  • @VishvaDave it's a bit more subtle than a simple NullPointerException because it is caused by variable hiding (see answer by Prem below), and I didn't see that specific case mentioned in the top 5 answers of the question you linked – Jeroen Steenbeeke Nov 30 '17 at 09:01

2 Answers2

2

You need to Bind Your EditText Outside addButton.setOnClickListener

EditText text =(EditText) customView2.findViewById(R.id.editText);

OR Try this change the view name of public void onClick(View View2) method like below code

Button addButton = (Button)customView2.findViewById(R.id.addButton);
            addButton.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View View2) {
                    EditText text = (EditText)customView2.findViewById(R.id.editText);

                    //This is where my java.lang.NullPointerException pops
                    String title = text.getText().toString();

                    taskList = Task.addTask(taskList, title);

                    mPopupWindow2.dismiss();

                }
            });
Goku
  • 9,102
  • 8
  • 50
  • 81
0

From looking at this other Stack Overflow post:

Null Pointer Exception - Getting EditText Value

Looks like you might want to try to declare the EditText in OnCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button addButton = (Button)customView2.findViewById(R.id.addButton);
    EditText text = customView2.findViewById(R.id.editText);            
    addButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View customView2) {


                //This is where my java.lang.NullPointerException pops
                String title = text.getText().toString();

                taskList = Task.addTask(taskList, title);

                mPopupWindow2.dismiss();

            }
        });
Andrew Steinmetz
  • 1,010
  • 8
  • 16