1

This assignment is asking for getLuminance to "return the luminance of a Pixel as the average of the three color channel values" and I think i have it set up right, but for some reason in the line

while ( sunflower.hasNext() ) {

'hasNext' is giving an error that says "Dereferencing possible null pointer" I have no idea what is going on.

the line

n = (int) TARGET/getLuminance(p);

is also giving an error that is requiring a Picture and a Pixel, but getLuminance only requires a pixel so I'm not sure what's happening there as well.

    public Luminance() {
    Picture  sunflower;        // picture to be modified
    sunflower = new Picture();
    display = new PictureDisplayer(sunflower);
    display.waitForUser();
    setLuminance(sunflower, 127.5);
    display.close();
    System.exit(0);
}

    private int getLuminance (Pixel p) {
       Picture sunflower = null;
       int   r;  // red value of pixel
       int   g;  // green value of pixel
       int   b;  // blue value of pixel
       int   v = 0;  // average
          while ( sunflower.hasNext() ) {
             p = sunflower.next();
             r = p.getRed();
             g = p.getGreen();
             b = p.getBlue();
             v = (r + g + b)/3;
             clip (v);

    }
    return v;
}

  private int clip(int val){
    if (val <=255){
        return val;
    }
    else {
        return 255;
    }
}

private void setLuminance(Picture sunflower, double TARGET) {
    Pixel p;
    int   r;  // red value of pixel
    int   g;  // green value of pixel
    int   b;  // blue value of pixel
    int   n;    
    //getLuminance(sunflower);
    while ( sunflower.hasNext() ) {
        p = sunflower.next();
        r = p.getRed(); 
        g = p.getGreen(); 
        b = p.getBlue(); 
        n = (int) TARGET/getLuminance(p); 
        p.setRed(n*r); 
        p.setGreen(n*g); 
        p.setBlue(n*b); 

        }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {Luminance r = new Luminance();
    // TODO code application logic here
}
Bruce P
  • 19,995
  • 8
  • 63
  • 73
alnmod
  • 13
  • 5
  • The function getLuminance is setting `sunflower = null` and then after initializing a few other variables you try to reference `sunflower.hasNext`. But `sunflower` is still null, hence the error. You're effectively trying to invoke `null.hasNext()` but that doesn't exist. – Bruce P Nov 08 '15 at 23:43
  • if I do that, the line while ( sunflower.hasNext() ) { will say that "sunflower" is not initialized – alnmod Nov 09 '15 at 01:23

0 Answers0