NullPointer exception while reading epub file from assets in android
android, epub
Solution
Null Pointer exception is basically due to partial loading of classes like sl4j and epublib ..For this you have to import dependencies like slf4-api and slf4-simple for slf4-android into libs folder and make this into build path.
Problem
I am accesing epub file from assets folder to read the epub content.I followed siegmann step by step. It is throwing nulpointer exception at ``` Book book = (new EpubReader()).readEpub(epubInputStream); ``` My code snippet is ``` public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AssetManager assetManager = getAssets(); System.err.println("assetManager>>"+assetManager); try { // find InputStream for book InputStream epubInputStream = assetManager.open("ww.epub"); // Load Book from inputStream Book book = (new EpubReader()).readEpub(epubInputStream); // Log the book's authors Log.i("epublib", "author(s): " + book.getMetadata().getAuthors()); // Log the book's title Log.i("epublib", "title: " + book.getTitle()); // Log the book's coverimage property Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream()); Log.i("epublib", "Coverimage is " + coverImage.getWidth() + " by " + coverImage.getHeight() + " pixels"); // Log the tale of contents logTableOfContents(book.getTableOfContents().getTocReferences(), 0); } catch (IOException e) { Log.e("epublib", e.getMessage()); } } private void logTableOfContents(List<TOCReference> tocReferences, int depth) { if (tocReferences == null) { return; } for (TOCReference tocReference : tocReferences) { StringBuilder tocString = new StringBuilder(); for (int i = 0; i < depth; i++) { tocString.append("\t"); } tocString.append(tocReference.getTitle()); Log.i("epublib", tocString.toString()); logTableOfContents(tocReference.getChildren(), depth + 1); } } } ``` `ww.epub` is the file i kept simply in assets folder.Could anyone tell me how can i solve this nullpointer exception? Logcat shows ``` 02-04 13:22:57.189: W/System.err(22968): 1915 [main] ERROR nl.siegmann.epublib.epub.NCXDocument - null 02-04 13:22:57.189: W/System.err(22968): java.lang.NullPointerException 02-04 13:22:57.189: W/System.err(22968): at nl.siegmann.epublib.epub.NCXDocument.read(NCXDocument.java:92) 02-04 13:22:57.189: W/System.err(22968): at nl.siegmann.epublib.epub.EpubReader.processNcxResource(EpubReader.java:113) 02-04 13:22:57.189: W/System.err(22968): at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:99) 02-04 13:22:57.189: W/System.err(22968): at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:53) 02-04 13:22:57.189: W/System.err(22968): at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:37) 02-04 13:22:57.189: W/System.err(22968): at com.example.epub.MainActivity.onCreate(MainActivity.java:36) 02-04 13:22:57.189: W/System.err(22968): at android.app.Activity.performCreate(Activity.java:5104) 02-04 13:22:57.189: W/System.err(22968): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-04 13:22:57.189: W/System.err(22968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 02-04 13:22:57.189: W/System.err(22968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-04 13:22:57.189: W/System.err(22968): at android.app.ActivityThread.access$600(ActivityThread.java:141) 02-04 13:22:57.189: W/System.err(22968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 02-04 13:22:57.189: W/System.err(22968): at android.os.Handler.dispatchMessage(Handler.java:99) 02-04 13:22:57.189: W/System.err(22968): at android.os.Looper.loop(Looper.java:137) 02-04 13:22:57.189: W/System.err(22968): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-04 13:22:57.189: W/System.err(22968): at java.lang.reflect.Method.invokeNative(Native Method) 02-04 13:22:57.189: W/System.err(22968): at java.lang.reflect.Method.invoke(Method.java:511) 02-04 13:22:57.189: W/System.err(22968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-04 13:22:57.189: W/System.err(22968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-04 13:22:57.189: W/System.err(22968): at dalvik.system.NativeStart.main(Native Method) 02-04 13:22:57.189: I/epublib(22968): author(s): [Melville, Herman] 02-04 13:22:57.189: I/epublib(22968): title: Moby-Dick 02-04 13:22:57.189: D/AndroidRuntime(22968): Shutting down VM 02-04 13:22:57.189: W/dalvikvm(22968): threadid=1: thread exiting with uncaught exception (group=0x41e99930) 02-04 13:22:57.189: E/AndroidRuntime(22968): FATAL EXCEPTION: main 02-04 13:22:57.189: E/AndroidRuntime(22968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.epub/com.example.epub.MainActivity}: java.lang.NullPointerException 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.ActivityThread.access$600(ActivityThread.java:141) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.os.Handler.dispatchMessage(Handler.java:99) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.os.Looper.loop(Looper.java:137) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-04 13:22:57.189: E/AndroidRuntime(22968): at java.lang.reflect.Method.invokeNative(Native Method) 02-04 13:22:57.189: E/AndroidRuntime(22968): at java.lang.reflect.Method.invoke(Method.java:511) 02-04 13:22:57.189: E/AndroidRuntime(22968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-04 13:22:57.189: E/AndroidRuntime(22968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-04 13:22:57.189: E/AndroidRuntime(22968): at dalvik.system.NativeStart.main(Native Method) 02-04 13:22:57.189: E/AndroidRuntime(22968): Caused by: java.lang.NullPointerException 02-04 13:22:57.189: E/AndroidRuntime(22968): at com.example.epub.MainActivity.onCreate(MainActivity.java:45) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.Activity.performCreate(Activity.java:5104) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-04 13:22:57.189: E/AndroidRuntime(22968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 02-04 13:22:57.189: E/AndroidRuntime(22968): ... 11 more ```