• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException adding to array list  RSS feed

 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to finish a program in which i add values to an array list from user input and organize them via comparable, but i keep getting a null pointer exception. Can someone help me?




Here is the error code


I've read up on stack overflow about null pointer exceptions but im still confused about what im doing wrong... Can someone help
 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Issue on line 24 points to UserMenu();

Issue on 72 points to Collections.sort(Courses);
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the null value on the line where the exception happens?  Print out the values of all the variables on the line if you can't see where the null value is.

Then backtrack in the code to see where the null value came from and change the code to have a valid value.
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are declaring a variable "Courses" on line 28. This overrides the class field of the same name and so the field does not get populated. This method does, however, return the local instance of "Courses" but where you are calling this method you aren't doing anything with the methods returned value. Hence, the field is null.

Also, variable names should always begin with a lower case letter.
 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
You are declaring a variable "Courses" on line 28. This overrides the class field of the same name and so the field does not get populated. This method does, however, return the local instance of "Courses" but where you are calling this method you aren't doing anything with the methods returned value. Hence, the field is null.

Also, variable names should always begin with a lower case letter.


Ahh Thank you. That resolved that issue.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!