【发布时间】:2015-03-17 18:07:20
【问题描述】:
为什么我不能从我的哈希表中正确检索?我创建了一个哈希表,其中包含键和值作为坐标类型(我创建的一个类)。然后我无法从我的坐标对象中检索 x 值。
public coordinates translateOffsetToPixel(int x, int y){
//Translate given coordinates to pixel coordinates for the cell
coordinates input = new coordinates(x,y);
coordinates outputPixelCoord;
Hashtable <coordinates, coordinates> table = new Hashtable<coordinates, coordinates>();
for (int r = 0 ; r<row; r++){
for(int c = 0; c< col; c++){
System.out.println("hit translation");
table.put(new coordinates(r,c), new coordinates(r*2,c*2));
}
}
outputPixelCoord = table.get(input);
System.out.println("outputX:" + outputPixelCoord.getX()); //ERROR
return outputPixelCoord;
}
坐标类:
public class coordinates {
private int x,y;
public coordinates(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {return x;}
public int getY() {return y;}
}
记录表:
03-17 13:55:53.690 1961-1961/com.example.sam.matrix D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
03-17 13:55:53.780 1961-1961/com.example.sam.matrix I/System.out﹕ hit board
03-17 13:55:53.780 1961-1961/com.example.sam.matrix I/System.out﹕ 5
03-17 13:55:53.780 1961-1961/com.example.sam.matrix I/System.out﹕ hit translation
03-17 13:55:53.780 1961-1961/com.example.sam.matrix E/InputEventReceiver﹕ Exception dispatching input event.
03-17 13:55:53.780 1961-1961/com.example.sam.matrix E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
03-17 13:55:53.800 1961-1961/com.example.sam.matrix E/MessageQueue-JNI﹕ java.lang.NullPointerException
【问题讨论】:
-
java.lang.NullPointerException
-
您永远不会在
table映射中添加一个条目,该映射以input作为键,这就是table.get(input)将返回 null 的原因。 -
这是输入表格的错误语句吗? table.put(新坐标(r,c), 新坐标(r*2,c*2));