NullPointerException:尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)'
NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
我在运行时遇到这个错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo
java.lang.NullPointerException: Attempt to invoke virtual method
'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a
null object reference
堆栈跟踪:
01-12 03:44:54.270: E/AndroidRuntime(1437): FATAL EXCEPTION: main
01-12 03:44:54.270: E/AndroidRuntime(1437): Process: home.saket, PID: 1437
01-12 03:44:54.270: E/AndroidRuntime(1437): java.lang.RuntimeException: Unable to start activity ComponentInfo{home.saket/home.saket.addmember.Add_Update_User}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.access0(ActivityThread.java:144)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.os.Handler.dispatchMessage(Handler.java:102)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.os.Looper.loop(Looper.java:135)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-12 03:44:54.270: E/AndroidRuntime(1437): at java.lang.reflect.Method.invoke(Native Method)
01-12 03:44:54.270: E/AndroidRuntime(1437): at java.lang.reflect.Method.invoke(Method.java:372)
01-12 03:44:54.270: E/AndroidRuntime(1437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-12 03:44:54.270: E/AndroidRuntime(1437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-12 03:44:54.270: E/AndroidRuntime(1437): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
01-12 03:44:54.270: E/AndroidRuntime(1437): at home.saket.addmember.Add_Update_User.onCreate(Add_Update_User.java:38)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.Activity.performCreate(Activity.java:5933)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-12 03:44:54.270: E/AndroidRuntime(1437): ... 10 more
01-12 03:44:54.272: W/ActivityManager(472): Force finishing activity home.saket/.addmember.Add_Update_User
01-12 03:44:54.273: E/ActivityManager(472): Invalid thumbnail dimensions: 384x384
下面贴出代码并指出错误行。
Add_Update_User.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_update_screen);
// set screen
Set_Add_Update_Screen();
// set visibility of view as per calling activity
String called_from = getIntent().getStringExtra("called");
if (called_from.equalsIgnoreCase("add")) { --->38th error line
add_view.setVisibility(View.VISIBLE);
update_view.setVisibility(View.GONE);
} else {
update_view.setVisibility(View.VISIBLE);
add_view.setVisibility(View.GONE);
USER_ID = Integer.parseInt(getIntent().getStringExtra("USER_ID"));
Contact c = dbHandler.Get_Contact(USER_ID);
add_name.setText(c.getName());
add_mobile.setText(c.getPhoneNumber());
add_email.setText(c.getEmail());
// dbHandler.close();
}
}
这是错误行:
if (called_from.equalsIgnoreCase("add")) { --->38th error line
这意味着 called_from
为空。在使用之前简单检查它是否为空:
String called_from = getIntent().getStringExtra("called");
if (called_from != null && called_from.equalsIgnoreCase("add")) {
// do whatever
} else {
// do whatever
}
这样,如果 called_from
为空,它将执行条件的 else
部分。
called_from
必须是 null
。添加针对该条件的测试,如
if (called_from != null && called_from.equalsIgnoreCase("add")) {
或者您可以使用 Yoda conditions(根据链接维基百科文章中的 优势 它还可以解决某些类型的不安全问题 null
行为 它们可以描述为将表达式的常量部分放在条件语句的左侧)
if ("add".equalsIgnoreCase(called_from)) { // <-- safe if called_from is null
异常是因为这条语句,
called_from.equalsIgnoreCase("add")
前面的说法好像是
String called_from = getIntent().getStringExtra("called");
返回空引用。
您可以检查启动此 activity 的意图是否包含这样的密钥 "called"。
我在运行时遇到这个错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
堆栈跟踪:
01-12 03:44:54.270: E/AndroidRuntime(1437): FATAL EXCEPTION: main
01-12 03:44:54.270: E/AndroidRuntime(1437): Process: home.saket, PID: 1437
01-12 03:44:54.270: E/AndroidRuntime(1437): java.lang.RuntimeException: Unable to start activity ComponentInfo{home.saket/home.saket.addmember.Add_Update_User}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.access0(ActivityThread.java:144)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.os.Handler.dispatchMessage(Handler.java:102)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.os.Looper.loop(Looper.java:135)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-12 03:44:54.270: E/AndroidRuntime(1437): at java.lang.reflect.Method.invoke(Native Method)
01-12 03:44:54.270: E/AndroidRuntime(1437): at java.lang.reflect.Method.invoke(Method.java:372)
01-12 03:44:54.270: E/AndroidRuntime(1437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-12 03:44:54.270: E/AndroidRuntime(1437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-12 03:44:54.270: E/AndroidRuntime(1437): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
01-12 03:44:54.270: E/AndroidRuntime(1437): at home.saket.addmember.Add_Update_User.onCreate(Add_Update_User.java:38)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.Activity.performCreate(Activity.java:5933)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-12 03:44:54.270: E/AndroidRuntime(1437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-12 03:44:54.270: E/AndroidRuntime(1437): ... 10 more
01-12 03:44:54.272: W/ActivityManager(472): Force finishing activity home.saket/.addmember.Add_Update_User
01-12 03:44:54.273: E/ActivityManager(472): Invalid thumbnail dimensions: 384x384
下面贴出代码并指出错误行。
Add_Update_User.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_update_screen);
// set screen
Set_Add_Update_Screen();
// set visibility of view as per calling activity
String called_from = getIntent().getStringExtra("called");
if (called_from.equalsIgnoreCase("add")) { --->38th error line
add_view.setVisibility(View.VISIBLE);
update_view.setVisibility(View.GONE);
} else {
update_view.setVisibility(View.VISIBLE);
add_view.setVisibility(View.GONE);
USER_ID = Integer.parseInt(getIntent().getStringExtra("USER_ID"));
Contact c = dbHandler.Get_Contact(USER_ID);
add_name.setText(c.getName());
add_mobile.setText(c.getPhoneNumber());
add_email.setText(c.getEmail());
// dbHandler.close();
}
}
这是错误行:
if (called_from.equalsIgnoreCase("add")) { --->38th error line
这意味着 called_from
为空。在使用之前简单检查它是否为空:
String called_from = getIntent().getStringExtra("called");
if (called_from != null && called_from.equalsIgnoreCase("add")) {
// do whatever
} else {
// do whatever
}
这样,如果 called_from
为空,它将执行条件的 else
部分。
called_from
必须是 null
。添加针对该条件的测试,如
if (called_from != null && called_from.equalsIgnoreCase("add")) {
或者您可以使用 Yoda conditions(根据链接维基百科文章中的 优势 它还可以解决某些类型的不安全问题 null
行为 它们可以描述为将表达式的常量部分放在条件语句的左侧)
if ("add".equalsIgnoreCase(called_from)) { // <-- safe if called_from is null
异常是因为这条语句,
called_from.equalsIgnoreCase("add")
前面的说法好像是
String called_from = getIntent().getStringExtra("called");
返回空引用。
您可以检查启动此 activity 的意图是否包含这样的密钥 "called"。