• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why am I getting a Null Pointer Exception???

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. What is size, what is directory, how are they initialized ?
2. Telling us where the NullPointerException happens would save some time too.
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Directory is an array of StudentRecords.
Size is the counter variable.

Null Pointer Expcetion happens at:


I think the error is in:


Being that if I comment out the above part of the code, I can change the grade.
[ March 14, 2006: Message edited by: Christopher Beech ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are making strange things

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the problem would be clearer if I post everything. Here's the StudentRecord class


And here's the code for StudentDirectory
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Mark Van Tuyl
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of what I'm talking about
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to resolve a java.lang.NullPointerException
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [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 ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic