tagz2712
tagz2712

Reputation: 55

NullPointerException Android - Unable to start activity

Not quite sure why im getting the following error since the code is lifted from another page and working fine there...

01-19 22:38:06.443: E/AndroidRuntime(27945): FATAL EXCEPTION: main
01-19 22:38:06.443: E/AndroidRuntime(27945): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fooditemmonitor/com.example.fooditemmonitor.CurrentItems}: java.lang.NullPointerException
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.os.Looper.loop(Looper.java:176)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.ActivityThread.main(ActivityThread.java:5419)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at java.lang.reflect.Method.invoke(Method.java:525)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at dalvik.system.NativeStart.main(Native Method)
01-19 22:38:06.443: E/AndroidRuntime(27945): Caused by: java.lang.NullPointerException
01-19 22:38:06.443: E/AndroidRuntime(27945):    at com.example.fooditemmonitor.CurrentItems.onCreate(CurrentItems.java:22)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.Activity.performCreate(Activity.java:5372)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-19 22:38:06.443: E/AndroidRuntime(27945):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
01-19 22:38:06.443: E/AndroidRuntime(27945):    ... 11 more

current_inventory.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="1" >

    <Button
        android:id="@+id/editItemCurrent"
        android:layout_width="145dp"
        android:layout_column="0"
        android:layout_gravity="right|bottom"
        android:layout_row="0"
        android:text="@string/editItem" />

    <Button
        android:id="@+id/scanCurrent"
        android:layout_width="160dp"
        android:layout_column="0"
        android:layout_gravity="left|bottom"
        android:layout_row="0"
        android:text="@string/scan" />

</GridLayout>

CurrentItems.java

package com.example.fooditemmonitor;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CurrentItems extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_inventory);

        final Button addButton = (Button) findViewById(R.id.scanner);
        final Button editInventoryButton = (Button) findViewById(R.id.editItem);

        addButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent(CurrentItems.this, AddItem.class));
            }
        });

        editInventoryButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent(CurrentItems.this, EditItems.class));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

addButton.setOnClickListener(new OnClickListener() { //LINE 22

Code posted from the files will be related to this. Have checked that the names of files referenced are correct.

All help will be appreciated - thanks!

Upvotes: 0

Views: 727

Answers (2)

Dani
Dani

Reputation: 4111

R.id.scanner and R.id.editItem are not present in your current_inventory.xml file.

Then, when you are trying to setOnClickListener in (Button) addButton is null and throws NullPointerException.

Upvotes: 3

Alexis C.
Alexis C.

Reputation: 93842

I think you made a typo, it should be :

final Button addButton = (Button) findViewById(R.id.editItemCurrent);
final Button editInventoryButton = (Button) findViewById(R.id.scanCurrent);

I see no buttons with the id scanner or editItem in your layout.

Upvotes: 5

Related Questions