[Solved] Why am I getting a NullPointerException when loading shapes or images?

Hey guys,

I feel silly for posting a null pointer issue put I really don't understand what's going on.

I've checked if the files are in the data folder, tried a png with loadImage (results in different type of null pointer), tried a png that works in another processing project and I've even created a new project and copy pasted the code. Nothing helps.

public class Meter {

  PShape meterBg;
  //PImage m;

  PVector origin, end;
  float l, w;
  float angle;
  float maxAngle = radians(180);
  float minAngle = radians(0);


  Meter ()
  {
    meterBg = loadShape("svg_test.svg");
    //m = loadImage("SPR_Electron.png");
    l = 50;
    w = 4;
    origin = new PVector(0,0);
    end = new PVector(0,l);
  }

  public void drawNeedle (float substanceX)
  {

    angle = (substanceX * (maxAngle / 10.0)) - PI;
    println(degrees(angle) + " --- " + substanceX);

    pushMatrix ();

      translate (width/2, height);

      shape(meterBg, -50, 0);
      //image(m, -50, 0);

      end.x = cos(angle) * l;
      end.y = sin(angle) * l;

      strokeWeight(w);
      stroke(255,0,0);

      line(origin.x,origin.y, end.x, end.y);

    popMatrix ();
  }
}

I'm hoping I'm just reeaaalllyy blind and missed the obvious..

Help would be madly appreciated!

Cheers, Phil

Tagged:

Answers

  • There is nothing obviously wrong here. Have you tried loading the shape in setup to see if you still get the NPE.

    Different types of null pointer

    Can you post the stack trace (the full error message) it might give some hints as to the problem.

    I assume you are in Java mode and using the latest version of Processing.

  • I used Processing 2.03 then got 2.1 with the same results. I am in Java Mode. I tried it in JavaScript mode, it didn't give me an error but it also didn't load the image (even with the pjs preload code at the top).

    This is what I get from the code above:

    java.lang.RuntimeException: java.lang.NullPointerException
        at processing.core.PApplet.runSketch(PApplet.java:10572)
        at processing.core.PApplet.main(PApplet.java:10376)
    Caused by: java.lang.NullPointerException
        at processing.core.PApplet.loadShape(PApplet.java:11482)
        at Homeostasis$Meter.<init>(Homeostasis.java:79)
        at Homeostasis.<init>(Homeostasis.java:20)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at processing.core.PApplet.runSketch(PApplet.java:10570)
        ... 1 more
    

    If i try loading a .png instead I end up with this error:

    The file "svg_test.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
    
  • Ok, so I solved it. It was pretty stupid and only happened because I was rushing it.

    In the main tab of the sketch I created the meter instance outside any class or setup. If I moved "meter = new Meter();" to setup it works fine.

    Thanks quark.

  • Technical FAQ - Processing, second article, addressing this issue among other common ones. :-)

    Anyway, thanks for sharing your analysis, it can be embarrassing (but it isn't obvious, actually, even more hidden in a class constructor like this) but it can be useful to people taking time to search for a solution. B-)

  • I have the same problem. It can't locate a image that is within the data folder.

Sign In or Register to comment.