Loading...
Logo
Processing Forum
Hi , 
I have been working my way through the tutorials with Eclipse. My compiler has thrown this error up -->>

Exception in thread "Animation Thread" java.lang.NullPointerException
at tutorial2.draw(tutorial2.java:36)
at processing.core.PApplet.handleDraw(PApplet.java:2142)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193)
at processing.core.PApplet.run(PApplet.java:2020)
at java.lang.Thread.run(Unknown Source)

I reckon the problem is pretty straight forward and that I am missing something obvious , Bellow is the code ..... has anyone come across this already ??   
Would appreciate anyhelp
thanks 
Steve.


import java.awt.color.ColorSpace;
import processing.core.*;
public class tutorial2 extends PApplet 
{
PImage img = loadImage("Picture1.jpg");
public void setup()
{
//load the image and put it in a 400*400 box.
size(400,400);
}
public void draw()
{
//computer i would like to talk to the pixels
img.loadPixels();
//run through the picture Identifyign the RED : Green : Blue colours
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
int loc = x + y*width;
///these functions identify the colour involved
float r = red(img.pixels[loc]);
float g = green(img.pixels[loc]);
float b = blue(img.pixels[loc]);
//image processing would go here
//set the display pixel to the image pixel
pixels[loc] = color(r,g,b);
}
}
updatePixels();
}
}

Replies(3)

HI Lads,, 

Just an update for anyone else that might come across this problem, .. I changed the Size paramaters to size(200,200) and the application worked, and it works for all smaller numbers .. maybe it's a processing thing :/

Steve.
Changing the size should make no difference - I use Eclipse and had much bigger windows than 400x400.

The message from Eclipse is called a stack trace and it lists the method calls that led upto the exception. If we look at yours -

Exception in thread "Animation Thread" java.lang.NullPointerException
at tutorial2.draw(tutorial2.java:36)
at processing.core.PApplet.handleDraw(PApplet.java:2142)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193)
at processing.core.PApplet.run(PApplet.java:2020)
at java.lang.Thread.run(Unknown Source)

we can see that the exception was caused by something in the draw() method in your file tutorial.java.

BINGO just spotted the error

Here is your draw method (I have taken the liberty to tidy it up a little) the mistakes are in lines 5, 6 and 7. The variables width and height refer to the size of the window canvas NOT the size of the image. So if the canvas is bigger than the image you run of pixels and that is why it worked when you reduced the size of the canvas. The solution is to use img.width and img.height in lines 5-7

Copy code
  1. public void draw() {
  2. //computer i would like to talk to the pixels
  3.     img.loadPixels();
  4.     //run through the picture Identifyign the RED : Green : Blue colours
  5.     for(int y = 0; y < height; y++) {
  6.         for(int x = 0; x < width; x++) {
  7.             int loc = x + y*width;
  8. ///these functions identify the colour involved
  9.             float r = red(img.pixels[loc]);
  10.             float g = green(img.pixels[loc]);
  11.             float b = blue(img.pixels[loc]);
  12.             //image processing would go here
  13.             //set the display pixel to the image pixel
  14.             pixels[loc] = color(r,g,b);
  15.         }
  16.     }
  17.     updatePixels();
  18. }


Hi Quarks,  
That's great thanks a million, ..
 I think that understanding will avoid problems down the line 

:)