Ok. Why does the following code give me a null pointer exception??? It's suppose to search an array for a given name. If name is found, it changes the grade to one the user gives. If name is not found, it adds the name and the grade given by the user.
[ March 14, 2006: Message edited by: Christopher Beech ]
For example, using 'size' as the ending condition for(int i=0;i<size;i++) but incrementing it size++;
Where do you initialize 'size' ?
Christopher Beech
Ranch Hand
Joined: Feb 08, 2006
Posts: 40
posted
0
Maybe the problem would be clearer if I post everything. Here's the StudentRecord class
And here's the code for StudentDirectory
Mark Van Tuyl
Ranch Hand
Joined: Mar 22, 2002
Posts: 60
posted
0
This looks kind of odd to me The if statement attempts to retrieve a StudentRecord object from the array. It then attempts to compare the name in the object to the name provided. If they're not equal, it creates a StudentRecord object at that position in the array.
Why not make sure the object exists before trying to call a method on it?
<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Mark Van Tuyl
Ranch Hand
Joined: Mar 22, 2002
Posts: 60
posted
0
Here's an example of what I'm talking about
Christopher Beech
Ranch Hand
Joined: Feb 08, 2006
Posts: 40
posted
0
Replacing the
with
still does not add a new record and it now takes twice as long to change the grade of an existing record.
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
posted
0
If you take a look at the stack trace, it will tell you exactly what line the NullPointerException happens on. Once you know that, you can take a look at the offending line and ask yourself, "what can be null here?"
If you post exactly what the offending line is, we will probably be better able to help.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ok. Using my original code, here's the error message I get.
NullPointerException: at StudentDirectory.addOrChangeRecord(StudentDirectory.java:118) at StudentDirTester.main(StudentDirTester.java:102) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
At line 118 of the StudentDirectory:
line 102 of the StudentDirTester is:
However, i'm 100% certain that there is no errors in the StudentDirTester, being that it was typed up by the graduate students and nobody else had any trouble.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
[Christopher]: At line 118 of the StudentDirectory:
This is the only line we really need to know here. But you have shown us 9 lines for some reason.
Which one line (one single, solitary line, more than zero lines but less than two lines is line number 118? That is the line you need to look at. [ March 14, 2006: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Christopher Beech
Ranch Hand
Joined: Feb 08, 2006
Posts: 40
posted
0
The code at line 118 is the if(directory[i].getName().equals(name)) code. I can't understand why the compiler doesn't like the code, through. The code seems logical enough to me.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
One of the things being referenced in that line is null. It's one of these:
directory is null
directory[i] is null
getName() is null
From your other code, it seems directory is not null, your the earlier reference to directory.length would have thrown an NPE. I recommend printing out each of these things to find out what their values are:
When you know what the cause really is, then you can better decide what to do about it.
Christopher Beech
Ranch Hand
Joined: Feb 08, 2006
Posts: 40
posted
0
My guess is that directory[i] is null. By the time the complier gets to the AddorChange method, directory is already filled with an array of StudentRecords. And I believe that getName() would be the name that the users gives.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
[Christopher]: My guess is that directory[ i ] is null.
But why guess? We have to guess, because we have incomplete information. You, however, have the complete source code, and are running this on your own machine, correct? You have the ability to modify the code and find out what is going on. I just suggested a way that you could find out. Insert those print statements into the code, just before line 118. Then you will easily be able to know, for sure, which element of the code is null. [ March 14, 2006: Message edited by: Jim Yingst ]