The get() function returns NullPointerException

Hello,
I’ve been trying to use the get() function inside my sketch to get the colour of a pixel at a specific position. However, when I attempt to do this inside the sketch it returns NullPointerException, I think I’ve found that this problem stems from my use of “surface.setSize()” in conjunction with the get() request, as when I remove it, the get request seems to work properly. But I still do not understand why this error is being created and am unable to reproduce this error in a more simple sketch like this.
surface.setSize(1000,625); println(get(0,0));
Is there anything else that could be causing get() to return NullPointerException that I might be missing?

Hi @s31,

We heed to see more of your code in order to diagnose the problem. In the meantime, what happens if you do this?:

println(surface.get(0,0));
1 Like

Hi @javagar,
sorry for the lack of code in my original post, the sketch that has this problem is very large but I’ve managed to boil the problem down to this snippet that demonstrates my issue (for me at least).

void settings() {
  size(1000,625,P2D);
}

void setup() {
  clear();
  surface.setResizable(true);
}

void draw() {
  clear();
  surface.setSize(1000,625);
  
  println(get(0,0));
}

In regards to the other half of your reply, if I try

println(surface.get(0,0));

Processing returns “The function ‘get(int, int)’ does not exist”. I assume this could be for an older version of processing or something as it doesn’t appear to work in 4. I hope the added code will help and thank you for your response!

1 Like

Okay so I found that if I remove the;

size(1000,625,P2D);

at the beginning of my code it starts working now for some reason, I have no idea as to why this is, but it functions perfectly now.

edit: i think the P2D renderer was the culprit in this instance as it seems to have a track record with surface functions, I hope they patch this

1 Like

Glad you got it worked out, @s31.

Thanks for the code. Adding this to your code reveals what type surface represents, which makes it obvious why my suggestion didn’t correct the problem:

  println(surface.getClass());

Output:

class processing.awt.PSurfaceAWT

That type does not offer a get() method. :blush:

Hello,

I am sharing what I was testing:

void settings()
  {
  size(800, 800, P2D);
  }

void setup()
  {
  //clear();
  //println(hex(get(700, 700))); //Uncomment and no Exception below
  surface.setSize(400, 400);
  surface.setResizable(true);
  println(hex(get(0, 0)));    //NullPointerException
  //noLoop();
  }

void draw() 
  {
  //clear();

  // Toggles every second
  if(((frameCount/60) & 0x1) == 0)
    surface.setSize(100, 100);
  else
    surface.setSize(200, 200);
  
  background(255, 0, 0);
  println(hex(get(0,0)));
  }

There are many topics on this in the forum that may be of interest:

:)