在将类引用传递给方法时,我得到java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
。
导致错误的行是:
if (!contactData.getContactNo().equals("")){
更多代码:
调用方法:
recContDBHelper.insertRecord(new ContactData("", number, date)); <--Exception here
调用方法:
public void insertRecord(ContactData contactData) {
String name = "";
if (!contactData.getContactNo().equals("")) { <--exception here
name = CommonServices.getContactDisplayNameByNumber(contactData.getContactNo(), context);
if (name.equalsIgnoreCase("")) {
name = contactData.getContactNo();
}
}
}
在上面的代码contactData.getContactNo()
中,返回类型为String
。
例外是:
04-12 18:21:40.153: E/AndroidRuntime(27924): FATAL EXCEPTION: main
04-12 18:21:40.153: E/AndroidRuntime(27924): Process: com.stackoverflow.ranjith.androidprojdel, PID: 27924
04-12 18:21:40.153: E/AndroidRuntime(27924): java.lang.RuntimeException: Unable to start receiver com.stackoverflow.ranjith.androidprojdel.PhoneCallreceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.app.ActivityThread.access$1700(ActivityThread.java:144)
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.os.Handler.dispatchMessage(Handler.java:102)
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.os.Looper.loop(Looper.java:135)
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-12 18:21:40.153: E/AndroidRuntime(27924): at java.lang.reflect.Method.invoke(Native Method)
04-12 18:21:40.153: E/AndroidRuntime(27924): at java.lang.reflect.Method.invoke(Method.java:372)
04-12 18:21:40.153: E/AndroidRuntime(27924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-12 18:21:40.153: E/AndroidRuntime(27924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-12 18:21:40.153: E/AndroidRuntime(27924): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
04-12 18:21:40.153: E/AndroidRuntime(27924): at com.stackoverflow.ranjith.androidprojdel.recentlyContacted.RecContDBHelper.insertRecord(RecContDBHelper.java:57)
04-12 18:21:40.153: E/AndroidRuntime(27924): at com.stackoverflow.ranjith.androidprojdel.PhoneCallreceiver.onReceive(PhoneCallreceiver.java:93)
04-12 18:21:40.153: E/AndroidRuntime(27924): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2579)
04-12 18:21:40.153: E/AndroidRuntime(27924): ... 9 more
ContactData
类:
public class ContactData {
private String contact_name;
private String ph_no;
private String time_call;
public ContactData(String contact_name, String ph_no, String time_call) {
this.contact_name = contact_name;
this.ph_no = ph_no;
this.time_call = time_call;
}
public ContactData(String contact_name, String ph_no) {
this.contact_name = contact_name;
this.ph_no = ph_no;
}
public String getContactNo() {
return this.ph_no;
}
}
答案 0 :(得分:3)
试试这个
if (contactData.getContactNo() != null && !contactDate.getContactNo().isEmpty()) {
// your code here
}
似乎contactData.getContactNo()
正在返回null
。
答案 1 :(得分:0)
不要尝试在实例上调用方法而不确保它不为null。 在您的情况下,contactData.getContactNo为null,当您尝试在其上调用equals方法时,将抛出NPE。 有两种方法可以避免它。第一个是将空字符串与contactData.getContactNo进行比较,否则确保contactData和contact.getContactNo不为空。
例如:
"".equals(contactData.getContactNo)
或
if(contactData != null && contactData.getContactNo != null)