0

I've been coding for a few weeks now and I'm stuck on this null pointer exception.

It gives me the following error:

java.lang.NullPointerException  
at processing.core.PGraphics.image(PGraphics.java:3768)     
at processing.core.PApplet.image(PApplet.java:12175)    
at beginfase.tekenBeginscherm(beginfase.java:126)   
at beginfase.draw(beginfase.java:51)    
at processing.core.PApplet.handleDraw(PApplet.java:2412)    
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540)    
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

it marks this line of code: image(kaarten[r],50,50);

   String[] kaart = { 
"Aclubs.png", 
"2clubs.png", 
"3clubs.png",
"4clubs.png",
"5clubs.png",
"6clubs.png",
"7clubs.png",
"8clubs.png",
"9clubs.png",
"10clubs.png",
"Jclubs.png",
"Qclubs.png",
"Kclubs.png",
};

PImage[] kaarten = new PImage [kaart.length];

void settings() {
  size(round(displayWidth * 0.9), round(displayHeight * 0.9));
}

void setup() {
  laadBeginscherm();
 // schudKaarten();
 }



void draw() {
  tekenBeginscherm();
 // schudKaarten();
  // println(mouseX);
  //println(mouseY);
}

There is a bit of my code I know it's probably a mess and really bad, but I can't find what the nullpointerexception error is. Please help.

Yes, i now added the 10clubs.png

This is the code I use to load the image.

if (scherm == 3) {
        background(0, 148, 0);
        for (int g = 0; g < 12000; g = g+round((displayWidth * 0.9))/12) {
          int r = int (random(kaarten.length));
          println(r);
          image(kaarten[r],g+25,50);
        }      
      }

EDIT: Okay i made a smaller (mini) version of the error. It uses just 1 card but that's just for the example of it, so you can add a .png yourself

    String[] kaart = { 
  "Aclubs.png", 
};


PImage[] kaarten = new PImage [kaart.length];

void setup() {
  kaart = loadImage("Aclubs.png");
}

void draw() {
  tekenBeginscherm();
}
void tekenBeginscherm() {
  if (keyPressed == true ) {
    background(0, 148, 0);
    for (int g = 0; g < 12000; g = g+round((displayWidth * 0.9))/12) {
      int r = int (random(kaarten.length));
      println(r);
      image(kaarten[r], 50, 50);
    }
  }
}

If I try to add this: kaart = loadImage("Aclubs.png"); in setup() The program will give me this error instead:

Type mismatch, "processing.core.PImage" does not match with "java.lang.String[]"

theArminas4
  • 3
  • 1
  • 5
  • Please post a [mcve] that shows how you're loading and displaying the images. – Kevin Workman Nov 02 '16 at 14:27
  • `PImage[] kaarten = new PImage [kaart.length];` initializes the array with `null` values. Do you ever construct instances of `PImage` to place in the array? – bradimus Nov 02 '16 at 14:29
  • Did you intend to have a `10clubs.png` in your `String` array? – PEF Nov 02 '16 at 14:30
  • Also: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim Nov 02 '16 at 14:41
  • @dorukayhan Please note that this is a [tag:processing] question, and this error is happening inside Processing itself. The answer to the canonical duplicate does not really apply to this type of question. – Kevin Workman Nov 02 '16 at 14:50
  • That still isn't a [mcve]. Please post a full sketch that we can copy and paste to run ourselves. Where are you calling the `loadImage()` function? – Kevin Workman Nov 02 '16 at 14:51
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – talex Nov 02 '16 at 14:55
  • @talex Please note that this is a [tag:processing] question, and this error is happening inside Processing itself. The answer to the canonical duplicate does not really apply to this type of question. – Kevin Workman Nov 02 '16 at 14:59
  • @KevinWorkman so you insist that this is not `Java` code? – talex Nov 02 '16 at 15:04
  • @talex It's Processing. Please see this: [Processing != Java](http://meta.stackoverflow.com/questions/321127/processing-java) – Kevin Workman Nov 02 '16 at 15:05
  • @KevinWorkman Then TS should remove `java` tag. – talex Nov 02 '16 at 15:06
  • @talex That's debatable, as discussed in the link I sent you. I don't really have any strong opinions on that. I'm just pointing out that a NPE in Processing isn't always solved by the canonical Java answer. What is TS? – Kevin Workman Nov 02 '16 at 15:08
  • It's a `java.lang.NullProcessorException` #justsayin – slim Nov 02 '16 at 15:19
  • @KevinWorkman "TS" stands for "topic starter" I don't remember acronym used on this site and used generic one. It isn't debatable. If this code is written in "processing" language then tag must be removed. The fact that the language is use `java` as backend doesn't change anything. – talex Nov 02 '16 at 15:28
  • @talex Ah, I've usually seen OP for original poster. But like I said, I don't have a strong opinion on this. Some Processing questions are indeed Java questions. Some aren't. This one could go either way, but the NPE isn't as simple as trying to dereference a `null` variable, so the canonical question isn't super helpful. I don't really want to debate this, so if you want to offer input, feel free to comment on the meta post. – Kevin Workman Nov 02 '16 at 15:31
  • Please see me edited answer in response to your edited question. And please post follow-up questions in their own question post, otherwise nobody will see them. Don't forget to include a [mcve]. – Kevin Workman Nov 02 '16 at 18:50

1 Answers1

1

When loading an image, you have to make sure of a few things:

  • Make sure your image files have been added to your sketch. They should be inside a data directory inside your sketch folder. Make sure they're spelled correctly.

  • Make sure you're calling the loadImage() function before trying to call the image() function.

From the reference, here is a small example that shows how to load and display an image:

PImage img;

void setup() {
  img = loadImage("laDefense.jpg");
}

void draw() {
  image(img, 0, 0);
}

My guess is that you forgot to call loadImage(), which means that your image variable (in your case, array index) is null, which will cause a NullPointerException when you call the image() function.

If you're still having trouble, please narrow your problem down to a small example like this one.

Edit: In response to your edited question:

If I try to add this: kaart = loadImage("Aclubs.png"); in setup() The program will give me this error instead:

Type mismatch, "processing.core.PImage" does not match with "java.lang.String[]"

You need to loop over every index in your array and call loadImage() to load an image into each index.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107