# The get() function returns NullPointerException

**URL:** <https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039>\
**Category:** Coding Questions\
**Created:** [October 24, 2021, 12:06pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039 "2021-10-24T12:06:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![s31](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@s31](https://discourse.processing.org/u/s31)\
**Post date:** [October 24, 2021, 12:06pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039/1 "2021-10-24T12:06:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [October 24, 2021, 12:24pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039/2 "2021-10-24T12:24:33Z")

</div>

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?:

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

```

---

<div class="post-metadata">

**Author:** ![s31](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@s31](https://discourse.processing.org/u/s31)\
**Post date:** [October 24, 2021, 12:48pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039/3 "2021-10-24T12:48:30Z")

</div>

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).

```auto
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

```auto
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!

---

<div class="post-metadata">

**Author:** ![s31](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@s31](https://discourse.processing.org/u/s31)\
**Post date:** [October 24, 2021, 1:05pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039/4 "2021-10-24T13:05:07Z")

</div>

Okay so I found that if I remove the;

```auto
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

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [October 24, 2021, 2:42pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039/5 "2021-10-24T14:42:36Z")

</div>

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:

```auto
  println(surface.getClass());

```

Output:

```
class processing.awt.PSurfaceAWT

```

That type does not offer a `get()` method. 😊

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 24, 2021, 4:06pm UTC](https://discourse.processing.org/t/the-get-function-returns-nullpointerexception/33039/6 "2021-10-24T16:06:00Z")

</div>

Hello,

I am sharing what I was testing:

```auto
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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/436b4a6a24bd9ddf6d7d5b0d00fc30572a88a3bb.png)

`:)`
