# Coding problem NullPointerException

**URL:** <https://discourse.processing.org/t/coding-problem-nullpointerexception/22551>\
**Category:** Coding Questions\
**Created:** [July 11, 2020, 3:46pm UTC](https://discourse.processing.org/t/coding-problem-nullpointerexception/22551 "2020-07-11T15:46:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christiank79](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/christiank79/32/11861_2.png) [@christiank79](https://discourse.processing.org/u/christiank79)\
**Post date:** [July 11, 2020, 3:46pm UTC](https://discourse.processing.org/t/coding-problem-nullpointerexception/22551/1 "2020-07-11T15:46:57Z")

</div>

I write a code who change a picture in Dithering,but show me a NullPointerException (line 16)  
What can I do? I have update Processing 3.5.4:

This is coding:

```auto
//variable to store image
import processing.svg.*;
PImage theImageCarrier; 

void setup(){
  
  //size of the output window displaying the image
  size(1100 ,512, SVG, "filename.svg"); 
  
  //loading the image into the variable
  theImageCarrier = loadImage("pics.jpg"); 
  
  //theImageCarrier.filter(GRAY); //turning the image into gray scale 
  
  //displaying the image on the left half of output window
  image(theImageCarrier,0,0); // here is NullPointerException
  
}

int index(int x, int y){
   return x + y * theImageCarrier.width; 
}

void draw() {
  //before working on pixels u load them
  theImageCarrier.loadPixels();
  
  //look at all of the pixels

  for(int y=0; y< theImageCarrier.height-1; y++)//all y values of the image
  {  
     for(int x=1; x<theImageCarrier.width-1; x++) //all x values of the image
     {
       //look at a given pixel from the image
       color pix = theImageCarrier.pixels[index(x,y)];
       
       //pull out the red,green and blue value from the pixel
       float oldR = red(pix);
       float oldG = green(pix);
       float oldB = blue(pix);
       
      //factor is the number of colors available in your color palet
      //using which u color your image.
      //eg if factor = 1, then there are 2 possible values 0 and 1.
      //Hence the number of colors in your color palet of each type ( ie R,G and B) are only 2.
      int factor = 1; 
      
      //quantise the values to a shorter range of values
      int newR = round(factor * oldR/255) * (255/factor);
      int newG = round(factor * oldG/255) * (255/factor);
      int newB = round(factor * oldB/255) * (255/factor);
      
      //set it back to that pixel
      theImageCarrier.pixels[index(x,y)] = color(newR,newG,newB);
      
   //Dithering algo for pixel correction begins from here
      
      //errors for each color after quantization 
      float errR = oldR - newR;
      float errG = oldG - newG;
      float errB = oldB - newB;
      
      
      //case1
      int index = index(x+1,y);
      color c = theImageCarrier.pixels[index];
      float r = red(c);
      float g = green(c);
      float b = blue(c);
      //pass the error onto the current color and then update the pixels
      r = r + errR * 7/16.0;
      g = g + errG * 7/16.0;
      b = b + errB * 7/16.0;
      theImageCarrier.pixels[index] = color(r,g,b);
      
      //case2
      index = index(x-1,y+1);
      c = theImageCarrier.pixels[index];
      r = red(c);
      g = green(c);
      b = blue(c);
      //pass the error onto the current color and then update the pixels
      r = r + errR * 7/16.0;
      g = g + errG * 7/16.0;
      b = b + errB * 7/16.0;
      theImageCarrier.pixels[index] = color(r,g,b);
      
      
      //case3
      index = index(x,y+1);
      c = theImageCarrier.pixels[index];
      r = red(c);
      g = green(c);
      b = blue(c);
      //pass the error onto the current color and then update the pixels
      r = r + errR * 3/16.0;
      g = g + errG * 3/16.0;
      b = b + errB * 3/16.0;
      theImageCarrier.pixels[index] = color(r,g,b);
      
      
      //case4
      index = index(x+1,y+1);
      c = theImageCarrier.pixels[index];
      r = red(c);
      g = green(c);
      b = blue(c);
      //pass the error onto the current color and then update the pixels
      r = r + errR * 5/16.0;
      g = g + errG * 5/16.0;
      b = b + errB * 5/16.0;
      theImageCarrier.pixels[index] = color(r,g,b);
      
   //Dithering algo ends here
      
     /*
      pixel[x + 1][y] := pixel[x + 1][y] + quant_error * 7 / 16
      pixel[x - 1][y + 1] := pixel[x - 1][y + 1] + quant_error * 3 / 16
      pixel[x][y + 1] := pixel[x][y + 1] + quant_error * 5 / 16
      pixel[x + 1][y + 1] := pixel[x + 1][y + 1] + quant_error * 1 / 16
     */
     }
  }
  
  //after working you update the image pixels
  theImageCarrier.updatePixels();
  
  //quantized image on the right
  image(theImageCarrier , 512,0);
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 11, 2020, 3:54pm UTC](https://discourse.processing.org/t/coding-problem-nullpointerexception/22551/2 "2020-07-11T15:54:56Z")

</div>

Maybe the image is not on the hard drive?

Name is case sensitive

---

<div class="post-metadata">

**Author:** ![christiank79](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/christiank79/32/11861_2.png) [@christiank79](https://discourse.processing.org/u/christiank79)\
**Post date:** [July 11, 2020, 4:07pm UTC](https://discourse.processing.org/t/coding-problem-nullpointerexception/22551/3 "2020-07-11T16:07:21Z")

</div>

Image is in a folder named Data.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 11, 2020, 4:47pm UTC](https://discourse.processing.org/t/coding-problem-nullpointerexception/22551/4 "2020-07-11T16:47:11Z")

</div>

Named Data or data?

Case sensitive too

image is named…jpg, not …JPG ?

---

<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:** [July 11, 2020, 5:56pm UTC](https://discourse.processing.org/t/coding-problem-nullpointerexception/22551/5 "2020-07-11T17:56:55Z")

</div>

Hello,

It works with the default renderer:

```auto
size(240, 120);

```

I used your image:

```auto
theImageCarrier = loadImage("7676_2.png"); 

```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/58d58706cfd44302706bdec7ea9ec08461bd550c.png)

`:)`

[https://processing.org/reference/size\_.html](https://processing.org/reference/size_.html)
