swing - Java paint class not displaying array of rectangles -


i'm trying print array of rectangles , getting errors on run time.

i send number main class ordinary int such 5 getdatafordisplay(the number send) function in paint class. checks in if statement know display rectangle. far working fine in program.

now saves in rectangle class , should display rectangles on run time?

also worth mentioning i'm learning site user posted on here, 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 jpanel looks here     }      public dimension getpreferredsize() {         return new dimension (1000, 1000);     }      public void getdatafordisplay (int proc)     {     //the method checks value "proc" see display rectangle on screen. proc comes 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);         (mainpanel.coloredrectangle cr : coloredrectangles)         {         g.setcolor(color.red );         rectangle r = cr.getrectangle();         g.drawrect(r.x, r.y, r.width, r.height);         }     }   } 

the error is:

exception in thread "awt-eventqueue-0" 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) @ sun.awt.sungraphicscallback.runonecomponent(sungraphicscallback.java:78) @ 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) @ java.security.accesscontroller.doprivileged(native method) @ 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) @ java.awt.event.invocationevent.dispatch(invocationevent.java:251) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:733) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:694) @ java.awt.eventqueue$3.run(eventqueue.java:692) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:703) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)

you never storing rectangle object in coloredrectangle objects. initializing variable, private rectangle rectangle; that's it. should edit code.

private rectangle rectangle;      public coloredrectangle(rectangle rectangle) {         this.rectangle = rectangle;         system.out.println("rec"); } 

and in getdatafordisplay method;

rectangle r = new rectangle(xcoor, ycoor, width, height); coloredrectangle cr = new coloredrectangle(r); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -