2darray of objects (셀)의 위치가 비어 있는지 컨설팅을하는 동안 왜 NPE를 얻는 지 모르겠습니다. 나는 if ((matrix[i][j]) == null)
에 의하여 이것을 검사하고 나는 if ((matrix[i][j]) != null)
를 시도하고 그러나 메신저 NPE를 몇번이고 얻는. 나는왜이 경우에 NullPointerException이 발생합니까?
public class 2darrayofcells {
//rows and columns are final by requirement
private final int rows;
private final int colums;
private Cell[][] matrix;
//the array needs an initial object
private Cell initialCell;
//Position class consist on 2 integers (x,y)
public 2darrayofcell(Position pos){
//the arg pos is used to create the initial cell in the specified position of the array
rows = 10;
columns = 10;
matrix = new Cell[rows][columns];
initialCell = new Cell();
matrix[pos.getX()][pos.getY()] = initialCell;
}
. . .
//Here i put a cell in a position of the array but before putting it
//i need to know if it's empty. It will return true if the cell is added to
//that position
public boolean putCell(Cell cell, Position pos){
//Null Pointer Exception
if (matrix[pos.getX()][pos.getY()] == null) {
//Do stuff
return true;
}
return false;
}
}
메인 클래스 사용자의 통화에서
매트릭스는 초기화를 가지고public static void main(String[] args) {
Position pos = new Position(5,5);
2darrayofcells test = new 2darrayofcells(pos);
//test only has an object in 5,5
//The position for the cell i want to insert (3,3) is empty
Position cellPos = new Position(3,3);
Cell testCell = new Cell();
test.putCell(testCell, cellpos);
}
...
코드를 컴파일 할 수 있습니까? Cell/Position 클래스의 정의는 무엇입니까? – SMA
자바가 클래스 이름을 2darrayofcells 클래스 이름과 충돌하는 숫자 문자로 시작하도록하고 클래스의 생성자 (public 2darrayofcell (Position pos))가 잘못된 서명을 가지고 있다고 생각하지 않아서 코드를 컴파일 할 수 있습니까? 아마도 ' 이름 끝. – Himanshu
@almasshaikh 코드가 컴파일되고, funcionalities를 시도 할 때 문제가 발생했습니다. 셀은 2 정수를 포함합니다. – Jan