0

I'm tring to write a method the creates an empty rectangle inside which there is another white rectangle surrounded by a pattern of lines (contained in the for loop).
Whenever the method is run I get a null point exception on first line of the loop.
I can't work out what's going on... please help.

   public void displayArea () {
            Graphics g = chartPanel.getGraphics();
            int Xpos = 30;
            int Ypos = 50;
            int displayWidth = 300;
            int displayHeight = 280;
            int borderPatternY = 10;
            int borderPatternX = 10; 

            for (int count = 0; borderPatternY <= 300 || borderPatternX <= 280; count++){
                g.drawLine(30, borderPatternY, 330, borderPatternY);
                g.drawLine(borderPatternX, 50, borderPatternX, 320);

                borderPatternX = borderPatternX + 10;
                borderPatternY = borderPatternY + 10;
            }

            g.setColor(Color.white);
            g.fillRect(Xpos + 10, Ypos + 10, displayWidth-10, displayHeight -10);

            g.setColor(Color.black);
            g.drawLine(Xpos, 60, Xpos + displayWidth, 60);
            g.drawLine(Xpos, 120, Xpos + displayWidth, 120);
            g.drawLine(Xpos, 180, Xpos + displayWidth, 180);
            g.drawLine(Xpos, 240, Xpos + displayWidth, 240);
        }
c0der
  • 15,550
  • 6
  • 26
  • 53
James Palmer
  • 47
  • 1
  • 10
  • 1
    Learn to use a built in debugger (e.g. netbeans). chartPanel is null if its the first line or getGraphics() throws a nully (less likely). – adamcooney Oct 25 '17 at 09:14
  • Change you question title to a meaning title instead of this one. ;) – Alex Oct 25 '17 at 09:18
  • Welcome to SO. Please mark with comment in the code the line which throws null point exception. For example `g.drawLine(30, borderPatternY, 330, borderPatternY); //throws npe ` . p.s : `System.out.println(chartPanel);` and `System.out.println(chartPanel.getGraphics());` will reveal all you need to know. – c0der Oct 25 '17 at 09:23
  • Don't use getGraphics to do drawing. Instead read and follow the tutorials. Note that this has been asked many times on this site, and duplicates are not hard to find. – Hovercraft Full Of Eels Oct 25 '17 at 09:32
  • thanks folks I've worked it out. – James Palmer Oct 26 '17 at 09:51

0 Answers0