-3

Keep trying to run my app but get the following error:

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

This occurs whenever I actually try to run any code using the variables on that page. Simply declaring their ID's doesn't pose any error, I guess because they are never called.

I'm not sure what I have got wrong. I know I will need a check for the parsing because a NumberException may occur, but it won't even reach that point and I don't know why. even a basic .getText() causes a crash.

I checked the link of a duplicate question but I can't see where I may have not initialized correctly.

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

    // variable declarations for reservation details
    final EditText emailInput = (EditText) findViewById(R.id.emailInput);
    final EditText phoneInput = (EditText) findViewById(R.id.phoneInput);
    final EditText dateInput = (EditText) findViewById(R.id.dateInput);
    final EditText totalAdults = (EditText) findViewById(R.id.totalAdults);
    final EditText totalKids = (EditText) findViewById(R.id.totalKids);

    // declare next and back buttons for all pages
    final Button nextToDinner = (Button) findViewById(R.id.nextToDinner);
    final Button backToReservation = (Button) findViewById(R.id.backToReservation);
    final Button nextToProcessing = (Button) findViewById(R.id.nextToProcessing);

    // spinner declarations
    final Spinner countySpinner = (Spinner) findViewById(R.id.countySpinner);
    final Spinner timeSpinner = (Spinner) findViewById(R.id.timeSpinner);

    // variable declarations for dinner choices
    final EditText tendiesInput = (EditText) findViewById(R.id.tendiesInput);
    final EditText crabInput = (EditText) findViewById(R.id.crabInput);
    final EditText saladInput = (EditText) findViewById(R.id.saladInput);
    final EditText wingsInput = (EditText) findViewById(R.id.wingsInput);
    final EditText soupInput = (EditText) findViewById(R.id.soupInput);
    final EditText fishDinnerInput = (EditText) findViewById(R.id.fishDinnerInput);
    final EditText steakDinnerInput = (EditText) findViewById(R.id.steakDinnerInput);
    final EditText chickenDinnerInput = (EditText) findViewById(R.id.chickenDinnerInput);
    final EditText kidsDinnerInput = (EditText) findViewById(R.id.kidsDinnerInput);
    final TextView totalAmount = (TextView) findViewById(R.id.totalAmount);

    // declare starter prices
    final double chickenTendies = 10.99;
    final double crabDip = 13.99;
    final double salad = 5.99;
    final double buffaloWings = 8.99;
    final double soup = 6.99;

    // declare dinner prices
    final double fishDinner = 26.99;
    final double steakDinner = 31.99;
    final double chickenDinner = 24.99;
    final double kidsDinner = 15.99;

    // variable declarations for confirmation page
    final TextView confirmEmail = (TextView) findViewById(R.id.confirmEmail);
    final TextView confirmPhone = (TextView) findViewById(R.id.confirmPhone);
    final TextView confirmCounty = (TextView) findViewById(R.id.confirmCounty);
    final TextView confirmTime = (TextView) findViewById(R.id.confirmTime);
    final TextView confirmDate = (TextView) findViewById(R.id.confirmDate);
    final TextView confirmAdults = (TextView) findViewById(R.id.confirmAdults);
    final TextView confirmKids = (TextView) findViewById(R.id.confirmKids);
    final TextView confirmApps = (TextView) findViewById(R.id.confirmApps);
    final TextView confirmDinners = (TextView) findViewById(R.id.confirmDinners);
    final TextView confirmTotal = (TextView) findViewById(R.id.confirmTotal);
    final TextView confirmAddress = (TextView) findViewById(R.id.confirmAddress);

    // get user input for reservation details and store in variable(s)
    final String storeEmail = emailInput.getText().toString();
    final String storePhone = phoneInput.getText().toString();
    final String storeDate = dateInput.getText().toString();
    final String storeAdults = totalAdults.getText().toString();
    final String storeKids = totalKids.getText().toString();

    // declare array adapters for county and time
    ArrayAdapter countiesArray = new ArrayAdapter(this, android.R.layout.simple_selectable_list_item, county);
    ArrayAdapter timeArray = new ArrayAdapter(this, android.R.layout.simple_selectable_list_item, time);

    countySpinner.setOnItemSelectedListener(this);
    timeSpinner.setOnItemSelectedListener(this);

    countySpinner.setAdapter(countiesArray);
    timeSpinner.setAdapter(timeArray);

** these lines break the code even before a NumberException can occur**
    // get user input for appetizers and parse to double variables for calculation
    final double tendiesNum = Double.parseDouble(tendiesInput.getText().toString());
    final double crabNum = Double.parseDouble(crabInput.getText().toString());
    final double saladNum = Double.parseDouble(saladInput.getText().toString());
    final double wingsNum = Double.parseDouble(wingsInput.getText().toString());
    final double soupNum = Double.parseDouble(soupInput.getText().toString());

    // get user input for dinners and parse to double variables for calculation
    final double fishNum = Double.parseDouble(fishDinnerInput.getText().toString());
    final double chickenNum = Double.parseDouble(chickenDinnerInput.getText().toString());
    final double steakNum = Double.parseDouble(steakDinnerInput.getText().toString());
    final double kidsNum = Double.parseDouble(kidsDinnerInput.getText().toString());

XML for respective page:

<Button
    android:id="@+id/backToReservation"
    android:layout_width="75dp"
    android:layout_height="40dp"
    android:text="BACK"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="293dp"
    app:layout_constraintRight_toRightOf="parent" />

