Tdonut Tdonut - 4 months ago 10
Android Question

Nullpointer Exception from change from Java to Android

We made a Java application and are converting it to Android. We need to read a bin file called "en-sent.bin" but in Android there is a nullpointerexception. I have put the bin file inside of the assets folder of the project and have messed with AssetManagers but nothing has worked. ModelIn and model variablse are always null, which make the sentenceDetector return a nullpointerexception. I am using an Apache sentence detector library that needs this bin file to work.

Here is our original code (from Java application):

SentenceModel model = null;
InputStream modelIn = null;
try {
modelIn = new FileInputStream("en-sent.bin");
model = new SentenceModel(modelIn);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (modelIn != null) {
try {
modelIn.close();
}
catch (IOException e) {
}
}
}

//initializes sentence detector
SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
String[] sentencesTemp = sentenceDetector.sentDetect(article);


Stack trace:

08-04 10:11:09.755 10247-10247/com.gigstudios.newssummary W/System.err: java.io.FileNotFoundException: en-sent.bin: open failed: ENOENT (No such file or directory)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:103)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.gigstudios.newssummary.WordCounter.makeSentences(WordCounter.java:52)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.gigstudios.newssummary.Article.<init>(Article.java:19)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.gigstudios.newssummary.ArticleReceiver.receiveNewsArticles(ArticleReceiver.java:87)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.gigstudios.newssummary.ArticleReceiver.<init>(ArticleReceiver.java:37)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.gigstudios.newssummary.MainActivity.fetchNews(MainActivity.java:114)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.gigstudios.newssummary.MainActivity$1.run(MainActivity.java:52)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at android.os.Handler.handleCallback(Handler.java:746)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at android.os.Looper.loop(Looper.java:148)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5443)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at libcore.io.Posix.open(Native Method)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
08-04 10:11:09.756 10247-10247/com.gigstudios.newssummary W/System.err: ... 15 more
08-04 10:11:09.757 10247-10247/com.gigstudios.newssummary D/AndroidRuntime: Shutting down VM
08-04 10:11:09.757 10247-10247/com.gigstudios.newssummary E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gigstudios.newssummary, PID: 10247
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] opennlp.tools.sentdetect.SentenceDetectorME.sentDetect(java.lang.String)' on a null object reference
at com.gigstudios.newssummary.WordCounter.makeSentences(WordCounter.java:56)
at com.gigstudios.newssummary.Article.<init>(Article.java:19)
at com.gigstudios.newssummary.ArticleReceiver.receiveNewsArticles(ArticleReceiver.java:87)
at com.gigstudios.newssummary.ArticleReceiver.<init>(ArticleReceiver.java:37)
at com.gigstudios.newssummary.MainActivity.fetchNews(MainActivity.java:114)
at com.gigstudios.newssummary.MainActivity$1.run(MainActivity.java:52)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Answer

You should have en-sent.bin in /res/raw/en_sent.bin (Note you will have to change the dash to an underscore.) Then replace

        modelIn = new FileInputStream("en-sent.bin");

with

        modelIn = getResources().openRawResource(R.raw.en_sent);

This is assuming you are calling from an Activity. If you are calling from a fragment, you need to get a Context first, i.e. getActivity().getResources()...