繁体   English   中英

“线程“ main”中的异常java.Lang.NullPointerException” [重复]

[英]“Exception in thread ”main“ java.Lang.NullPointerException” [duplicate]

这个问题已经在这里有了答案:

我的目标是执行情感分析,主题提取,实体...当我在Thrift服务器启动时执行以下Java代码时...

     package com.diskoverorta.tamanager;

     import com.diskoverorta.entities.EntityManager;
   //import com.diskoverorta.lifesciences.LSInterface;
     import com.diskoverorta.osdep.StanfordNLP;
     import com.diskoverorta.utils.EntityUtils;
     import com.diskoverorta.vo.*;
     import com.google.common.base.Charsets;
     import com.google.common.io.Files;
     import com.google.gson.Gson;
     import com.google.gson.GsonBuilder;
     import com.diskoverorta.pyinterface.*;
   //import com.serendio.diskoverer.lifesciences.document.LifeScienceDocument;
     import java.io.File;
     import java.util.List;
     import java.util.Set;
     import java.util.TreeSet;
     import org.kohsuke.args4j.Argument;
     import org.kohsuke.args4j.CmdLineException;
     import org.kohsuke.args4j.CmdLineParser;
     import org.kohsuke.args4j.Option;
     import java.io.IOException;  
     import java.util.ArrayList;
     import java.nio.charset.Charset;

     public class TextManager
     {
@Option(name = "-analyze", aliases = {"-analyze"}, usage = "Accepted options for analyze : Entity, Sentiment, Topic, Keyword, All")
String analysis;

@Option(name = "-Text", aliases = {"-text"}, usage = "Extracts Text analytics components for given Text")
String text;

@Option(name = "-File", aliases = {"-file"}, usage = "Extracts Text analytics components for given File")
String fileName;

@Argument
List<String> arguments = new ArrayList<String>();

static StanfordNLP nlpStanford = null;
static ThriftClient pyClient = null;

DocumentObject doc = null;

public TextManager()
{
    if(nlpStanford==null)
    {
        nlpStanford = new StanfordNLP();
    }
    if(pyClient==null)
    {
        pyClient = new ThriftClient("localhost",8002);
    }
}

public DocSentObject tagTextAnalyticsComponents(String sDoc,TAConfig config)
{
    DocSentObject doc = new DocSentObject();
    List<String> sentList = nlpStanford.splitSentencesINDocument(sDoc);

    for(int i=0; i < sentList.size(); i++)
    {
        SentenceObject temp = new SentenceObject();
        temp.sentenceText = sentList.get(i);

        if(config.analysisConfig.get("Entity") == "TRUE")
            temp.entities = (new EntityManager().getSelectedEntitiesForSentence(temp.sentenceText,config.entityConfig));

        doc.docSentences.add(temp);
    }
    return doc;
}

public DocumentObject aggregateDocumentComponentsFromSentences(DocSentObject docSent)
{
    DocumentObject docObject = new DocumentObject();
    for (SentenceObject sentObj : docSent.docSentences)
    {
        docObject.entities.currency.addAll(sentObj.entities.currency);
        docObject.entities.date.addAll(sentObj.entities.date);
        docObject.entities.location.addAll(sentObj.entities.location);
        docObject.entities.organization.addAll(sentObj.entities.organization);
        docObject.entities.percent.addAll(sentObj.entities.percent);
        docObject.entities.person.addAll(sentObj.entities.person);
        docObject.entities.time.addAll(sentObj.entities.time);
    }
    docObject.entitiesMeta = EntityUtils.extractEntityMap(docObject.entities);
    return docObject;
}

public String tagTextAnalyticsComponentsINJSON(String sDoc,TAConfig config)
{
    DocSentObject doc = tagTextAnalyticsComponents(sDoc,config);
    DocumentObject docObject = aggregateDocumentComponentsFromSentences(doc);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonOutput = gson.toJson(docObject);
    return jsonOutput;
}

public String tagUniqueTextAnalyticsComponentsINJSON(String sDoc,TAConfig config)
{
    String jsonOutput = "";
    APIOutput apiOut = new APIOutput();

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    if(config.analysisConfig.get("Entity") == "TRUE")
    {
        DocSentObject doc = tagTextAnalyticsComponents(sDoc, config);
        DocumentObject docObject = aggregateDocumentComponentsFromSentences(doc);
        EntityAPISet apiSet = EntityUtils.getEntitySet(docObject);
        apiOut.entity_general = apiSet;
    }

   // if(config.analysisConfig.get("LSEntity") == "TRUE")
   //     apiOut.entity_lifesciences = gson.fromJson(LSInterface.getLSEntitiesinJSON(sDoc),LifeScienceDocument.class);
    if(config.analysisConfig.get("Topic") == "TRUE")
    {
        Set<String> topic_set = new TreeSet<String>();
        if (apiOut.text_information == null)
            apiOut.text_information = new TextInformation();
        if(pyClient != null)
        {

            Charset.forName("UTF-8").encode(sDoc);
            List<String> topics = pyClient.getTopics(sDoc);


            for (String topic : topics)
                topic_set.add(topic);
        }
        if (topic_set.isEmpty() == true)
        {
            topic_set.add("Topic analyzer not working, Start Thrift server at port 8002");
        }
        apiOut.text_information.topics = topic_set;
    }

    if(config.analysisConfig.get("Keyword") == "TRUE")
    {
        Set<String> keyword_set = new TreeSet<String>();
        if (apiOut.text_information == null)
            apiOut.text_information = new TextInformation();
        if(pyClient != null)
        {
            List<String> keywords = pyClient.getKeywords(sDoc);
            for (String keyword : keywords)
                keyword_set.add(keyword);
        }
        if (keyword_set.isEmpty() == true)
        {
            keyword_set.add("Keyword extractor not working, Start Thrift server at port 8002");
        }
        apiOut.text_information.keywords = keyword_set;
    }

    if(config.analysisConfig.get("Sentiment") == "TRUE")
    {
        String senti_temp = null;
        Set<String> senti_set = new TreeSet<String>();

        if (apiOut.text_information == null)
            apiOut.text_information = new TextInformation();

        if(pyClient != null)
        {
            if (apiOut.text_information == null)
                apiOut.text_information = new TextInformation();

            if (config.sentimentConfig.get("textType") == "blogs_news")
                senti_temp = pyClient.getSentimentScore(sDoc, config.sentimentConfig.get("title"), config.sentimentConfig.get("middleParas"), config.sentimentConfig.get("lastPara"), 1);
            if (config.sentimentConfig.get("textType") == "reviews")
                senti_temp = pyClient.getSentimentScore(sDoc, config.sentimentConfig.get("title"), config.sentimentConfig.get("topDomain"), config.sentimentConfig.get("subDomain"));
            else
                senti_temp = pyClient.getSentimentScore(sDoc, config.sentimentConfig.get("textType"));
        }
        if (senti_temp == null)
        {
            senti_temp = "Sentiment analyzer not working, Start Thrift server at port 8002";
        }

        senti_set.add(senti_temp);
        apiOut.text_information.sentiment = senti_set;
    }
    return gson.toJson(apiOut);
}

public static void main(String args[])
{
    TAConfig config = new TAConfig();
    TextManager temp = new TextManager();
    CmdLineParser parser = new CmdLineParser(temp);
    String sample = "The last week of Parliament is unlikely to see any business being conducted with the Congress party showing no signs of relenting on its demands and continuing to seek the resignation of Union and state ministers, while hitting out at the BJP for its “politics of abuse.Congress leaders parried questions on whether the MPs would continue to show placards in the House, an act that had angered the presiding officer. But Monday morning saw several Congress MPs carry placards to the House.Here’s the latest from Parliament:3:22 pm: Lok Sabha adjourned for the day amid continued Opposition protests over Lalitgate and Vyapam issue.2:38 pm: Rajya Sabha adjourned for the day after uproar over Lalit Modi, Vyapam and some other issues.: http://indianexpress.com/article/india/politics/parliament-logjam-25-suspended-mps-return-will-congress-let-house-function/#sthash.63UHi4eu.dpuf";


    String trialtext = "";
    try {
        parser.parseArgument(args);
    }catch(CmdLineException ex)
    {
        ex.printStackTrace();
    }
    System.out.println("Command line API :");
    parser.printUsage(System.out);
    config.entityConfig.put("person", "TRUE");
    config.entityConfig.put("organization", "TRUE");
    config.entityConfig.put("location", "TRUE");
    config.entityConfig.put("date", "TRUE");
    config.entityConfig.put("time", "TRUE");
    config.entityConfig.put("currency", "TRUE");
    config.entityConfig.put("percent", "TRUE");

    if((temp.text == null) && (temp.fileName == null)) {
        trialtext = "The last week of Parliament is unlikely to see any business being conducted with the Congress party showing no signs of relenting on its demands and continuing to seek the resignation of Union and state ministers, while hitting out at the BJP for its “politics of abuse.Congress leaders parried questions on whether the MPs would continue to show placards in the House, an act that had angered the presiding officer. But Monday morning saw several Congress MPs carry placards to the House.Here’s the latest from Parliament:3:22 pm: Lok Sabha adjourned for the day amid continued Opposition protests over Lalitgate and Vyapam issue.2:38 pm: Rajya Sabha adjourned for the day after uproar over Lalit Modi, Vyapam and some other issues.: http://indianexpress.com/article/india/politics/parliament-logjam-25-suspended-mps-return-will-congress-let-house-function/#sthash.63UHi4eu.dpuf";
        System.out.println("No Text source provided considering this default text for analysis : "+trialtext);
    }
    else if((temp.text != null) && (temp.fileName != null)) {
        System.out.println("Command line text input considered for Text Analytics : " + temp.text);
        trialtext = temp.text;
    }
    else if((temp.text != null)) {
        System.out.println("Command line text input considered for Text Analytics : " + temp.text);
        trialtext = temp.text;
    }
    else if((temp.fileName != null)) {
        System.out.println("Command line text input considered for Text Analytics");
        try {
            trialtext = Files.toString(new File(temp.fileName), Charsets.UTF_8);
        }catch(IOException ex)
        {
            ex.printStackTrace();
        }
        System.out.println("File input considered for Text Analytics : " + trialtext);
    }

    if((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Entity") == true)) {
        config.analysisConfig.put("Entity", "TRUE");
        System.out.println("Analyzing Entity in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Sentiment") == true)) {
        config.analysisConfig.put("Sentiment", "TRUE");
        System.out.println("Analyzing Sentiment in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Topic") == true)) {
        config.analysisConfig.put("Topic", "TRUE");
        System.out.println("Analyzing Topic in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Keyword") == true)) {
        config.analysisConfig.put("Keyword", "TRUE");
        System.out.println("Extracting keywords in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("All") == true)) {
        System.out.println("Analyzing Entity, Sentiment, Topic and Keywords");
        config.analysisConfig.put("Entity","TRUE");
        config.analysisConfig.put("Sentiment", "TRUE");
        config.analysisConfig.put("Topic", "TRUE");
        config.analysisConfig.put("Keyword","TRUE");
    }
    else {
        System.out.println("Analysis options not specified. Possible values : Entity, Sentiment, Topic, Kaeyword, All");
        System.out.println("Choosing All by default");
        config.analysisConfig.put("Entity","TRUE");
        config.analysisConfig.put("Sentiment", "TRUE");
        config.analysisConfig.put("Topic", "TRUE");
        config.analysisConfig.put("Keyword","TRUE");
    }
    System.out.println("Text Analytics output : ");

    System.out.println(temp.tagUniqueTextAnalyticsComponentsINJSON(trialtext, config));

}
}

