Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何访问getPixel中的阵列?_Java_Arrays_Methods_Pixel - Fatal编程技术网

Java 如何访问getPixel中的阵列?

Java 如何访问getPixel中的阵列?,java,arrays,methods,pixel,Java,Arrays,Methods,Pixel,我真的很喜欢这个。所以基本上,我试图检索我在setPixel的2d数组中分配的像素,但是当我尝试设置pixel=cp[x][y]时,它告诉我它是空的。任何帮助都将不胜感激 public class ColorFrame implements Frame { int width; int height; Pixel pixel; int x; int y; int i; int j; public ColorPixel cp[][]

我真的很喜欢这个。所以基本上,我试图检索我在setPixel的2d数组中分配的像素,但是当我尝试设置pixel=cp[x][y]时,它告诉我它是空的。任何帮助都将不胜感激

public class ColorFrame implements Frame {

    int width;
    int height;
    Pixel pixel;
    int x;
    int y;
    int i;
    int j;
    public ColorPixel cp[][] = new ColorPixel[500][500];
    ColorPixel init_color;

    public ColorFrame(int width, int height, ColorPixel init_color) {
        this.width = width;
        this.height = height;
        this.init_color = init_color;
        pixel = pixel;
    }

    public ColorFrame(int width, int height) {
        this.width = width;
        this.height = height;
        //init_color= new ColorPixel(.5,.5,.5);
        //new ColorFrame(width,height, init_color);
    }

    /*public static void main(String[] args) {
        // TODO Auto-generated method stub

    }*/
    @Override
    public int getWidth() {
        // TODO Auto-generated method stub
        return width;
    }

    @Override
    public int getHeight() {
        // TODO Auto-generated method stub
        return height;
    }

    @Override
    public Pixel getPixel(int x, int y) {
        pixel = cp[x][y];
        return pixel;
    }

    @Override
    public void setPixel(int x, int y, Pixel p) {
        // TODO Auto-generated method stub
        for (i = 0; i <= width; i++) {
            for (j = 0; j <= height; j++) {
                cp[i][j] = init_color;
            }
        }
        cp[width][height] = new ColorPixel(.5, .5, .5);
        //r++;
        //cp[x][y]=getPixel(x,y);
    }

}
公共类ColorFrame实现框架{
整数宽度;
内部高度;
像素;
int x;
int-y;
int i;
int j;
公共彩色像素cp[][]=新彩色像素[500][500];
彩色像素初始颜色;
公共色框(整数宽度、整数高度、ColorPixel初始颜色){
这个。宽度=宽度;
高度=高度;
this.init_color=init_color;
像素=像素;
}
公共色框(整数宽度、整数高度){
这个。宽度=宽度;
高度=高度;
//init_color=新的彩色像素(.5、.5、.5);
//新色框(宽度、高度、初始颜色);
}
/*公共静态void main(字符串[]args){
//TODO自动生成的方法存根
}*/
@凌驾
公共int getWidth(){
//TODO自动生成的方法存根
返回宽度;
}
@凌驾
公共整数getHeight(){
//TODO自动生成的方法存根
返回高度;
}
@凌驾
公共像素getPixel(整数x,整数y){
像素=cp[x][y];
返回像素;
}
@凌驾
公共无效设置像素(整数x,整数y,像素p){
//TODO自动生成的方法存根

对于(i=0;i我认为您得到了
NullPointerEception

将其放入构造函数
ColorFrame()


或者您可能被称为
pixel=cp[x][y];
before
setPixel()
function

此代码有一系列问题(未初始化的成员、构造函数中的pixel=pixel、成员可见性、数组),但这不是重点

首先,尝试将
final
放在
ColorPixel init\u color
之前,IDE会立即告诉您,当它保持未初始化状态时,有一种情况:您的第二个构造函数将它保留为null,因此如果使用它,整个数组将初始化为null

如果使用第一个构造函数,则只要
x
y
超出原始的
宽度
高度
且低于500,则在
getPixel
中仍然会得到null——您从不检查边界


如果你使用第一个构造函数,并且你的
x
y
在限制范围内,你应该会得到一个非空的结果(除非你在构造函数中没有通过init_color的null)。

谢谢!它仍然告诉我cp[][]当我尝试在getPixel方法中使用数组时,它是空的。我可以在setPixel中很好地访问数组。你知道为什么会这样吗?@user3240906对不起,我不理解你的评论:我给了你两个为什么它可以为空的原因,因为你的代码段没有更改。因此,同样的两个原因仍然有效,你尝试过吗解决这些问题,如果是,如何解决?
pixel = new Pixel(); // initializing pixel