-1

I am extremely frustrated with this, and have been trying for days to try and fix it. I have two classes, Main and GetNounList

Main:

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

The file has random nouns like this:

cat
laptop
dog
headphones

etc...

The only error is the nullpointer exception at the line where it is adding the nouns from the file to the arraylist. Can someone please help me out?

Joshua Fox
  • 15,727
  • 14
  • 65
  • 108

1 Answers1

0

listOfWords was never constructed.

Replace

ArrayList<String> listOfWords;

with

List<String> listOfWords=new ArrayList<>();
Joshua Fox
  • 15,727
  • 14
  • 65
  • 108