当前位置: 首页 > 面试题库 >

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'booleanjava.lang.String.equals(java.lang.Object)'

严令秋
2023-03-14
问题内容

运行我的项目时出现错误。

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): 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)

这是我的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);

        }
    });

}

谁能告诉我错误在哪里以及如何解决?如果有人可以帮助我,我将非常感激。


问题答案:

您的字符串checkOriginnull这样给你一个NullPointerException。这是导致它的原因:

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

字符串checkOriginnull因为您没有从接收任何值Intent。这可能是因为您忘记了传递以前的值Activity
但是,您可以检查一下String null是否存在,那么将不会执行这些操作。您可以通过以下方式做到这一点:

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

因此,如果checkOrigin为null,则将不会执行这些操作。

但我建议您检查Activity接收Intent来自的消息,以确保Intent正确发送和接收消息。



 类似资料: