为什么我得到一个空指针异常? (初学者)

为什么我得到一个空指针异常? (初学者)

问题描述:

我对此感到非常沮丧,并且一直在努力尝试修复它。我有两个班,主要和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 
}//end of Main class 

GetNounList:

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 

该文件具有随机的名词是这样的:

cat 
laptop 
dog 
headphones 

等等

The唯一的错误是在它将文件名称添加到数组列表的行的空指针异常。有人可以帮我吗?

+1

你没有初始化列表 –

listOfWords从未构建过。

更换

ArrayList<String> listOfWords; 

List<String> listOfWords=new ArrayList<>(); 
+0

哦,我的上帝,感谢你这么多 –

+0

你不知道多少,这激怒我了 –