Search code examples
javanullwordnetstemmingdata-processing

Logical flaw: if List<String> is null return input else print function output


In my code I call this method, as a preprocessing step to 'stem' words:

public void getStem(String word)
{
    WordnetStemmer stem =  new WordnetStemmer( dict );

    List<String> stemmed_words = stem.findStems(word,  POS.VERB);
    System.out.println( stemmed_words.get(0) );
}

Usually everything is good if it gets a normal word (I'm using the Java Wordnet Interface to handle the stemming). The thing is--> I don't always get a normal word, somethings I get things along the lines of isa which is a conjunction of is and a. In such a case that method will return null and my program will crash. How can I defend against this?

This is how I call that code:

public Sentence(String verb, String object, String subject ) throws IOException
{
    WordNet wordnet = new WordNet();

    this.verb = verb;
    this.object = object;
    this.subject = subject;

    wordnet.getStem( verb );
}

Eventually I want that to read:

this.verb = wordnet.getStem( verb );

I once heard about doing something with null objects, is that applicable here?

I tried this but it didn't work, but I want to do something like this:

public void getStem(String word)
{
 WordnetStemmer stem =  new WordnetStemmer( dict );

 List<String> stemmed_words = stem.findStems(word,  POS.VERB);
 if( stemmed_words != null)
     System.out.println( stemmed_words.get(0) );
 else
     System.out.println( word );
}

This is the output:

prevent
contain
contain
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0
at java.util.Collections$EmptyList.get(Collections.java:4454)
at inference_learner.WordNet.getStem(WordNet.java:76)
at inference_learner.Sentence.<init>(Sentence.java:23)
at inference_learner.RegEx.match_regex_patterns(RegEx.java:33)
at inference_learner.ReadFile.readFile(ReadFile.java:30)
at inference_learner.Main.main(Main.java:38)

Solution

  • That won't work because the List is not null, the List is empty. You have to do the check like this if (stemmed_words.size() > 0)