1

I am somewhat desperate as i dont even know how its possible that i get the Exception i am getting.

From the stack trace:

java.lang.NullPointerException
    at us.fl.k12.sdhc.services.dto.MagnetApplicant.getGrade(MagnetApplicant.java:86)
...

MagnetApplicant code:

84 public Grade getGrade()
85 {
86  return grade;
87 }

Grade is an Enumeration. How can a NullPointerException be thrown when just returning a value?

Thanks for reading!

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Stefan
  • 21
  • 2
  • 2
    What is the line calling getGrade()? I'm guessing you have a class hierarchy in that call and one of the classes hasn't been initialized...rather than grade. – Justin Niessner Jan 26 '11 at 15:22
  • 2
    Are you sure the source is exactly the version that is running? – biziclop Jan 26 '11 at 15:23
  • 2
    "return grade;" will never throw NPE. I guess your source code and runtime class are not at same version. – 卢声远 Shengyuan Lu Jan 26 '11 at 15:26
  • 1
    Consider creating an [SSCCE](http://sscce.org). – BalusC Jan 26 '11 at 15:30
  • Doing a clean rebuild should either solve the problem or give more meaningful stack trace. Right now it looks like a very weird bug in the JVM or just something that can't possibly happen. – Sergei Tachenov Jan 26 '11 at 15:51
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – user2864740 Oct 11 '14 at 21:41

4 Answers4

2

The object reference on which you are invoking the getGrade() method might be null. It is not necessarily the enum itself that has this method.

Since enums do not get autoboxed/autounboxed and returning a null from a method is fine, the problem is not within the method as such.

pnt
  • 1,916
  • 1
  • 20
  • 29
  • 1
    The stacktrace doesn't indicate that. It's been thrown inside `getGrade()` method, not inside the method where the `getGrade()` method is been called. – BalusC Jan 26 '11 at 15:27
  • The problem still is somewhere in the method, because the stack trace clearly says so. The question is whether the method running is the same as the one we can see the source code of. – biziclop Jan 26 '11 at 15:29
  • @BalusC: Acutally, you're right. I mistook it for 84 at first glance. But my arguments are still valid. That means that the stacktrace is not full - there must have been a chained call on the enum reference that failed because it was null. – pnt Jan 26 '11 at 15:31
  • Looks like this is the right answer. I have about 100 emails in my inbox with an attached stack trace all mentioning that line 86. What i hadnt noticed is that the stacktrace on my local machine doesnt have it at the top. So for some unexplainable reason the server gives me a wrong stack trace. Thanks everyone for their time :D – Stefan Jan 26 '11 at 20:06
1

I've seen similarly weird exceptions in cases like this:

Integer value;

public int getValue() {
  return value;
}

In these cases return value can throw an NPE when value is null (because of auto-unboxing).

I can't really explain your case, 'though.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

It should not happen. Are you using some kind of a framework that does something in background for you? E.g. Spring AOP, hibernate etc.

Hery
  • 7,443
  • 9
  • 36
  • 41
0

Is it possible that the class on which the method is invoked is null, and that the compiler has optimized out the function call so that the bytecode reads, in effect:

Grade myGrade = myClass.grade;

The compiler has some tricky stuff to do working out which line of the original source that corresponds to, and maybe chose getGrade line 86.

If should be easy to prove whether the class is null or not.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79