opennlp.tools.postag.POSTaggerME.train()上的线程“ main”中的异常java.lang.NullPointerException

有同样的问题!我得到 InputSteram = null ,我使用了IntelliJ IDEA,OpenNLP 1.9.1。在Ubuntu 18.04上

    public void makeDataTrainingModel() {
    model = null;
    System.out.println("POS model started");
    //InputStream dataIn = null;
    InputStreamFactory dataIn = null;
    try {
        dataIn = new InputStreamFactory() {
            public InputStream createInputStream() throws IOException {
                return NLPClassifier.class.getResourceAsStream("/home/int/src          
    /main/resources/en-pos.txt");
            }
        };
        //I get null pointer here in dataIn
        ObjectStream<String> linestream = new PlainTextByLinestream((InputStreamFactory),"UTF-8");
        ObjectStream<POSSample> sampleStream = new WordTagSampleStream(linestream);
     //This train part IS NOT WORK ?
        model = POSTaggerME.train("en",sampleStream,TrainingParameters.defaultParams(),null);
    } catch (IOException e) {
        // Failed to read or parse training data,training failed
        e.printStackTrace();
    } finally {
        if (dataIn != null) {
            //                    dataIn.close();
            System.out.println("InputStreamFactory was not created!");
        }
    }
    System.out.println("POS model done...");
    System.out.println("Success generate model...");
    //write Data model
    OutputStream modelOut = null;
    try {
        String currentDir = new File("").getabsolutePath();
        modelOut = new BufferedOutputStream(new FileOutputStream(currentDir + "//src//main//resources//example-bad-model.dat"));


        model.serialize(modelOut);
    } catch (IOException e) {
        // Failed to save model
        e.printStackTrace();
    } finally {
        if (modelOut != null) {
            try {
                modelOut.close();
            } catch (IOException e) {
                // Failed to correctly save model.
                // Written model might be invalid.
                e.printStackTrace();
            }
        }
    }
    System.out.println("Model generated and treated successfully...");
}

我在inputStream中得到了空指针,并且出现了错误... 未创建InputStreamFactory!

    Exception in thread "main" java.lang.NullPointerException
    at java.io.reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
    at     
    opennlp.tools.util.PlainTextByLinestream.reset(PlainTextByLinestream.java:57)
    at opennlp.tools.util.PlainTextByLinestream.<init>   
    (PlainTextByLinestream.java:48)
    at opennlp.tools.util.PlainTextByLinestream.<init>    
   (PlainTextByLinestream.java:39)

   at NLPClassifier.makeDataTrainingModel(NLPClassifier.java:98)
   at NlpProductClassifier.main(NlpProductClassifier.java:39)


数据如下:
profit_profit shell_environment 384912_CD bucks_currency
工资_利润finger_body 913964_CD美元_货币
profit_profit faith_law 3726_CD rur_currency
gain_profit game_entertainment 897444_CD dollar_currency
got_buy gift_jewelery 534841_CD rub_currency

为什么线程无法打开并引发异常?

markrolon 回答:opennlp.tools.postag.POSTaggerME.train()上的线程“ main”中的异常java.lang.NullPointerException

如果getResourceAsStream返回null,则表示找不到资源。

您应该检查null并执行其他操作,例如抛出异常(在这种情况下,抛出异常(IOExceptionFileNotFoundException,因为IOException和子类被throws声明)-您不应让其将null传递给其余代码。

NLPClassifier.class.getResourceAsStream("/home/int/src/main/resources/en-pos.txt")不起作用,因为资源与Java包具有相同的结构,只是用斜杠代替了点。它不是文件系统中的路径。

将其更改为:getResourceAsStream("/en-pos.txt")(因为您的文件位于包层次结构的根目录中)

,

我按照Erwin Bolwidt的说法更改了代码,

    /** I commented this part
    return NLPClassifier.class.getResourceAsStream("/home/interceptor/src/main/resources/en-pos.txt");
    */
    /**
     Add this location of my resoures:
     /Project/src/main/resources
    */
    return getClass().getClassLoader().getResourceAsStream("en-pos.txt");

此后,我发现Apache OpenNLP: java.io.FileInputStream cannot be cast to opennlp.tools.util.InputStreamFactory遇到了类似的问题,但是使用了其他方法。 @schrieveslaach说

  

您需要一个InputStreamFactory实例,该实例将检索您的InputStream。此外, TokenNameFinderFactory不能为空!,例如 posFactory-不能为空!

    /**
     *  Factory must not be a null. Add posModel.getFactory()
     *  model = POSTaggerME.train("en",sampleStream,TrainingParameters.defaultParams(),null);
     */
     model = POSTaggerME.train("en",posModel.getFactory());

回购https://github.com/AlexTitovWork/NLPclassifier中项目的完整代码

本文链接:https://www.f2er.com/3170085.html

大家都在问