NullPointerException on going to another activity, android studio

Question!

update I posted the new LogCat reports, and I know there is something related to my second class named "Done" returning null which is causing this error:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rocrider.foodmenue/com.rocrider.foodmenue.Done}: java.lang.NullPointerException

I think the problem is somewhere in association of the class Done, with the button BDone and second_activity XML file.

Just for information, I do give more than 0 values to food1amount and food2amount when my app starts. After I push the button "BDone", it crashes.

This app is supposed to be for waiters and waitresses to take orders from customers on tablets and then the program is supposed to make a list of foods with amounts ordered more than 0, "and later, not in this code" it is supposed to be able to send it to the printer in restaurant's kitchen.

My main class named MainActivity:

package com.rocrider.foodmenue;

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

public class MainActivity extends Activity {

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

        Button Bdone = (Button) findViewById(R.id.Bdone);
        Bdone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, Done.class));
            }
        });
    }

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

My second class named Done, supposedly associated with second_activity.xml:

package com.rocrider.foodmenue;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class Done extends MainActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Done();
    }

    public void BDone(){
        EditText Tfood1amount = (EditText) findViewById(R.id.TFood1Amount);
        EditText Tfood2amount = (EditText) findViewById(R.id.TFood2Amount);
        ListView lvlast = (ListView) findViewById(R.id.LVfinal);
        final ArrayList<String> OrderList = new ArrayList<String>();
        final ArrayAdapter<String> adapter;
        int food1amount = 0;
        int food2amount = 0;
        food1amount = Integer.parseInt(String.valueOf(Tfood1amount.getText()));
        food2amount = Integer.parseInt(String.valueOf(Tfood2amount.getText()));

        adapter = new ArrayAdapter<String>(this,R.id.LVfinal,OrderList);
        lvlast.setAdapter(adapter);

        if(food1amount != 0){
            class alaki{
                public void adder(View v){
                    OrderList.add("Food1: ");
                    adapter.notifyDataSetChanged();
                }
            }
        }

        if(food2amount != 0){
            class alaki{
                public void adder(View v){
                    OrderList.add("Food2: ");
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}

My androidmanifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rocrider.foodmenue"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rocrider.foodmenue.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Done"/>
    </application>
</manifest>

Latest LogCat report:

09-29 10:40:58.976  15228-15228/com.rocrider.foodmenue E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rocrider.foodmenue/com.rocrider.foodmenue.Done}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.rocrider.foodmenue.Done.BDone(Done.java:37)
    at com.rocrider.foodmenue.Done.onCreate(Done.java:19)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more
    09-29 10:40:58.980      118-136/? E/? Dumpstate > /data/log/dumpstate_app_error


Answers
  Button bdone = (Button)findViewById(id of button);
  bdone.setOnClickListener(new OnlickListener)
  {
      @Override
      public void onClick(View view)
      {
          //Code here
      }
  }
By : gbl


Your problem is in the manual creation of the second activity, because onCreate doesn't get called, and you get a NullPoinerException. BDone() should called from onCreate in the Done activity.

By : Vang


In OnClick in MainActivity, you are calling done.BDone(); which is causing the error. Because at that time the layout has not been inflated in the Done activity, so findViewById is throwing an error. Call BDone from onCreate in the Done activity.

In the main class, do this:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button Bdone = (Button) findViewById(R.id.Bdone);
        Bdone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, Done.class));
            }
        });
    }
}

And in the Done class, do this:

public class Done extends MainActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        BDone();
    }
By : rachit


This video can help you solving your question :)
By: admin