-2

i'm using linkedhashset to store my synonym list. if there is synonym for my search word, some statement will be done. However, when there is no synonym for my search word, some errors will occur. Below is part of my program.

String[] synset = wordnet.getAllSynsets(keyword, "n");
Set<String> synsetVec = new LinkedHashSet<String>();
for (int k = 0; k < synset.length; k++) {
    //store synonym in synsetVec
    synsetVec.add(s.Stem(synset[k]));
    System.out.println("SynsetVec = " + synsetVec);

    if (!synsetVec.isEmpty()) {
        //do something here
    } else {
        GUIsynonymTA.append("No synsets");
    }
}

This error "java.lang.NullPointerException" occur when no synset for the search word. Can anyone help me? Thanks in advance.

orien
  • 2,130
  • 16
  • 20
syakirah ibrahim
  • 29
  • 1
  • 1
  • 8
  • What is GUIsynonymTA and where is it initialized – Kal Nov 19 '11 at 02:57
  • on what line in your code do you get the nullpointer? (relevant stacktrace plz) – ratchet freak Nov 19 '11 at 02:57
  • You should use the debugger: set a breakpoint on the first line in the example and execute the program in Debug mode. The debugger will pause your code before the execution of the line with the breakpoint. Then you can execute staments one at a time (Step execution) and inspect your variables to see where and when exactly the code breaks. – zloster Nov 19 '11 at 09:08
  • @Kal: GUIsynonymTA is my text area to show the results. it has been initialized. This is only my part of programming. – syakirah ibrahim Nov 20 '11 at 02:12

1 Answers1

2

Based on the limited information I assume the NPE is occurring on the for loop line when you do sunset.lenght. Posting the stack trace would make answering this significantly easier.

Try:

String[] synset = wordnet.getAllSynsets(keyword, "n");
Set<String> synsetVec = new LinkedHashSet<String>();
if(sunset != null){
    for (int k = 0; k < synset.length; k++) {
        //store synonym in synsetVec
        synsetVec.add(s.Stem(synset[k]));
        System.out.println("SynsetVec = " + synsetVec);

        if (!synsetVec.isEmpty()) {
            //do something here
        } else {
            GUIsynonymTA.append("No synsets");
        } 
    }
}
Eric Rosenberg
  • 1,543
  • 9
  • 18