简体   繁体   中英

InputStream Always returns nullpointerexception

    SentenceModel model = null;
    InputStream modelIn = null;

    AssetManager assManager = context.getAssets();

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

    //initializes sentence detector
    SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);

//Stacktrace 
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err: java.io.FileNotFoundException: en-sent.bin
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at android.content.res.AssetManager.openAsset(Native Method)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:313)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:287)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at com.gigstudios.newssummary.WordCounter.makeSentences(WordCounter.java:35)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at com.gigstudios.newssummary.Article.<init>(Article.java:19)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at com.gigstudios.newssummary.ArticleReceiver.receiveNewsArticles(ArticleReceiver.java:87)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at com.gigstudios.newssummary.ArticleReceiver.<init>(ArticleReceiver.java:37)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at com.gigstudios.newssummary.MainActivity.fetchNews(MainActivity.java:114)
08-03 14:25:23.842 27210-27210/com.gigstudios.newssummary W/System.err:     at com.gigstudios.newssummary.MainActivity$1.run(MainActivity.java:52)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at android.os.Handler.handleCallback(Handler.java:746)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at android.os.Looper.loop(Looper.java:148)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-03 14:25:23.843 27210-27210/com.gigstudios.newssummary D/AndroidRuntime: Shutting down VM
08-03 14:25:23.844 27210-27210/com.gigstudios.newssummary E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.gigstudios.newssummary, PID: 27210
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'opennlp.tools.sentdetect.SentenceDetectorFactory opennlp.tools.sentdetect.SentenceModel.getFactory()' on a null object reference
                                                                                at opennlp.tools.sentdetect.SentenceDetectorME.<init>(SentenceDetectorME.java:86)
                                                                                at com.gigstudios.newssummary.WordCounter.makeSentences(WordCounter.java:50)
                                                                                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)

I have been trying to fix this issue for hours and have gotten nowhere. In the above code sample model is always null and the en-sent.bin file doesn't seem to be read into the modelIn object. My bin file is in an assets folder currently. Any help is greatly appreciated.

I found an easy solution! I put the bin file in the "raw" directory and...

Instead of:

        modelIn = assManager.open("en-sent.bin");
        model = new SentenceModel(modelIn);

Use:

        modelIn = context.getResources().openRawResource(R.raw.en_sent);
        model = new SentenceModel(modelIn);

Works perfectly!

You need to:

    BufferedReader reader = null;
StringBuilder sbuilder = new StringBuilder();
    try {
        reader = new BufferedReader(
            new InputStreamReader(getAssets().open("en-sent.bin")));

        // read
        String mLine; 
        while ((mLine = buffer.readLine()) != null) {
                    sbuilder.append(mLine);
        }
    } catch (IOException e) {
//log
    } finally {
        if (reader != null) {
             try {
                 reader.close();
             } catch (IOException e) {
 //log
             }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM