Reputation: 91
I have to create a battleship game where there is a multidimensional array of JButtons to denote the player grid. I am supposed to initialize the JButtons in the main UI class but they need to be declared in a separate class holding the player grid. I have tried this to add the buttons:
for(int i = 0; i <= 10; i++){
for(int j = 0; j <= 10; j++){
playerOnePanel.add(playerOne.getBoard());
}
}
But this returns a null pointer exception. The class I'm trying to reference is:
public class Player {
private Color shipColor;
private String userName;
private boolean isFirst;
private JButton[][] buttonBoard = new JButton[rows][cols];
private final static int rows = 10;
private final static int cols = 10;
public Player(String name){
initComponents();
}
private void initComponents(){
for(int i = 0; i <= rows; i++){
for(int j = 0; j <= cols; j++){
buttonBoard[i][j] = new JButton();
}
}
}
public JButton[][] getBoard(){
return buttonBoard;
}
}
And the code I want to use the object in is:
public class BattleshipUI extends JFrame {
private JMenuBar menuBar;
private JMenu gameMenu;
private JMenu optionMenu;
private JMenuItem playerPlayer;
private JMenuItem playerComputer;
private JMenuItem computerComputer;
private JMenuItem exit;
private JMenuItem game;
private JMenuItem player;
private JButton deploy;
private JPanel shipLayoutPanel;
private JPanel playerOnePanel;
private JComboBox shipCb;
private JComboBox directionCb;
// Data arrays for various components on the UI
private String[] rowLetters = {" ","A","B","C","D","E","F","G","H","I","J"};
private String[] columnNumbers = {" ","1","2","3","4","5","6","7","8","9","10"};
private String[] ships = {"Carrier","Battleship","Submarine","Destroyer", "Patrol Boat"};
private String[] direction = {"Horizontal","Vertical"};
private static final int PLAYER_ONE = 0;
private static final int PLAYER_TWO = 1;
private Player playerOne;
private Player playerTwo;
private Player[] players = new Player[2];
private Color[] color = {Color.cyan, Color.green, Color.yellow, Color.magenta, Color.pink, Color.red, Color.white};
public BattleshipUI(){
initComponents();
initObjects();
}
public BattleshipUI getThisParent(){
return this;
}
private void initObjects(){
playerOne = new Player("Player One");
playerTwo = new Player("Player Two");
players[1] = playerOne;
players[2] = playerTwo;
}
private void initComponents(){
this.setTitle("Battleship");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(500,500));
this.setMinimumSize(new Dimension(500,500));
menuBar = new JMenuBar();
gameMenu = new JMenu("Game");
optionMenu = new JMenu("Option");
menuBar.add(gameMenu);
menuBar.add(optionMenu);
playerPlayer = new JMenuItem("Player vs. Player");
playerComputer = new JMenuItem("Player vs. Computer");
computerComputer = new JMenuItem("Computer vs. Computer");
exit = new JMenuItem("Exit");
gameMenu.add(playerPlayer);
gameMenu.add(playerComputer);
gameMenu.add(computerComputer);
gameMenu.add(exit);
playerPlayer.setEnabled(false);
computerComputer.setEnabled(false);
game = new JMenuItem("Game Options");
player = new JMenuItem("Player Options");
optionMenu.add(game);
optionMenu.add(player);
shipLayoutPanel = new JPanel();
shipLayoutPanel.setBorder(BorderFactory.createTitledBorder("Select Ship and Direction"));
shipCb = new JComboBox(ships);
directionCb = new JComboBox(direction);
deploy = new JButton("DEPLOY");
deploy.setEnabled(false);
shipLayoutPanel.add(shipCb);
shipLayoutPanel.add(directionCb);
shipLayoutPanel.add(deploy);
playerOnePanel = new JPanel(new GridLayout(11,11));
playerOnePanel.setMinimumSize(new Dimension(400,400));
playerOnePanel.setPreferredSize(new Dimension(400,400));
playerOnePanel.setBorder(BorderFactory.createTitledBorder("Player One"));
for(int i = 0; i <= 10; i++){
for(int j = 0; j <= 10; j++){
playerOnePanel.add(playerOne.getBoard());
}
}
this.setJMenuBar(menuBar);
this.add(shipLayoutPanel, BorderLayout.NORTH);
this.add(playerOnePanel, BorderLayout.WEST);
this.setVisible(true);
}
}
Is there a way to do this? I need to use the methods and variables listed.
Upvotes: 0
Views: 254
Reputation: 11
Another thing I see is: You are trying to add a multi-dimensional JButton-Array at once to the JPanel. I think this will not work (please correct me if i am wrong). You have to do this Button by Button:
JButton[][] board = playerOne.getBoard();
for(int i=0;i<rowSize;i++){
for(int j=0;j<columnSize;j++){
playerOnePanel.add(board[i][j]);
}
}
Notice: Obviously you have to get somehow the row size and column size and declare as a variable as here as rowSize
and columnSize
.
Upvotes: 1
Reputation: 1606
You seem to initialize the players (and their boards respectively) after trying to add their board to a panel, which is impossible since you did not create the players yet.
public BattleshipUI(){
initComponents(); //Here you try to add the board of a player
initObjects(); //Here you initialize the players (ergo nullpointer)
}
private void initComponents(){
for(int i = 0; i <= 10; i++){
for(int j = 0; j <= 10; j++){
//playerOne is not instantiated yet
playerOnePanel.add(**playerOne**.getBoard());
}
}
}
private void initObjects(){
**playerOne** = new Player("Player One");
playerTwo = new Player("Player Two");
players[1] = playerOne;
players[2] = playerTwo;
}
Upvotes: 1