0

So this is my code:

import javax.swing.*;
import java.awt.*;

public class ShapeTest extends JFrame{
     public ShapeTest(){
          setSize(600,600);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocationRelativeTo(null);
          setVisible(true);
     }

     public static void main(String a[]){
         new ShapeTest();
     }

     public void paint(Graphics g){
          Rectangle[][] v = new Rectangle[200][200];
          v[1][1].x=0;
          v[1][1].y=0;
          v[1][1].width=50;
          v[1][1].height=50;
          int y1=50;
          for(int i=1; i<=7; i++){
              int cont=50;
              for(int j=1; j<=7; j++){
                  v[i][j].x+=cont;
                  v[i][j].y=y1;
                  cont+=70;
              }
              y1+=70;
          }
          for(int i=1; i<=7; i++){
              for(int j=1; j<=7; j++){
                  g.drawRect(v[i][j].x, v[i][j].y, v[i][j].width, v[i][j].height);
                  g.setColor(Color.yellow);
                  g.fillRect(v[i][j].x, v[i][j].y, v[i][j].width, v[i][j].height);
              }
          }

          }
    }

and this is the sub-class:

public class Rectangle{
    public int x;
    public int y;
    public int width;
    public int height;
}

When I run the code, no rectangles are displayed, but i get a bunch of errors inside a text box:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ShapeTest.paint(ShapeTest.java:18)


this is the only error that is in red and I have no idea why it appears. I tried the other day running the code without arrays and it worked so I believe that might be the problem.

  • Just to be sure, do you know that if an array has 7 cells they are numbered 0 to 6? `for(int i=1; i<=7; i++)` seems peculiar. Usually you would have for(int i=0; i<7; i++). Same thing for `v[1][1]`, usually the 'first' cell is `v[0][0]`. – kabanus Dec 31 '17 at 11:49
  • @kabanus yes but my original array has 200*200 values and I always like to start from 1 in order to not mess with the zeros, so basically if I start from 1 and go to 7 included I'm going to have 7 cells anyway. – Whitewolf3131 Dec 31 '17 at 11:53
  • When the line of the error is specified, please add a comment on the line in your post to allow us to know where it is – azro Dec 31 '17 at 11:54
  • Duplicate of [NullPointerException when Creating an Array of objects](https://stackoverflow.com/q/1922677) – Bernhard Barker Dec 31 '17 at 15:09

1 Answers1

0

I guess line 18 is where you call

v[1][1].x=0;

You forgot to initiate the Rectangle class. After the array creation

Rectangle[][] v = new Rectangle[200][200];

you need to write

for(int i=0;i<200;i++) {
    for(int j=0;j<200;j++) {
        v[i][j] = new Rectangle();
    }
}
The_Programmer
  • 186
  • 3
  • 12
  • Okay that seemed to be the problem. I usually code in c++ and I had no idea that in java you have to initiate each time the Rectangle class. Thanks a lot! – Whitewolf3131 Dec 31 '17 at 12:08