我试图打印一个矩形数组并在运行时遇到错误。
我从我的主类中发送一个数字,只是一个普通的int,例如5,在我的paint类中的getdatafordisplay(the number I send)
函数。这在if语句中进行了一些检查,因此我们知道在哪里显示矩形。到目前为止,这在我的程序中运行良好。
现在它将它保存在矩形类中,然后应该在运行时显示所有矩形?
另外值得一提的是,我从这个网站上了解到用户在这里发布的主动方法:https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/
我的涂料类是:
class mainPanel extends JPanel
{
int processes, storedProcesses;
//for inital values of rectangles
int xCoor = 0;
int yCoor = 0;
int width = 10;
int height = 50;
static int x = 100;
int [] y = {100,150,200,250,300,350,400,450,500,550};
private ArrayList<ColoredRectangle> coloredRectangles = new ArrayList<ColoredRectangle>();
class ColoredRectangle
{
private Rectangle rectangle;
public ColoredRectangle()
{
System.out.println("REC");
}
public Rectangle getRectangle()
{
return rectangle;
}
}
public void addRectangle(ColoredRectangle rectangle)
{
coloredRectangles.add( rectangle );
repaint();
}
public mainPanel(int processFROMmain)
{
//just some jpanel looks here
}
public Dimension getPreferredSize() {
return new Dimension (1000, 1000);
}
public void getDataForDisplay (int proc)
{
//the method checks the value from "proc" to see where to display a rectangle on screen. proc comes from user i.e 5
int loop = 0;
while (loop < storedProcesses)
{
int breakloop = 0;
if (proc == loop)
{
xCoor = x;
yCoor = y[loop];
x = x + 10;
breakloop = 1;
Rectangle r = new Rectangle(xCoor, yCoor, width, height);
ColoredRectangle cr = new ColoredRectangle();
addRectangle( cr );
}
if (breakloop == 1)
{
break;
}
loop++;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (mainPanel.ColoredRectangle cr : coloredRectangles)
{
g.setColor(Color.RED );
Rectangle r = cr.getRectangle();
g.drawRect(r.x, r.y, r.width, r.height);
}
}
}
我得到的错误是:
线程中的异常&#34; AWT-EventQueue-0&#34;显示java.lang.NullPointerException
在mainPanel.paintComponent(main.java:275)
在javax.swing.JComponent.paint(JComponent.java:1045)
在javax.swing.JComponent.paintChildren(JComponent.java:878) 在javax.swing.JComponent.paint(JComponent.java:1054) 在javax.swing.JComponent.paintChildren(JComponent.java:878) 在javax.swing.JComponent.paint(JComponent.java:1054) 在javax.swing.JLayeredPane.paint(JLayeredPane.java:585) 在javax.swing.JComponent.paintChildren(JComponent.java:878) 在javax.swing.JComponent.paintToOffscreen(JComponent.java:5217) 在javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295) 在javax.swing.RepaintManager.paint(RepaintManager.java:1249) 在javax.swing.JComponent.paint(JComponent.java:1031) 在java.awt.GraphicsCallback $ PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115) 在java.awt.Container.paint(Container.java:1967) 在java.awt.Window.paint(Window.java:3877) 在javax.swing.RepaintManager $ 3.run(RepaintManager.java:819) 在javax.swing.RepaintManager $ 3.run(RepaintManager.java:796) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) 在javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769) 在javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718) 在javax.swing.RepaintManager.access $ 1100(RepaintManager.java:62) 在javax.swing.RepaintManager $ ProcessingRunnable.run(RepaintManager.java:1677) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) at java.awt.EventQueue.access $ 200(EventQueue.java:103) at java.awt.EventQueue $ 3.run(EventQueue.java:694) at java.awt.EventQueue $ 3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:703) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:4)
您永远不会在Rectangle
个对象中存储ColoredRectangle
个对象。您正在初始化变量private Rectangle rectangle;
,但这就是它。你应该编辑你的代码。
private Rectangle rectangle;
public ColoredRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
System.out.println("REC");
}
在getDataForDisplay
方法中;
Rectangle r = new Rectangle(xCoor, yCoor, width, height);
ColoredRectangle cr = new ColoredRectangle(r);
答案 1 :(得分:1)
当您尝试访问矩形属性之一时,忘记将Rectangle
与ColoredRectange
连接为NPE。
(快速和肮脏)修复将是:
Rectangle r = new Rectangle(xCoor, yCoor, width, height);
ColoredRectangle cr = new ColoredRectangle();
cr.rectangle = r; //<-- adding this line
addRectangle( cr );