0

So, I have the following code written in Java which uses the Stanford CoreNLP library to perform sentiment analysis of tweets.

import java.util.*;
import java.util.List;
import java.util.Properties;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.ArrayCoreMap;
import edu.stanford.nlp.util.CoreMap;
public class NLPAnalysis 
{
static StanfordCoreNLP pipeline;

public static void init() {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize,parse,sentiment,pos");
    pipeline = new StanfordCoreNLP(props);
}

public static String findSentiment() {
    Scanner sc = new Scanner (System.in);
    String tweet = sc.next();
    String SentiReturn = "";
    String[] SentiClass ={"very negative", "negative", "neutral", "positive", "very positive"};

    //Sentiment is an integer, ranging from 0 to 4. 
    //0 is very negative, 1 negative, 2 neutral, 3 positive and 4 very positive.
    int sentiment = 2;

    if (tweet != null && tweet.length() > 0) {
        Annotation annotation = pipeline.process(tweet);

        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        if (sentences != null && sentences.size() > 0) {

            ArrayCoreMap sentence = (ArrayCoreMap) sentences.get(0);                
            Tree tree = sentence.get(SentimentAnnotatedTree.class);  
            sentiment = RNNCoreAnnotations.getPredictedClass(tree);             
            SentiReturn = SentiClass[sentiment];
        }
    }
    return SentiReturn;
}

public static void main(String[] args) throws Exception
    {
    System.out.println("Enter your tweet:");
    String answer = findSentiment();
    System.out.println("The Sentiment:"+answer);
    }
}

I have set the annotators to tokenize, parse, sentiment, pos based on a previous answer to a similar problem, but I still keep getting a NullPointerException at line 33: Annotation annotation = pipeline.process(tweet);.

Please help me with this issue. Thanks.

reddevil_j
  • 33
  • 11
  • 1
    Are you supposed to call `init()` first within the main method? Else, how does pipeline get initialized? – Hovercraft Full Of Eels Mar 18 '18 at 17:26
  • Oh yes, that helped. I completely forgot about that. Thanks for pointing that out. It works now. – reddevil_j Mar 18 '18 at 17:32
  • 1
    The exception should have told you to do this. You already know which line throws it, it is simple to test which fields are null *before* that line (simple null tests in your code), and that should tell you that you're not initiating pipeline. Then look back and see why. In the future, search on the exception to see how others have solved it previously. Please do this *before* asking since we get asked about NullPointerExceptions 10 times a day, at least. – Hovercraft Full Of Eels Mar 18 '18 at 17:33

0 Answers0