Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                    
                
        

我一直在使用bookClasses组的类来处理图像,并且在尝试去除图像中的红眼时收到NullPointerException错误。这是代码:

首先是Picture.Java类内部的removeRedEye方法:

 public void removeRedEye(int startX, int startY, int endX, int endY, Color newColor){

    Pixel pixel = null;

    for (int x = startX; x < endX; x++){
        for (int y = startY; y < endY; y++){
            if (pixel.colorDistance(Color.RED) < 167){
                        pixel.setColor(newColor);
            }
        }
    }
  }
}


和测试类别:

public class TestRemoveRedEye{

    public static void main(String[] args){

        String fileName = FileChooser.getMediaPath("//jenny-red.jpg");

        Picture jennyPicture = new Picture(fileName);

        jennyPicture.removeRedEye(109,91,202,107,java.awt.Color.BLACK);

        jennyPicture.explore();

    }
}


如果有人可以提出建议,说明我的程序为何不起作用,将不胜感激。

这些行在错误中被选中:
从removeRedEye方法中if (pixel.colorDistance(Color.RED) < 167){

测试班的jennyPicture.removeRedEye(109,91,202,107,java.awt.Color.BLACK);

最佳答案

pixel为null,需要先对其进行初始化,然后再调用其引用上的方法。

Pixel pixel = null;// neew to initialize this.
pixel = new Pixel(); // somethin like this
for (int x = startX; x < endX; x++){
    for (int y = startY; y < endY; y++){
        if (pixel.colorDistance(Color.RED) < 167){

10-02 11:20