0

I'm trying to print an array of rectangles and getting errors on run time.

I send a number from my main class just an ordinary int such as 5 to my getdatafordisplay(the number I send) function in my paint class. This does some checks in a if statement so we know where to display the rectangle. so far this is working fine in my program.

Now it then saves it in the rectangle class and this should then display all the rectangles on run time?

Also worth mentioning I'm learning from this site that a user posted on here, the active method: https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/

My paint class is:

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);
        }
    }  
}

The error I get is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mainPanel.paintComponent(main.java:275)
at javax.swing.JComponent.paint(JComponent.java:1045)
at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JLayeredPane.paint(JLayeredPane.java:585) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295) at javax.swing.RepaintManager.paint(RepaintManager.java:1249) at javax.swing.JComponent.paint(JComponent.java:1031) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115) at java.awt.Container.paint(Container.java:1967) at java.awt.Window.paint(Window.java:3877) at javax.swing.RepaintManager$3.run(RepaintManager.java:819) at javax.swing.RepaintManager$3.run(RepaintManager.java:796) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718) at javax.swing.RepaintManager.access$1100(RepaintManager.java:62) at 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

2 Answers 2

4

You are never actually storing a Rectangle object in your ColoredRectangle objects. You are initializing a variable, private Rectangle rectangle; but that's it. You should edit your code.

private Rectangle rectangle;

    public ColoredRectangle(Rectangle rectangle) {
        this.rectangle = rectangle;
}

And in the getDataForDisplay method;

Rectangle r = new Rectangle(xCoor, yCoor, width, height);
ColoredRectangle cr = new ColoredRectangle(r);
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to connect Rectangle with ColoredRectange, for that the NPE, when you try to access one of rectangle's attributes.

The (quick & dirty) fix would be:

Rectangle r = new Rectangle(xCoor, yCoor, width, height);
ColoredRectangle cr = new ColoredRectangle();
cr.rectangle = r; //<-- adding this line
addRectangle( cr );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.