• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException  RSS feed

 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what is causing the runtime error can anyone check my code and tell me what I am doing wrong?

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please paste stack trace as well for easily finding the line which throwing null pointer exception?

I am thinking may be line 71 and 72 in grade class throwing an exception as you are using grades[0] without initializing it.
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I do that?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Grades constructor you are defining the grade instance variable array and in setGrades is the method in which you initializing the values of grades.
So before that you can't assign value to max and min. May be you can initialize max and min in that method as well.
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was the error message

Exception in thread "main" java.lang.NullPointerException
at project4.Grades.<init>(Grades.java:18)
at project4.Project4.main(Project4.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i think i am correct. It is getting the error at same place i thought.
 
C C Campbell
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried putting the max min in the setGrades but that just caused more error messages.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar is too polite, I think. When you get error messages, they actually contain information which is meant to be useful to you. So when you say "there were error messages" you are throwing that useful information away. Put yourself in the place of somebody trying to help you -- just knowing that there are error messages is basically useless. In future it would help if you would post the relevant information about your problem and not just an information-free summary. Have a look at our FAQ entry TellTheDetails (<-- click on that link) and it will explain more fully what information is useful.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to be specific: these lines of code are the problem:



As Tushar pointed out, you initialize the grades array in the constructor when you know how many students there are.
Maybe you want to initialize max and min there also?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to edit your code because one line was too long, and that made the post hard to read. I simply had to take a few hundred hyphens out. I didn't alter the rest of the code otherwise the line numbers would have been wrong, but your code is difficult to read because you are not consistent with where the { and } go and you have far too many blank lines. Please have a look at our suggestions about formatting.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good debugging technique I learned as a beginner is using System.out.println();

this allows you to check the data of your variables to ensure accuracy.....Hint: check you data

This goes well with Null Pointer Exceptions

For lists or Arrays (Java collections in general) I would just do a for loop to iterate through it and do a printLn statement to show me the data.

Lists may also require you to override the toString method and such, but there are tons of information that you can find stuff in this nature how to print out collections etc.... just by using Google. example Google: how to print an arrayList in java then I went to second link ---> example the first two answers looks really good to me and I would try both methods if needed......

System.out.printLn(); is also good to see if your program enters a loop properly such as an if else statement. Example....I want to check and see if my program enters the if and else statement properly based on user input. So I would put a printLn statement in both the if and else statement and then run the program twice to where the first time it enters the if statement and the second time it enters the else statement.....


Since the NPE is the array grades.....I would go to my various methods and check the data.....
When your problem is fixed you can just easily delete the printLn statements.....

I hope this helps.....

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!