2012-04-16 2 views
0

다음 줄에서 NullPointerException이 발생합니다.
가끔은 (특히 첫 번째 인스턴스에서) 때로는 이해하지 못하는 경우가 있습니다. 그래서 나는 이것을 한 번에 해결하려고 노력하고 있습니다.
이미지 데이터베이스에서 파일 경로를 검색 한 다음 비트 맵 디코딩을 사용합니다. 나는 데이터베이스에 값이 있다고 확신한다.이미지의 ArrayList에있는 NullPointerException

감사합니다.

 ImageDB imagedb = new ImageDB(this);  
     imagedb.open(); 

     ArrayList<String> image_list = new ArrayList<String>(); 
     image_list.clear(); 

      if(imagedb.open() != null){ 
      Cursor tagcursor = imagedb.retrieveTag(tag); 

      if(tagcursor.moveToFirst()){ 
       do{ 
        image_list.add(tagcursor.getString(tagcursor.getColumnIndex(tag))); 

       }  
       while(tagcursor.moveToNext()); 

       } 

      tagcursor.close(); 
      imagedb.close(); 
      } 


       if(image_list != null && !image_list.isEmpty()){ 
      Collections.shuffle(image_list); 

    if(image_list.get(0).toString() != null){ // nullpointerexception here or below if I comment here 

       imagepath = image_list.get(0).toString(); 
      } 

     } 

이 스택 추적입니다,하지만 그냥 경고로, 프로그램은 여전히 ​​실행 : 당신은 목록을 셔플하는

04-16 19:03:31.603: W/System.err(1355): java.lang.NullPointerException 
04-16 19:03:31.613: W/System.err(1355):  at com.MucaaApps.imageUpdate.ImageService.setTag(ImageService.java:422) 
04-16 19:03:31.613: W/System.err(1355):  at com.MucaaApps.ImageUpdate.imageService.setImageIcon(ImageService.java:377) 
04-16 19:03:31.623: W/System.err(1355):  at com.MucaaApps.ImageUpdate.ImageService.onStart(ImageService.java:237) 
04-16 19:03:31.623: W/System.err(1355):  at android.app.Service.onStartCommand(Service.java:306) 
04-16 19:03:21.552: W/System.err(1355):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2873) 
04-16 19:03:21.552: W/System.err(1355):  at android.app.ActivityThread.access$3500(ActivityThread.java:119) 
04-16 19:03:21.552: W/System.err(1355):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926) 
04-16 19:03:21.552: W/System.err(1355):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-16 19:03:21.552: W/System.err(1355):  at android.os.Looper.loop(Looper.java:123) 
04-16 19:03:21.552: W/System.err(1355):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
04-16 19:03:21.563: W/System.err(1355):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-16 19:03:21.563: W/System.err(1355):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-16 19:03:21.573: W/System.err(1355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-16 19:03:21.573: W/System.err(1355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-16 19:03:21.582: W/System.err(1355):  at dalvik.system.NativeStart.main(Native Method) 
+1

당신이 스택 트레이스를 게시 할 수 있습니까? 귀하의 의견에서 그것은'toString()'NullPointerException' 던져 야지 image_list의 인덱스 0에 아무 것도없는 것 같습니다 – Nicholas

+0

@ 니콜라스, 나는 스택 추적을 추가했습니다. 그것은 경고로 온다. 내가 말한 것처럼, 그것은 내가 주석 처리 한 줄에서 NullPointerException을 보여준다. – irobotxxx

+0

1)'imagedb.open()'이 null이 아니고, 2)'tagcursor'가 null이 아니거나 값을 가지고 있는지 확인하기 위해 break 라인이나 출력 문을 넣으십시오. 3)'image_list'는 값을 채운 후에 값을가집니다. – Nicholas

답변

1

간단한 답변을 확인할 수 있습니다. ArrayList에 null 값을 추가했습니다.

그렇게 문제를 해결할 수 있습니다

ImageDB imagedb = new ImageDB(this); 
     imagedb.open(); 
     ArrayList<String> image_list = new ArrayList<String>(); 
     image_list.clear(); 

     if (imagedb.open() != null) { 
      Cursor tagcursor = imagedb.retrieveTag(tag); 

      if (tagcursor.moveToFirst()) { 
         final int index = tagcursor.getColumnIndex(tag) 
       do { 
        final String val = tagcursor.getString(index); 
        if (val!=null){ 
         image_list.add(val); 
        } 

       } while (tagcursor.moveToNext()); 

      } 

      tagcursor.close(); 
      imagedb.close(); 
     } 

     if (image_list != null && !image_list.isEmpty()) { 
      Collections.shuffle(image_list); 

      if (image_list.get(0).toString() != null) { // nullpointerexception 
                 // here or below if i 
                 // comment here 

       imagepath = image_list.get(0).toString(); 
      } 

     } 
+0

나는 이것에 동의한다. 그러나 실제 문제는 더 어딘가에있다. 유청은 처음부터 데이터베이스에 null 값을 넣었습니까? 그게 다른 문제의 징후 일 수 있으므로 그 문제를 해결하고 싶을 수도 있습니다 – Kyle

+0

왜 목록에 null을 넣지 않도록 코드를 변경했는지 – Renard

+0

@Kyle .. 나는 그것이 완전한 것인지를 살펴볼 것입니다. 케이스하지만 나는 이전에 null을 확인했다고 생각합니다. – irobotxxx

0

. 그래서 내 생각 엔 filepath 중 하나가 null이고 null 포인터로 연결되는 첫 번째 위치로 무작위로 섞여 있습니다.

현재

String filePath = tagcursor.getColumnIndex(tag); 
if (filepath == null || filepath.isEmpty()) [ 
    //get additional info from cursor ..row id etc to debug 
} else { 
    image_list.add(filepath); 
} 
+0

@Anirudh .. thanks – irobotxxx