The method paint(Graphics g) of JComponent does not see the field

44 views Asked by At

I have an array of objects as the field of one of my classes public UserSquare[] userSquares = new UserSquare[8]; When i handle whith this array in any regular method like

public void showSquares() {
    for(UserSquare square : userSquares) {
        System.out.println(square.x);
    }
}

then it outputs what i need)

0,413,87,87
87,413,87,87
174,413,87,87
261,413,87,87
348,413,87,87
435,413,87,87
522,413,87,87
609,413,87,87

But when i do the same in the method paint(Graphics g)

public void paint(Graphics g) {
    for(UserSquare sq : userSquares) {
        System.out.println(sq.x+","+sq.y+","+sq.width+","+sq.height);
    }
}

It does not see the array userSquares and throws the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
0

There are 0 answers