Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中调用对象时发生NoSuchMethodError_Java_Object_Methods_Nosuchmethoderror - Fatal编程技术网

在Java中调用对象时发生NoSuchMethodError

在Java中调用对象时发生NoSuchMethodError,java,object,methods,nosuchmethoderror,Java,Object,Methods,Nosuchmethoderror,我是Java新手,我不太明白当我试图调用一个方法时为什么会出现NoSuchMethodError。基本上,我尝试在另一个名为Agent.java的文件中调用一个方法,该文件反过来使用WordList.java中的方法 public class DiscussionDirector{ public static void main(String args[]){ Agent ag = new Agent(); ag.generateAgent();// line

我是Java新手,我不太明白当我试图调用一个方法时为什么会出现NoSuchMethodError。基本上,我尝试在另一个名为Agent.java的文件中调用一个方法,该文件反过来使用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(){
  }
}
以下是错误:

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)
下面是Agent.java的第148行:

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";
         }
  }
...
}
这是我的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;
}
}
import java.util.ArrayList;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.FileReader;
导入java.util.ArrayList;
导入java.util.Random;
导入java.io.FileNotFoundException;
公共类词表{
私有静态随机r=新随机();
私有静态ArrayList words=新ArrayList();
公共静态void main(字符串[]args)引发IOException{
字符串filename=“Majors.txt”;
单词列表wl=新单词列表();
buffer(文件名);
System.out.println(字);
System.out.println(getRandomWord());
}
公共静态ArrayList Buffer(字符串文件名)引发IOException{
//这就是相关的方法,基本上它从文件中读取单词并将其添加到ArrayList中。
words.clear();//从以前的阅读中清除ArrayList。
字符串行=null;
BufferedReader br=null;
br=新的BufferedReader(新的文件读取器(文件名));
而((line=br.readLine())!=null){
添加(行);
}
br.close();
返回单词;
}
公共静态字符串getRandomWord(){//方法,用于从ArrayList中随机检索字符串
单词列表wl=新单词列表();
字符串随机词;
如果(words.size()>1){
int index=r.nextInt(words.size());
randomWord=words.get(索引);
}
否则{
randomWord=“无法读取文件:Cities.txt”;
}
返回随机字;
}
}

不要使用
=
比较字符串的内容。使用
equals()
代替1)重新编译所有内容。2) 使用IDE,比如Eclipse。3) 点击Ctrl-Shift-F或Cmd-Shift-F!!请格式化您的代码…通过类而不是对象访问静态方法,如:
WordList.buffer(“MaleNames.txt”)
您正在运行某种实时调试器吗?尝试停止程序、重新编译并重新启动-NoSuchMethodErrors通常是旧类卡在加载程序中的结果