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

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

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

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