I'm trying to make a Scrabble game for a school project, but I'm stuck with a silly issue. The idea is that the board is made up out of 15x15 JButtons, and then this button is replaced with one sporting an image representing the letter the player chose to put there. Now I am getting a NullPointerException every time I try to change the JButton in any manner. I have tried different ways of accomplishing this, but I have not succeeded.
Any help would be GREATLY appreciated, as I'm seriously running out of time.
Thanks
(Please bear in mind that the program is in very early stages, and I am very new to java )
Here is my code:
Code: Select all
package scrabble;
public class Main {
public static void main(String[] args) {
Board board = new Board(true);
}
}
Code: Select all
package scrabble;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Player implements ActionListener {
private JFrame frame;
private Container con;
private JButton[] btnHand = new JButton[7];
private String[] alpha = new String[26];
private int btnX, btnY;
private Board board = new Board(false);
public Player() {
assignAplhabet();
}
private void assignAplhabet() {
int count = 0;
for (int asc = 65; asc < 91; asc++) {
alpha[count] = "" + (char) asc;
count++;
}
}
public void start() {
frame = new JFrame("PLAYER 1");
frame.setSize(400, 100);
frame.setLocation(0, 0);
con = frame.getContentPane();
con.setLayout(null);
con.setBackground(Color.LIGHT_GRAY);
for (int k = 0; k < 7; k++) {
int rndm = (int) (Math.random() * 26);
// btnP1[k] = new JButton(alpha[rndm]);
btnHand[k] = new JButton(new ImageIcon( alpha[rndm] + ".gif"));
btnHand[k].setBounds(10 + k * 52, 10, 47, 47);
con.add(btnHand[k]);
// btnHand[k].addActionListener(this);
}
frame.setVisible(true);
}
public void boardBtnClicked(int x, int y, int w, int h) {
btnX = x;
btnY = y;
frame.dispose();
frame.setLocation((w / 2) - 200, (h / 2) - 100);
frame.setTitle("Choose which letter to place.");
frame.show();
for (int r = 0; r < 7; r++) {
btnHand[r].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
String text = (String) e.getActionCommand();
board.placeLetter(btnX, btnY, text);
}
}
Code: Select all
package scrabble;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Board implements ActionListener {
private JFrame boardFrame;
private JLabel title;
private Container con;
private JButton[][] btnBlock = new JButton[15][15];
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private Gameplay gameplay;
private Color defaultColor;
private Player p1;
public Board(boolean first) {
if (first) {
createBoardFrame();
createBoardBtns();
//gameplay = new Gameplay();
p1 = new Player();
p1.start();
}
}
private void createBoardFrame() {
boardFrame = new JFrame("| SCRABBLE |");
boardFrame.setSize(screenSize.width, screenSize.height);
boardFrame.setLocation(0, 0);
boardFrame.setVisible(true);
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = boardFrame.getContentPane();
con.setLayout(null);
con.setBackground(Color.CYAN);
}
private void createBoardBtns() {
int wide = screenSize.width;
int high = screenSize.height;
for (int y = 0; y < 15; y++) {
for (int x = 0; x < 15; x++) {
btnBlock[x][y] = new JButton("");
btnBlock[x][y].setBounds(((wide - 780) / 2) + (x * 52), ((high - 780) / 2)
+ (y * 52), 47, 47);
btnBlock[x][y].addActionListener(this);
con.add(btnBlock[x][y]);
}
}
btnBlock[7][7].setBackground(Color.RED);
title = new JLabel("SCRABBLE", JLabel.CENTER);
title.setBounds((wide / 2) - 375, 0, 750, 50);
title.setFont(new Font("Serif", Font.BOLD, 48));
title.setForeground(Color.BLACK);
con.add(title);
boardFrame.setVisible(true);
defaultColor = btnBlock[0][0].getBackground();
}
public void placeLetter(int xx, int yy, String letter) {
con.remove(btnBlock[xx][yy]); // <<<<<---- THE ERROR MESSAGE POINTS HERE
btnBlock[xx][yy] = new JButton(new ImageIcon(letter + ".gif"));
btnBlock[xx][yy].setBounds(((screenSize.width - 780) / 2) + (xx * 52),
((screenSize.height - 780) / 2) + (yy * 52), 47, 47);
btnBlock[xx][yy].addActionListener(this);
con.add(btnBlock[xx][yy]);
}
public void actionPerformed(ActionEvent e) {
for (int x = 0; x < 15; x++) {
for (int y = 0; y < 15; y++) {
if (e.getSource() == btnBlock[x][y]) {
p1.boardBtnClicked(x, y, screenSize.width, screenSize.height);
}
}
}
}
}