<Button
    android:id="@+id/nextToProcessing"
    android:layout_width="85dp"
    android:layout_height="40dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="259dp"
    android:text="CONFIRM"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/starterPrice1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$10.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/tendiesText" />

<TextView
    android:id="@+id/starterPrice2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$13.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/crabText" />

<TextView
    android:id="@+id/starterPrice3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$5.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="124dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/saladText" />

<TextView
    android:id="@+id/starterPrice4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$8.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="124dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/wingsText" />

<TextView
    android:id="@+id/starterPrice5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$6.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="124dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/soupText" />

<TextView
    android:id="@+id/dinnerPrice1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$26.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/fishDinnerText" />

<TextView
    android:id="@+id/dinnerPrice2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$31.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/steakDinnerText" />

<TextView
    android:id="@+id/dinnerPrice3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$24.99"
    android:textSize="10sp"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/chickenDinnerText" />

<TextView
    android:id="@+id/dinnerPrice4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$15.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/kidsDinnerText" />

<TextView
    android:id="@+id/totalAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="178dp"
    android:text="Total: "
    app:layout_constraintTop_toBottomOf="@+id/dinnerTitle"
    android:layout_marginLeft="109dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/menuText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Menu"
    android:textAlignment="center"
    android:textSize="24sp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="162dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/appetizerTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Appetizers"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_marginTop="2dp"
    app:layout_constraintTop_toBottomOf="@+id/menuText"
    android:layout_marginLeft="149dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/tendiesText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chicken Tenders:"
    android:textAlignment="textStart"
    android:textSize="16sp"
    android:layout_marginLeft="29dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/tendiesInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/tendiesInput"
    android:layout_width="100dp"
    android:layout_height="35dp"
    android:layout_marginLeft="100dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:inputType="number"
    android:textAlignment="center"
    android:textSize="12sp"
    app:layout_constraintLeft_toRightOf="@+id/tendiesText"
    app:layout_constraintTop_toBottomOf="@+id/appetizerTitle" />

<TextView
    android:id="@+id/crabText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Crab Dip:"
    android:textSize="16sp"
    android:layout_marginLeft="85dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/crabInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/crabInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/tendiesInput"
    app:layout_constraintLeft_toRightOf="@+id/crabText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/wingsText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Buffalo Wings:"
    android:textSize="16sp"
    android:layout_marginLeft="47dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/wingsInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/wingsInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/saladInput"
    app:layout_constraintLeft_toRightOf="@+id/wingsText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/soupText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Soup Special:"
    android:textSize="16sp"
    android:layout_marginLeft="57dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/soupInput"
    android:layout_marginTop="-4dp" />

<EditText
    android:id="@+id/soupInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/wingsInput"
    app:layout_constraintLeft_toRightOf="@+id/soupText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/saladText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Salad:"
    android:textSize="16sp"
    android:layout_marginLeft="107dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/saladInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/saladInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/crabInput"
    app:layout_constraintLeft_toRightOf="@+id/saladText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/dinnerTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dinners"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_marginTop="205dp"
    app:layout_constraintTop_toBottomOf="@+id/appetizerTitle"
    android:layout_marginLeft="161dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/fishDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fish:"
    android:textSize="16sp"
    android:layout_marginLeft="116dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/fishDinnerInput"
    android:layout_marginTop="-8dp" />

<EditText
    android:id="@+id/fishDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/dinnerTitle"
    app:layout_constraintLeft_toRightOf="@+id/fishDinnerText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/steakDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Steak:"
    android:textSize="16sp"
    android:layout_marginLeft="106dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/steakDinnerInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/steakDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/fishDinnerInput"
    app:layout_constraintLeft_toRightOf="@+id/steakDinnerText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/chickenDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chicken:"
    android:textSize="16sp"
    android:layout_marginLeft="90dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/chickenDinnerInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/chickenDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/steakDinnerInput"
    app:layout_constraintLeft_toRightOf="@+id/chickenDinnerText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/kidsDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Kid's Choice"
    android:textSize="16sp"
    android:layout_marginLeft="65dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/kidsDinnerInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/kidsDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/chickenDinnerInput"
    app:layout_constraintLeft_toRightOf="@+id/kidsDinnerText"
    android:layout_marginLeft="100dp" />

Error log:

                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.dtcc.dinnerreservationapp/edu.dtcc.dinnerreservationapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                 at edu.dtcc.dinnerreservationapp.MainActivity.onCreate(MainActivity.java:116)
                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  • 1
    The stack trace will specify on which line the Exception occurs. Find that line, determine which `EditText` reference is null, and then figure out whether you forgot to assign it, or if it's not in the layout you passed in `setContentView()`. – Mike M. Oct 12 '17 at 03:10
  • add `R.layout.activity_main` – Om Infowave Developers Oct 12 '17 at 03:28
  • `MainActivity.onCreate(MainActivity.java:116)` - Find line 116 in `MainActivity`, and proceed as I described above. We can't see your line numbers. – Mike M. Oct 12 '17 at 04:29
  • MainActivity.onCreate(MainActivity.java:116) is simply me parsing a double(actually the first line of code where I say it breaks). Even with a basic if statement check to avoid a NumberException it still crashes. – MrAchilles Oct 12 '17 at 04:44

1 Answers1

0

the edit text from which you want to get text id is not properly added in code re check your id assigning to edit text

Naseem Ahmad
  • 103
  • 8