java.lang.NullPointerException: Attempt to invoke

2019-02-21 06:26发布

问题:

I'm getting error when runtime my project.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olympic/com.prima.olympic.ProductDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

This is full log :

06-06 23:12:45.561: E/AndroidRuntime(17135): FATAL EXCEPTION: main
06-06 23:12:45.561: E/AndroidRuntime(17135): Process: com.example.olympic, PID: 17135
06-06 23:12:45.561: E/AndroidRuntime(17135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olympic/com.prima.olympic.ProductDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.os.Looper.loop(Looper.java:145)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.main(ActivityThread.java:5942)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at java.lang.reflect.Method.invoke(Native Method)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at java.lang.reflect.Method.invoke(Method.java:372)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
06-06 23:12:45.561: E/AndroidRuntime(17135): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
06-06 23:12:45.561: E/AndroidRuntime(17135):    at com.prima.olympic.ProductDetail.onCreate(ProductDetail.java:85)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.Activity.performCreate(Activity.java:6289)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-06 23:12:45.561: E/AndroidRuntime(17135):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)

This is my ProductDetail.java

public class ProductDetail extends Activity {
Button btnAddtoShoppingList;
Button btnDeleteShoppingList;
TextView detail_productType;
TextView detail_wdh;
TextView detail_volume;
TextView detail_weight;
TextView detail_cont20;
TextView detail_cont40;
TextView detail_pack;
TextView detail_quantity;
TextView detail_colour;
TextView detail_cetegories;
TextView detail_series;
TextView detail_price;
ImageView detail_imageView;

String shown_id, shown_type, shown_wdh, shown_volume, shown_weight, shown_cont20, shown_cont40, shown_pack, shown_quantity, shown_colour, shown_categories, shown_series, shown_price;
String customer_id, error_message ;



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

    btnAddtoShoppingList = (Button)findViewById(R.id.btnAddtoShoppingList);
    btnDeleteShoppingList = (Button)findViewById(R.id.btnDeleteShoppingList);
    SharedPreferences userInformation = getApplicationContext().getSharedPreferences("userinfo", 0);
    customer_id = userInformation.getString("customer_id", "none");

    detail_productType = (TextView)findViewById(R.id.detail_productType);
    detail_wdh = (TextView)findViewById(R.id.detail_wdh);
    detail_volume = (TextView)findViewById(R.id.detail_volume);
    detail_weight = (TextView)findViewById(R.id.detail_weight);
    detail_cont20 = (TextView)findViewById(R.id.detail_cont20);
    detail_cont40 = (TextView)findViewById(R.id.detail_cont40);
    detail_pack = (TextView)findViewById(R.id.detail_pack);
    detail_quantity = (TextView)findViewById(R.id.detail_quantity);
    detail_colour = (TextView)findViewById(R.id.detail_colour);
    detail_cetegories = (TextView)findViewById(R.id.detail_categories);
    detail_series = (TextView)findViewById(R.id.detail_series);
    detail_price = (TextView)findViewById(R.id.detail_price);
    detail_imageView = (ImageView)findViewById(R.id.detail_imageView);

    // Get Value from previous activity
    Intent i = getIntent();
    shown_id = i.getStringExtra("shown_id");
    shown_type = i.getStringExtra("shown_type");
    shown_wdh = i.getStringExtra("shown_wdh");
    shown_volume = i.getStringExtra("shown_volume");
    shown_weight = i.getStringExtra("shown_weight");
    shown_cont20 = i.getStringExtra("shown_cont20");
    shown_cont40 = i.getStringExtra("shown_cont40");
    shown_pack = i.getStringExtra("shown_pack");
    shown_quantity = i.getStringExtra("shown_quantity");
    shown_colour = i.getStringExtra("shown_colour");
    shown_categories = i.getStringExtra("shown_categories");
    shown_series = i.getStringExtra("shown_series");
    shown_price = i.getStringExtra("shown_price");

    String checkOrigin = i.getStringExtra("from_activity");
    if(checkOrigin.equals("shoppinglist")){
        btnAddtoShoppingList.setVisibility(View.GONE);
        btnDeleteShoppingList.setVisibility(View.VISIBLE);
    }

    Bitmap bitmap = (Bitmap) i.getParcelableExtra("shown_bitmap");

    detail_productType.setText("PRODUCT TYPE        : "+shown_type);
    detail_wdh.setText("W x D x H                                   : "+shown_wdh);
    detail_volume.setText("VOLUME                                   : "+shown_volume);
    detail_weight.setText("WEIGHT                                       : "+shown_weight);
    detail_cont20.setText("CONT20                                   : "+shown_cont20);
    detail_cont40.setText("CONT40                                   : "+shown_cont40);
    detail_pack.setText("PACK                                               : "+shown_pack);
    detail_quantity.setText("QUANTITY                           : "+shown_quantity);
    detail_colour.setText("COLOUR                                   : "+shown_colour);
    detail_cetegories.setText("CATEGORIES                   : "+shown_categories);
    detail_series.setText("SERIES                                           : "+shown_series);
    detail_price.setText("PRICE                                             : Rp "+shown_price);
    detail_imageView.setImageBitmap(bitmap);



    btnAddtoShoppingList.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v)
        {
            addtoShoppingListTask addtoShoppingList = new addtoShoppingListTask();
            addtoShoppingList.execute((Void) null);

        }
    });

    btnDeleteShoppingList.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v)
        {
            deleteShoppingListTask deletetoShoppingList = new deleteShoppingListTask();
            deletetoShoppingList.execute((Void) null);

        }
    });

}

can anyone tell me where is the error and how to fix it please? if anyone can help i will be so thankful.

回答1:

Your String checkOrigin is null thus giving you a NullPointerException. This is what is causing it:

String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin.equals("shoppinglist")){
    btnAddtoShoppingList.setVisibility(View.GONE);
    btnDeleteShoppingList.setVisibility(View.VISIBLE);
}

The String checkOrigin is null because you are not receiving any value from your Intent. This might be because you forgot to pass a value from your previous Activity.
However, you can check to see if your String is null and if it is, then those actions won't be performed. You would do this by the following:

String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin != null && checkOrigin.equals("shoppinglist")){
    btnAddtoShoppingList.setVisibility(View.GONE);
    btnDeleteShoppingList.setVisibility(View.VISIBLE);
}

Therefore, if checkOrigin is null, then those actions won't be performed.

But I would recommend checking the Activity that you are receiving the Intent from to make sure that you are sending and receiving the Intent correctly.

If you want to read more about NullPointerExceptions, have a look at this question and its answers for more detail.



回答2:

Change <view> to <View>, because view is not about empty view. It's for custom view defined through class attr, like below:

<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.your.package.YourCustomView" />

And you got error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference


回答3:

checkOrigin is null

String checkOrigin = i.getStringExtra("from_activity");
        if(checkOrigin!=null && checkOrigin.equals("shoppinglist")){
            btnAddtoShoppingList.setVisibility(View.GONE);
            btnDeleteShoppingList.setVisibility(View.VISIBLE);
        }