0

I'm new to Java and I don't quite understand how come I get a NoSuchMethodError when I try to call a method. Basically, I try to call a method in another file called Agent.java which in turn use a method found in WordList.java.

    public class DiscussionDirector{
  public static void main(String args[]){
      Agent ag = new Agent();
      ag.generateAgent();// line5: This line is problematic.         
  }
  public static void discuss(){
  }
}

Here is the error:

java.lang.NoSuchMethodError: WordList.buffR(Ljava/lang/String;)Ljava/util/ArrayList;
    at Agent.generateAgent(Agent.java:152)
    at DiscussionDirector.main(DiscussionDirector.java:5)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Here is the line 148 of Agent.java:

public static void generateAgent(){
...
     try{
    if(gender == "male"){
    wl.buffR("MaleNames.txt");// line 148: I'm using a method in another file called WordList.java
    name = wl.getRandomWord();
    }
    else{
    wl.buffR("FemaleNames.txt");
    name = wl.getRandomWord();
    }
    }
       catch (IOException e){
         if(gender == "male"){
           name = "Rejean";
         }
         else{
           name = "Ginette";
         }
  }
...
}

And Here is my WordList.java

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.io.FileNotFoundException;

public class WordList{
  private static Random r = new Random();
  private static ArrayList<String> words =new ArrayList<String>();


public static void main(String[] args) throws IOException {
  String filename = "Majors.txt";
   WordList wl = new WordList();
    buffR(filename);
    System.out.println(words);
    System.out.println(getRandomWord());
}

    public static ArrayList buffR(String filename) throws IOException {
// this is the method concerned, basically it reads words from a file and add it to an ArrayList.
      words.clear();//clear ArrayList from previous readings.
String line = null;
BufferedReader br = null;

           br = new BufferedReader(new FileReader(filename));
            while (( line = br.readLine()) != null){
            words.add(line);  
            }
            br.close();

            return words;
    }


public static String getRandomWord(){ //method that retrieves randomly a string from the ArrayList
  WordList wl = new WordList();
  String randomWord;
  if(words.size() > 1){
int index = r.nextInt(words.size());
randomWord = words.get(index);
  }
  else{
    randomWord = "Unable to read file: Cities.txt";
  }
return randomWord;
}
}
user2826974
  • 199
  • 2
  • 7
  • 17
  • Don't compare content of Strings using `==`. Use `equals()` instead – Alexis C. Dec 01 '13 at 21:46
  • 4
    1) Recompile everything. 2) Use an IDE, like Eclipse. 3) Hit Ctrl-Shift-F or Cmd-Shift-F!! Format your code, please... – Martijn Courteaux Dec 01 '13 at 21:47
  • 2
    Access static methods over the class and not over the object, like so: `WordList.buffR("MaleNames.txt")` – Blub Dec 01 '13 at 21:47
  • 1
    Are you running some sort of live debugger? Try stopping your program, recompiling, and starting fresh - NoSuchMethodErrors are often the result of old classes being stuck in the loader – torquestomp Dec 01 '13 at 21:47

0 Answers0