-1

I have some problem in my program. Line number 93 has a error. How to resolve this?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at first_JWNL.main(first_JWNL.java:93)

Code

 for (int i=0;i<sentences.length;i++)
             {  
                 System.out.println(i);
              System.out.println(sentences[i]);
              int wcount = sentences[i].split("\\s+").length;
        String[] word1 = sentences[i].split(" ");


        for (int j=0;j<wcount;j++){  
            System.out.println(j);

         System.out.println(word1[j]);
         String sen="one";

         IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]);
         IndexWord[] ws = set.getIndexWordArray(); 
         **POS p = ws[0].getPOS();**///////Line no 93

         Set<String> synonyms = new HashSet<String>();
         IndexWord indexWord = wordnet.lookupIndexWord(p, word1[j]);
         Synset[] synSets = indexWord.getSenses();
         for (Synset synset : synSets)
         {  Word[] words = synset.getWords();

            for (Word word : words)
            {  synonyms.add(word.getLemma());
            }
         }
         System.out.println(synonyms);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2010228
  • 47
  • 1
  • 4

3 Answers3

0

do a check :

if(ws.length > 0){
  POS p = ws[0].getPOS();
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

Your set.getIndexWordArray () returns empty array for some reason. You didn't post the code of this method, so I don't know why.

Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
0
IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]);

cannot find any index words in the array, and returns an empty set. Thus

IndexWord[] ws = set.getIndexWordArray();

returns a zero length array, and the expression

ws[0]

causes the Exception.

You have to take into consideration that there may be no matches.

For example, in your code, do something like this:

if (wordnet.size() == 0 ) {
    System.out.println("Could not find any words!!!!!");
} else {
    IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]);
    IndexWord[] ws = set.getIndexWordArray(); 
    POS p = ws[0].getPOS();

    // .......
    // ETC
    // ....... 

}
user000001
  • 32,226
  • 12
  • 81
  • 108