0

I have the following test class Test_Retina that is testing a method in a class Retina called "seeBMPImage" by first retrieving a .bmp image. However I am getting a null pointer exception and I don't understand why because a image 66 by 66 pixels wide is named "2.bmp" and it is in the same package as classes "Retina.java" and "Test_Retina.java"

public class Test_Retina extends junit.framework.TestCase {
private Retina retina;

public void setUp() {
VisionCell[][] visionCells = new VisionCell[66][66];
// this.retina = new Retina(visionCells);
}

public void test_seeBMPImage() throws IOException {
this.retina.seeBMPImage("2.bmp"); <-- !!GETTING A NULLPOINTEREXCEPTION!!
// ...
}

}

public class Retina {
private VisionCell[][] visionCells;

public void seeBMPImage(String BMPFileName) throws IOException {
BufferedImage image = ImageIO.read(getClass().getResource(BMPFileName));
    int color = image.getRGB(1, 1);
if (color == Color.BLACK.getRGB()) {
    System.out.println("black");
    } else {
        System.out.println("white");
    }
}

}

4

2 回答 2

0

您需要为资源名称提供完全限定的包名称,即使该资源与您调用它的类位于同一包中。getClass().getResource() 和 this.getClass().getClassLoader().getResource(...) 本质上是一样的加载资源。所以如果你的资源在包test.p1.p2中,例如,你想使用资源名称“test/p1/p2/2.bmp”

于 2013-06-10T19:58:27.027 回答
0

取消注释该行:

// this.retina = new Retina(visionCells);

在设置方法中。

目前 this.retina 是null.

于 2013-06-11T07:48:49.833 回答