java - Getting a NullPointerException when creating a method that adds to an ArrayList -


this code:

private arraylist<integer> gradelist;    public void addgrades(scanner reader) {         int grade = reader.nextint();         if (grade != -1) {             this.gradelist.add(grade); // --> nullpointerexception         }     } 

i can't figure out why getting nullpointerexception, because have made methods without problem. thought changing this:

 public void addgrades(scanner reader) {         int grade = integer.parseint(reader.nextline());         while (true) {             if (grade == -1) {                 break;             } else {                 this.gradelist.add(grade); // --> nullpointerexception             }         }     } 

would help, did not fix problem. in both cases, problem on line this.gradelist.add(grade). tried declaring arraylist null within method before loop, realized redundant. i'm not sure i'm doing wrong.

you never instanitating arraylist. try

private arraylist<integer> gradelist = new arraylist<integer>(); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -