有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

线程“main”java中的eclipse异常。lang.IllegalArgumentException:in不能为null

我尝试使用OpenNLP库来使用它的sentencedetector,并尝试编写以下代码,但我得到了一个异常,该异常与发送的en的地址有关。bin文件,但我不知道如何处理此文件

import java.io.*;
import java.net.URL;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
public class SentenceDetect 
{

       private SentenceDetectorME sentenceDetector;

        public void init()
        {
            /** Load and initialize the sentence detection model */

            InputStream modelIn = null;
            SentenceModel model = null;

            try {

                modelIn = SentenceDetect.class.getClassLoader().getResourceAsStream("Tokenizer/models/en-sent.bin");
                model = new SentenceModel(modelIn); //*<- line 36*
                }
            catch (IOException e) 
                {
                  e.printStackTrace();
                }
            finally {
                  if (modelIn != null) {
                        try {
                          modelIn.close();
                        }
                        catch (IOException e) {}
                      }
                }

            sentenceDetector = new SentenceDetectorME(model);

        }

        public String[] getSentences(String longSentence)
        {
            return sentenceDetector.sentDetect(longSentence);
        }

}

主要类别:

public static void main(String[] args)
    {

       SentenceDetect d = new SentenceDetect();


       d.init();   ///*<- line 10*

       String[] s = d.getSentences("This is sentence #1. This is Sentence #2");

       System.out.println( s[0] );  // Should be the first sentence

       System.out.println( s[1] );  // Should be the second sentence

    }

下图显示了我的项目的层次结构(很抱歉我使用Ubuntu的图片,但我不知道这里是否有使用打印屏幕按钮):

enter image description here

整个错误是:

`Exception in thread "main" java.lang.IllegalArgumentException: in must not be null!
at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:179)
at opennlp.tools.sentdetect.SentenceModel.<init>(SentenceModel.java:95)
at SentenceDetect.init(SentenceDetect.java:36)
at Main.main(Main.java:10)`

我尝试了这些路径,但得到了相同的错误:

  • /标记器/模型/en已发送。垃圾箱
  • /模型/电子发送。垃圾箱
  • 模型/电子发送。垃圾箱
  • /主页/suri/workspace/2/Tokenizer/models/en已发送。垃圾箱

共 (3) 个答案

  1. # 1 楼答案

    改变

      .getResourceAsStream("Tokenizer/models/en-sent.bin");
    

     .getResourceAsStream("models/en-sent.bin");
    

    路径中有“标记器”,它是项目的名称,与此无关,因此您只需删除该位即可:)

  2. # 2 楼答案

    你需要改变你的人生道路

     .getResourceAsStream("en-sent.bin");
    

    因为getResourceAsStream读取了一个包,并且这些文件(.bin)位于源文件夹中

  3. # 3 楼答案

    由于您使用的是getClassLoader().getResourceAsStream(),因此文件必须位于类路径中。右键单击eclipse“构建路径”中的“模型”文件夹->;“用作源文件夹”。然后确保路径与文件夹结构匹配。如果您保留照片中的内容,那将是“models/en sent.bin”

    如果您希望这些.bin文件通常位于构建的.jar文件之外,那么应该使用构造一个FileInputStream文件,它可以采用绝对文件系统路径