通話ログから特定の期間のすべての連絡先の合計通話時間を計算したいのですが、whileループでこのNullPointerExceptionを使用し続けました。
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);
}
エラーは、if(name[y].equals(""))
カーソルの投影と選択が正常に機能しているように見える場合に発生します。
これが私のスタックトレースです:
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
簡単なことだと思いますが、私にはわかりません。