我想計算從通話記錄中某一時期的每個聯繫人的通話總時長,但我還是堅持了這個NullPointerException異常在我while循環:的NullPointerException在while循環
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
if(c.moveToFirst()){
int[] duration = new int[c.getCount()];
String[] name = new String[c.getCount()];
do{
boolean w = false;
int y=0;
do{
if(name[y].equals("")){
name[y]=c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));
duration[y]+=Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.DURATION)));
w = true;
}else if(name[y].equals(c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)))){
duration[y]+=Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.DURATION)));
w = true;
}
y++;
}while(w);
}
如果statment在錯誤accurs在if(name[y].equals(""))
遊標投影螞蟻選擇似乎工作正常。
Here`s我的堆棧跟蹤:
10-22 07:36:18.429: E/AndroidRuntime(27320): FATAL EXCEPTION: main
10-22 07:36:18.429: E/AndroidRuntime(27320): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dailyreports.ainius/com.dailyreports.ainius.ReportView}: java.lang.NullPointerException
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.os.Looper.loop(Looper.java:123)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.main(ActivityThread.java:3729)
10-22 07:36:18.429: E/AndroidRuntime(27320): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 07:36:18.429: E/AndroidRuntime(27320): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
10-22 07:36:18.429: E/AndroidRuntime(27320): at dalvik.system.NativeStart.main(Native Method)
10-22 07:36:18.429: E/AndroidRuntime(27320): Caused by: java.lang.NullPointerException
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.dailyreports.ainius.DataProvider.getTotalCallDuration(DataProvider.java:1359)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.dailyreports.ainius.DataProvider.getReportData(DataProvider.java:109)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.dailyreports.ainius.ReportView.onCreate(ReportView.java:139)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-22 07:36:18.429: E/AndroidRuntime(27320): ... 11 more
進出口確保其容易的,但我只是不能想出辦法來的。
你檢查你的c.getCount()值?我想它會返回零。 – manman
在這種情況下會不會拋出arrayIndexOutOfBoundsException?無論如何,我檢查了它並沒有歸零。 – Ainius
是的,你是對的。 java中字符串類型的默認值爲null,因爲您尚未初始化數組成員,所以它們都是null,因此您要調用null.equals(「」)。 – manman