gpt4 book ai didi

java - 从Java更改为Android的空指针异常

转载 作者:行者123 更新时间:2023-12-01 09:37:17 26 4
gpt4 key购买 nike

我们制作了一个 Java 应用程序并将其转换为 Android。我们需要读取一个名为“en-sent.bin”的bin文件,但在Android中存在空指针异常。我已将 bin 文件放入项目的 asset 文件夹中,并与 AssetManagers 混淆,但没有任何效果。 ModelIn 和模型变量始终为 null,这使得句子检测器返回空指针异常。我正在使用需要此 bin 文件才能工作的 Apache 句子检测器库。

这是我们的原始代码(来自 Java 应用程序):

    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);

堆栈跟踪:

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)

最佳答案

您应该在 /res/raw/en_sent.bin 中有 en-sent.bin (请注意,您必须将破折号更改为下划线。)然后替换

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

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

这是假设您从Activity调用。如果您从 fragment 调用,则需要首先获取 Context,即 getActivity().getResources()...

关于java - 从Java更改为Android的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38773797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com