將類引用傳遞給方法時,我得到了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;
}
}
怎麼會是空..我路過'新的聯絡人資料( 「」,數字,日期)'的方法。 – Psypher
是的......但是電話會返回什麼?它是否返回尚未初始化的變量的值?代碼說什麼? –
至少有兩種可能性:1.創建對象時number爲null。 2你的getter返回null – Blackbelt