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)
我一直试图解决这个问题几个小时而且无处可去。在上面的代码中,示例模型始终为null,并且似乎没有将en-sent.bin文件读入modelIn对象。我的bin文件当前位于assets文件夹中。非常感谢任何帮助。
答案 0 :(得分:0)
我找到了一个简单的解决方案!我将bin文件放在&#34; raw&#34;目录和......
而不是:
modelIn = assManager.open("en-sent.bin");
model = new SentenceModel(modelIn);
使用:
modelIn = context.getResources().openRawResource(R.raw.en_sent);
model = new SentenceModel(modelIn);
完美无缺!
答案 1 :(得分:-1)
你需要:
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
}
}
}