Reputation: 3
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[]"
Upvotes: 0
Views: 4668
Reputation: 42174
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.
Upvotes: 1