why do i get this error:
Exception in thread "main" java.lang.NullPointerException
at view.ChessBoardView.<init>(ChessBoardView.java:38)
at controller.Controller.makeFrame(Controller.java:43 )
at controller.Controller.<init>(Controller.java:24)
at controller.Main.main(Main.java:9)
i am really sure i made all the objects
package view;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import controller.Controller;
import model.ChessBoard;
import model.Square;
public class ChessBoardView extends JPanel
{
private final static ImageIcon DARK_BROWN = new ImageIcon
(ChessBoardView.class.getResource("assets/sqb.gif"));
private final static ImageIcon LIGHT_BROWN = new ImageIcon
(ChessBoardView.class.getResource("assets/sqw.gif"));;
private ChessBoard chessboard;
public ChessBoardView (Controller controller)
{
chessboard = new ChessBoard();
Dimension boardSize = new Dimension(200, 200);
setLayout( new GridLayout(8, 8) );
setPreferredSize( boardSize );
setBounds(10, 10, boardSize.width, boardSize.height);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
JPanel board = new JPanel(new BorderLayout());
super.add(board);
for (Square square: chessboard.getAllSquares()){
if(square.getColor().equals("BROWN")){
board.add(new JLabel (DARK_BROWN));
}
else if(square.getColor().equals("WHITE")){
board.add(new JLabel (LIGHT_BROWN));
}
}
}
}
the problem is when i run my program i looks for the main, it tries to start the program i see the java icon it quits after 3 sec and in eclipse it shows the error written above
for (Square square: chessboard.getAllSquares()){
if(square.getColor().equals("BROWN")){
board.add(new JLabel (DARK_BROWN));
}
in the for-loop the chessboard.getAllSquares() return an ArrayList<Square> with all squares but why is it keep saying
NullPointerException
Exception in thread "main" java.lang.NullPointerException
at view.ChessBoardView.<init>(ChessBoardView.java:38)
at controller.Controller.makeFrame(Controller.java:43 )
at controller.Controller.<init>(Controller.java:24)
at controller.Main.main(Main.java:9)