我一直在我的代码中得到一个“线程中的异常”主“java.lang.NullPointerException”

时间:2015-11-08 23:23:58

标签: java image pixels

这个赋值要求getLuminance“将像素的亮度作为三个颜色通道值的平均值返回”,我认为我已将其设置正确,但由于某种原因在行中

while ( sunflower.hasNext() ) {

'hasNext'给出一个错误,上面写着“取消引用可能的空指针” 我不知道发生了什么。

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

也提供了一个需要Picture和Pixel的错误,但是getLuminance只需要一个像素,所以我不确定那里发生了什么。

    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
}

0 个答案:

没有答案