Java 为什么我得到一个空指针异常?(初学者)
我对此感到非常沮丧,并且已经尝试了好几天来修复它。我有两个班,Main和GetNounList 主要内容:Java 为什么我得到一个空指针异常?(初学者),java,eclipse,Java,Eclipse,我对此感到非常沮丧,并且已经尝试了好几天来修复它。我有两个班,Main和GetNounList 主要内容: import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws FileNotFoundException { GetNounList nouns = new GetNounList(); }// end main method
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
GetNounList nouns = new GetNounList();
}// end main method
}//end of Main class
获取列表:
import java.io.*;
import java.util.*;
public class GetNounList extends Main {
ArrayList<String> listOfWords;
public GetNounList() throws FileNotFoundException {
Scanner list = new Scanner(new File(
"/Users/FareedMabrouk/Desktop/Explore/Coding/Java/BusinessIdeaGenerator/CodeRepository/BusinessGen/src/Nouns.txt"));
while (list.hasNextLine()) {
listOfWords.add(list.nextLine());
} // end while loop
System.out.println(listOfWords);
}//end constructor
}//end GetNounList class
等等
唯一的错误是将文件中的名词添加到arraylist的行中的nullpointer异常。有人能帮我一下吗?
listOfWords
从来没有构建过
替换
ArrayList<String> listOfWords;
arraylistlistofwords;
与
listOfWords=newarraylist();
你没有初始化列表哦,天哪,非常感谢你你不知道这有多让我生气
ArrayList<String> listOfWords;
List<String> listOfWords=new ArrayList<>();