Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2665
  • Last Modified:

What does this mean ?java.lang.NullPointerException

What does this mean ?
Uncaught exception in evaluation: java.lang.NullPointerException.

How would you fix this ?
0
pheats
Asked:
pheats
1 Solution
 
CEHJCommented:
Tricky! You'll have to look through the 'evaluation' method for objects that should have been initialised but weren't
0
 
TimYatesCommented:
what are you doing?
0
 
twansCommented:
heyas.. not too sure if your new to java soooo..

could you post come code?

also, to expand on CEHJ's suggestion: "look through the 'evaluation' method for objects that should have been initialised but weren't "

in newbie's terms this means the following:

Object whatever; => Uninitialised object
Object whatever = new Object(); => the 'new' operator calls the constructor of that class and thus initialises the object..

Now in regard to your 'evaluation' method.. it may look like:

public void evaluation( object Object) {..} .. the parameter, Object, that you pass this method MUST be initilalised.. a common error is to have the following..

if {}
else {
evaluation(Object)..
}

but since you have not initialised the object initially, there is a CHANCE that you might pass an object assigned as null. A quick fix is possibly to modify your initial instance variable from:
Object object;
to Object object = new Object();

the success of this will be dependent on your code.

hope this helps somewhat :)

twans
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
anthony_castillonCommented:
NullPointerException is thrown by the JVM when you are invoking a method on a null object.  Make sure your object references refer to actual objects before you invoke methods on them.
0
 
CEHJCommented:
Please don't repeat comments that have already been made
0
 
objectsCommented:
> Please don't repeat comments that have already been made

I was about to make the same comment as I didn't see where anyone had explained that.
There are many ways an NPE can occur than  just an unitialised var, and is not a clear explanation of an NPE.
0
 
applekannaCommented:
>>Uncaught exception in evaluation

Does this not mean that no exception occured still but we need to use a try catch to catch the Exception ...
Correct me if I am wrong

eg for pheats
if you have

public int Test() throws NullPointerException
{
 if ( a< 5 )
     throw new NullPointerExeption
else
return a;
}

and we use

int b = Test()

this needs to be used as
try{
int b = Test()
}
catch (NullPointerException e)
{
 System.out.println(e);
}

Hope this helps.
0
 
CEHJCommented:
No, NullPointerException does not have to be caught
0
 
objectsCommented:
> Does this not mean that no exception occured still but we need to use a try catch to catch the Exception ...

No it means an exception was thrown but it wasn't caught.
0
 
CEHJCommented:
One way of reproducing this would be to call evaluation here:

public static void evaluation() {
      try {
            x();
      }
      catch(Exception e) {
            System.err.println("Uncaught exception in evaluation: " + e.getClass().getName());
      }
}

public static void x() {
      String s = null;
      int i = s.length();
}




0
 
FesterWimCommented:
Maybe the simplest example would be:

String testString = null;
testString.substring(2); // Will throw a NullPointerException

Since testString does not refer to any real object, a NullPointerException is thrown.
0
 
CEHJCommented:
8-)
0
 
pheatsAuthor Commented:
I tried this and it proved that what is was doing was wrong.

I was able to catch the evaluation error.
Cheers
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now