java - Why am I getting a nullpointer exception? (Beginner) -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i extremely frustrated this, , have been trying days try , fix it. have 2 classes, main , 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 this:
cat laptop dog headphones
etc...
the error nullpointer exception @ line adding nouns file arraylist. can please me out?
listofwords
never constructed.
replace
arraylist<string> listofwords;
with
list<string> listofwords=new arraylist<>();
Comments
Post a Comment