我一直在嘗試製作一個矩形數組來形成一個遊戲網格,並且由於某些原因,當矩形應該繪製時,我會得到幾個Nullpointer異常。我不知道這是否與我設置x,y共同角度,寬度,高度或其他東西的方式有關。通過我的測試,似乎這與它無關,但我可能是錯的。當繪製Rectangle2D數組時,Nullpointer異常
public class NewClass extends Frame {
Rectangle2D[][] rectangelMatrix = new Rectangle[4][5];
int width = 25;
int height = 25;
int x = 0;
int y = 0;
public NewClass() {
setSize(1000, 1000);
}
public void arraysi() {
for (int i = 0; i < rectangelMatrix.length; i++) {
y = y + height;
for (int j = 0; j < rectangelMatrix[i].length; j++) {
x = x + width;
rectangelMatrix[i][j] = new Rectangle(x, y, width, height);
}
}
}
public void paint(Graphics g) {
System.out.println("hi");
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < rectangelMatrix.length; i++) {
for (int j = 0; j < rectangelMatrix[i].length; j++) {
// EXCEPTION THROWN FROM LINE BELOW
g2d.draw(rectangelMatrix[i][j]);
}
}
}
public void coolios() {
repaint();
}
}
的方法是從塔主類名爲如下:
arraysi()
coolios()
編輯:這裏是日誌 -
exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.geom.Path2D$Float.<init>(Path2D.java:215)
at java.awt.geom.Path2D$Float.<init>(Path2D.java:190)
at sun.java2d.windows.GDIRenderer.doShape(GDIRenderer.java:204)
at sun.java2d.windows.GDIRenderer.draw(GDIRenderer.java:236)
at sun.java2d.pipe.ValidatePipe.draw(ValidatePipe.java:136)
at sun.java2d.SunGraphics2D.draw(SunGraphics2D.java:2336)
at NewClass.paint(NewClass.java:64)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
at sun.awt.windows.WWindowPeer.handleEvent(WWindowPeer.java:695)
at java.awt.Component.dispatchEventImpl(Component.java:4706)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
好的,拋出的nullpointerexception在哪裏? – Stultuske
顯示LogCat – elbaulp
首先格式化您的帖子,因爲有很多空的空間 – dharr