Hi, First time poster here so sorry if I've done something wrong in relation to posting. I am creating a hangman game and I want to read in a list of words from a text file, but after the user inputs the name of the text file. I get 'Exception in thread "main" java.lang.NullPointerException'.
Here is the code, where I think the problems lie.
The full error message is this:public void runModel(){ ArrayList<String> pirateWordsList = new ArrayList<String>(); System.out.println("What is the name of the file you would like to load? (The file included is called piratewords.txt'"); Scanner in=new Scanner(System.in); String file=in.next(); load(file); System.out.println(pirateWordsList); public void load(String pirate){ Scanner infile; try { infile = new Scanner(new InputStreamReader (new FileInputStream(pirate))); int num=infile.nextInt();infile.nextLine(); for (int i=0;i<num;i++){ String secretPirateWord=infile.nextLine(); pirateWordsList.add(secretPirateWord); } infile.close(); } catch (FileNotFoundException e) { System.out.println("File not found, are you sure you entered the files name correctly and is the file in the correct directory"); } }
Exception in thread "main" java.lang.NullPointerException
at uk.ac.aber.dcs.pirate_hangman.Model.load(Model.jav a:108)
at uk.ac.aber.dcs.pirate_hangman.Model.runModel(Model .java:45)
at uk.ac.aber.dcs.pirate_hangman.Main.main(Main.java: 6)