我收到这样的错误...

org.org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132)org.apache.thrift.transport.TTransport.readAll(TTransport.java:86)处的org.apache.thrift.transport.TTransportException org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:318)上的apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:429)位于org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinary) org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:69)处com.diskoverorta.pyinterface.PyInterface $ Client.recv_getTopics(PyInterface.java:142)处com.diskoverorta.pyinterface.PyInterface $ Client处的java:219) com.diskoverorta.pyinterface.ThriftClient.getTopics(ThriftClient.java:123)处的com.diskoverorta.tamanager.TextManager.tagUniqueTextAnalyticsComponentsINJSON(TextManager.java:142)处的com.diskoverorta.tamanager处的.getTopics(PyInterface.java:129)。 TextManager.main(TextManager.java:285)线程“主”中的异常java.lang.NullPointerExceptio n在com.diskoverorta.tamanager.TextManager.main(TextManager.java:285)处的com.diskoverorta.tamanager.TextManager.tagUniqueTextAnalyticsComponentsINJSON(TextManager.java:145)

我对此完全陌生。...我无法弄清楚问题出在哪里...请帮助,谢谢!!

似乎config.analysisConfig为null。 因此,在调用config.analysisConfig.get时出现NullPointerException

您应该在所有使用config.analysisConfig的if中添加如下内容: config.analysisConfig != null && as:

if(config.analysisConfig != null && config.analysisConfig.get("Entity") == "TRUE") {
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM