2015-08-10 2 views
0

Моя цель провести анализ настроений, экстракцию тема, объект ... когда я выполнить следующий код Java с комиссионном сервер начал ....«Исключение в потоке» главный «java.lang.NullPointerException»

 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.apache.thrift.transport.TTransportException в org.apache.thrift.transport.TIOStreamTransport.read (TIOStreamTransport.java:132) в org.apache.thrift. transport.TTransport.readAll (TTransport.java:86) при org.apache.thrift.protocol.TBinaryProtocol.readAll (TBinaryProtocol.java:429) в org.apache.thrift.protocol.TBinaryProtocol.readI32 (TBin aryProtocol.java:318) при org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin (TBinaryProtocol.java:219) при org.apache.thrift.TServiceClient.receiveBase (TServiceClient.java:69) в com.diskoverorta.pyinterface.PyInterface $ Client.recv_getTopics (PyInterface.java:142) в com.diskoverorta.pyinterface.PyInterface $ Client.getTopics (PyInterface.java:129) в com.diskoverorta.pyinterface.ThriftClient.getTopics (ThriftClient.java:123) на ком. diskoverorta.tamanager.TextManager.tagUniqueTextAnalyticsComponentsINJSON (TextManager.java:142) при com.diskoverorta.tamanager.TextManager.main (TextManager.java:285) Исключение в потоке "основного" java.lang.NullPointerException в com.diskoverorta.tamanager.TextManager .tagUniqueTextAnalyticsComponentsINJSON (TextManager.java:145) на com.diskoverorta.tamanager.TextManager.main (TextManager.java:285)

Я совершенно новый для этого .... не могу выяснить, где проблема ... пожалуйста, помогите, спасибо!

ответ

0

Кажется, что config.analysisConfig равна нулю. Поэтому NullPointerException при вызове config.analysisConfig.get

Вы должны добавить во всех МСФ, которые используют config.analysisConfig что-то вроде: config.analysisConfig != null && как:

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

Я сделал changes..am все еще получаю ту же ошибку. – Karthik

+0

Его работа над некоторым пробным текстом, данным ... не все. – Karthik

+0

Проверьте http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it. Найдите все конструкции, такие как a.b.c, и проверьте, является ли a.b нулевым. – JFPicard