0

I am facing the problem when I use Intent to send a value to another activity by click a button. I have tried to solve it in many ways but it always show NullPointerException although I declared it clearly.

Here is my code.

Manufacturer.java

package com.example.student.macheckcar;

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.Button;

 public class Manufacturer extends AppCompatActivity {
 public String message;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manufacturer);
    Button toyota = (Button) findViewById(R.id.Toyota);
    Button subaru = (Button) findViewById(R.id.Subaru);
    Button audi = (Button) findViewById(R.id.Audi);

    toyota.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            message = "Toyota";
            goAnother(message.toString());
        }
    });

    subaru.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            message = "Subaru";
            goAnother(message);
        }
    });

    audi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            message = "Audi";
            goAnother(message);
        }
    });
 }

 protected  void goAnother(String brand){
    Intent i = new Intent(Manufacturer.this, ShowCarPrice.class);
    i.putExtra("brand", brand);
    startActivity(i);
 }
}

Showcarprice.java

package com.example.student.macheckcar;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.Window;
import java.util.ArrayList;

public class ShowCarPrice extends AppCompatActivity {
    private Cursor cursor;
    private ArrayList<String> price;
    private ArrayAdapter<String> aa;
    private DBprice dbPrice;
    private Manufacturer maFact;
    SQLiteDatabase db;
    ListView list;
    Intent intent = getIntent();
    String brand = intent.getStringExtra("brand");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_car_price);

        list = (ListView) findViewById(R.id.listView);
        dbPrice = new DBprice(this);
        db = dbPrice.getWritableDatabase();
        cursor = db.rawQuery("SELECT " + dbPrice.KEY_TASK + " FROM " + dbPrice.TABLE_NAME + " WHERE Manufacturer = ?", new String[] {brand});
        price = new ArrayList<String>();
        cursor.moveToFirst();
        while ( !cursor.isAfterLast() ){
                price.add(cursor.getString(cursor.getColumnIndex(dbPrice.KEY_TASK)));
        cursor.moveToNext();
        }
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, price);
        list.setAdapter(aa);
    }

    public void onPause() {
        super.onPause();
        dbPrice.close();
        db.close();
    }
}

And the error:

E/AndroidRuntime: FATAL EXCEPTION: main
                                   Process: com.example.student.macheckcar, PID: 934
                                   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.student.macheckcar/com.example.student.macheckcar.ShowCarPrice}: java.lang.NullPointerException
                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                   at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                   at android.os.Looper.loop(Looper.java:136)
                                   at android.app.ActivityThread.main(ActivityThread.java:5001)
                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                   at dalvik.system.NativeStart.main(Native Method)
                  Caused by: java.lang.NullPointerException
                                   at com.example.student.macheckcar.ShowCarPrice.<init>(ShowCarPrice.java:22)
                                   at java.lang.Class.newInstanceImpl(Native Method)
                                   at java.lang.Class.newInstance(Class.java:1208)
                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                   at android.os.Looper.loop(Looper.java:136) 
                                   at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                   at dalvik.system.NativeStart.main(Native Method) 

Please help.

3 Answers3

1

UPDATE

public class ShowCarPrice extends AppCompatActivity {

    // OK, Create the objects, variables here if needed
    Intent intent;
    String brand;

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

        // You should assign the intent here (in onCreate method)
        intent = getIntent();

        // Then, the string variable
        brand = intent.getStringExtra("brand");

        // You can also check whether or not received a valid value
        if (brand != null && !brand.isEmpty())
        // do something with brand value
        else
        // brand value is null or empty
    }
}
Robert
  • 10,403
  • 14
  • 67
  • 117
Arnaldo
  • 673
  • 6
  • 22
  • Code dumps are not particularly useful - it can be hard to pick out what you have changed. You should add some explanation of the key points in your solution, and/or remove irrelevant code. – Andy Turner Apr 24 '16 at 20:59
  • Thank you @AndyTurner, I just did it – Arnaldo Apr 24 '16 at 21:22
  • @user5195185, well if you did it you should post your solution or accept the Andy Turner answer. – Robert Apr 25 '16 at 03:15
0

Well, you call getIntent() at the wrong position.
You should call getIntent() in method onCreate() , and I don't know why you keep intent as a member of your activity. If I were you I just do

    String brand = getIntent().getStringExtra("brand");

in onCreate() method.

Usher
  • 41
  • 1
0

get your intent and extras of intent in onCreate() method of activity just check below link for this

https://stackoverflow.com/a/5265952/5316836

Community
  • 1
  • 1
Pitty
  • 1,907
  • 5
  • 16
  • 34