本文介绍了opennlp.tools.postag.POSTaggerME.train()java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are same problem! I get InputSteram = null, I used IntelliJ IDEA, OpenNLP 1.9.1. on 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) {

            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!

I get null pointer in inputStream and Error...InputStreamFactory was not created!

    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 usd_currency
profit_profit faith_law 3726_CD rur_currency
profit_profit游戏_娱乐897444_CD dollar_currency
got_buy gift_jewelery 534841_CD rub_currency

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


Data looks like this:
profit_profit shell_environment 384912_CD bucks_currency
salary_profit finger_body 913964_CD usd_currency
profit_profit faith_law 3726_CD rur_currency
profit_profit game_entertainment 897444_CD dollar_currency
got_buy gift_jewelery 534841_CD rub_currency

**Why the thread does not open and it throw an exception?**

推荐答案

如果getResourceAsStream返回null,则表示未找到资源.

If getResourceAsStream returns null, it means that the resource wasn't found.

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

You should check for null and do something else, such as throwing an exception (IOException or FileNotFoundException in this case, since IOException and subclasses are allowed by the throws declaration) - you shouldn't let it pass the null to the rest of your code.

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

NLPClassifier.class.getResourceAsStream("/home/int/src/main/resources/en-pos.txt") won't work, because resources have the same structure as Java packages, except that dots are replaced with slashes. It's not a path in the file system.

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

Change it to: getResourceAsStream("/en-pos.txt") (because your file is at the root of the package hierarchy)

这篇关于opennlp.tools.postag.POSTaggerME.train()java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 19:37