nullpointerexception question

edited March 2014 in Programming Questions

First.... what does this mean? While doing a search I see this error a lot attached to a variety of different sketches.

second I am doing a serial feed read from temperature sensors on an arduino. It runs fine for awhile then throws the nullpointerexception.

steve

Answers

  • edited March 2014

    when a var is null and you wanna use it it throws this

    e.g. you load an image which is not there and then you want to show it.

    PImage a1 = loadImage("wrong.png");
    image (a1,30,30); // fails 
    

    better

    PImage a1 = loadImage("wrong.png");
    if (a1!=null) 
        image (a1,30,30); // better
    

    please read on here, section exceptions:

    http://wiki.processing.org/w/Technical_FAQ

  • That happens when we try to access a property from a null reference. Using the dot operator for example:

    http://processing.org/reference/dot.html

  • you wrote

    second I am doing a serial feed read from temperature sensors on an arduino. It runs fine for awhile then throws the nullpointerexception.

    I think when the speed of the sensor and your PC don't match, the sensor value can be null (there wasn't a sensor value, buffer was empty) - hence the error

    to avoid this, check your sensor data if they are null:

    if (sensor1 != null)
        {
           // do something 
        }
    

    also look at try---catch....

  • See Technical FAQ in the Exceptions section for general explanations, and more specifically: Why do I get a NullPointerException?

Sign In or Register to comment.