androidcommonsware

EmPubLite Tutorial 12-crashes


I have been going through Tutorial 12 of Busy Coders Guide version 5.1.

When I run the emulator the app keeps crashing during startup. I am not clear on what to look for in my logcat file. Can you please help me understand what I need to look for. Thanks.

I searched and didn't see any other posts regarding Tutorial 12. If there is can you please point me in the right direction. Thanks.

  10-01 16:01:11.921: D/dalvikvm(1397): GC_FOR_ALLOC freed 103K, 8%
 free 2808K/3028K, paused 37ms, total 39ms 10-01 16:01:11.921:
 I/dalvikvm-heap(1397): Grow heap (frag case) to 3.460MB for
 635812-byte allocation 10-01 16:01:12.041: D/dalvikvm(1397):
 GC_FOR_ALLOC freed 3K, 7% free 3426K/3652K, paused 114ms, total 114ms
 10-01 16:01:12.291: D/gralloc_goldfish(1397): Emulator without GPU
 emulation detected. 10-01 16:01:12.491: D/AndroidRuntime(1397):
 Shutting down VM 10-01 16:01:12.511: W/dalvikvm(1397): threadid=1:
 thread exiting with uncaught exception (group=0x41465700) 10-01
 16:01:12.581: E/AndroidRuntime(1397): FATAL EXCEPTION: main 10-01
 16:01:12.581: E/AndroidRuntime(1397): java.lang.NullPointerException
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.commonsware.empublite.BookContents.getChapterCount(BookContents.java:16)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.commonsware.empublite.ContentsAdapter.getCount(ContentsAdapter.java:26)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 android.support.v4.view.ViewPager.setAdapter(ViewPager.java:434) 10-01
 16:01:12.581: E/AndroidRuntime(1397):  at
 com.commonsware.empublite.EmPubLiteActivity.setupPager(EmPubLiteActivity.java:70)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.commonsware.empublite.ModelFragment.deliverModel(ModelFragment.java:29)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.commonsware.empublite.ModelFragment.access$2(ModelFragment.java:27)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.commonsware.empublite.ModelFragment$ContentsLoadTask.onPostExecute(ModelFragment.java:82)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.commonsware.empublite.ModelFragment$ContentsLoadTask.onPostExecute(ModelFragment.java:1)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 android.os.AsyncTask.finish(AsyncTask.java:631) 10-01 16:01:12.581:
 E/AndroidRuntime(1397):    at
 android.os.AsyncTask.access$600(AsyncTask.java:177) 10-01
 16:01:12.581: E/AndroidRuntime(1397):  at
 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 android.os.Handler.dispatchMessage(Handler.java:99) 10-01
 16:01:12.581: E/AndroidRuntime(1397):  at
 android.os.Looper.loop(Looper.java:137) 10-01 16:01:12.581:
 E/AndroidRuntime(1397):    at
 android.app.ActivityThread.main(ActivityThread.java:5103) 10-01
 16:01:12.581: E/AndroidRuntime(1397):  at
 java.lang.reflect.Method.invokeNative(Native Method) 10-01
 16:01:12.581: E/AndroidRuntime(1397):  at
 java.lang.reflect.Method.invoke(Method.java:525) 10-01 16:01:12.581:
 E/AndroidRuntime(1397):    at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 10-01 16:01:12.581: E/AndroidRuntime(1397):    at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-01
 16:01:12.581: E/AndroidRuntime(1397):  at
 dalvik.system.NativeStart.main(Native Method)

Solution

  • I am not clear on what to look for in my logcat file

    That would be the exception and line where you are crashing:

    10-01 16:01:12.581: E/AndroidRuntime(1397): FATAL EXCEPTION: main
    10-01 16:01:12.581: E/AndroidRuntime(1397): java.lang.NullPointerException
    10-01 16:01:12.581: E/AndroidRuntime(1397): at com.commonsware.empublite.BookContents.getChapterCount(BookContents.java:16) 
    

    Hence, you are crashing in line 16 of BookContents.java, in the getChapterCount() method of the BookContents class.

    That method should look like:

      int getChapterCount() {
        return(chapters.length());
      }
    

    That indicates that either:

    1. You are missing the chapters=raw.optJSONArray("chapters"); line in the constructor, or

    2. Your JSON is missing the chapters array

    Here is a working implementation of Tutorial 12, including the BookContents class.