The problem:
- making an array of objects
- as soon as I click on the GUI the array is set to null
- I traced the code and saw that my array is being set up properly
Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Puzzle1 extends JPanel{
public Square[] sq;
public String zero = "16";
public class Square{
public int realPos;
public int curPos;
}
public String setAdjacent(String curZero, int z){
String adj = "-1";
int zeroInt = Integer.parseInt(curZero);
if(z==0){
adj = Integer.toString(zeroInt+1);
if(zeroInt+1 == 17 || zeroInt+1 == 13 || zeroInt+1 == 9 || zeroInt+1 == 5){
adj = "-1";
}
}
if(z==1){
adj = Integer.toString(zeroInt-1);
if(zeroInt-1 == 0|| zeroInt-1 == 4 || zeroInt-1 == 8 || zeroInt-1 == 12){
adj = "-1";
}
}
if(z==2){
adj = Integer.toString(zeroInt+4);
if(zeroInt+4 > 16){
adj = "-1";
}
}
if(z==3){
adj = Integer.toString(zeroInt-4);
if(zeroInt-4 < 1){
adj = "-1";
}
}
return adj;
}
public static void buildGUI(){
JFrame frame = new JFrame("Puzzle");
Puzzle1 panel = new Puzzle1();
GridLayout mainLayout = new GridLayout(1, 1);
panel.setPreferredSize(new Dimension(300, 300));
panel.setMinimumSize(new Dimension(300, 300));
panel.setMaximumSize(new Dimension(300, 300));
frame.setResizable(false);
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
Puzzle1 p1 = new Puzzle1();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p1.setName("1");
panel.setLayout(mainLayout);
p1.add(new JLabel("1"));
panel.add(p1);
frame.setContentPane(panel);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes thread on close.
frame.setVisible(true);
}
public void clicked(JPanel pnlClick){
String adj[] = new String[4];
for(int z=0; z<4; z++){
adj[z] = setAdjacent(zero, z);
}
String clicked = pnlClick.getName();
for(int i = 0; i<4; i++){
String stAdj = adj[i];
System.out.println(stAdj);
int clkd = Integer.parseInt(clicked);
sq[clkd].curPos = 0;
System.out.println("clicked and adj");
}
}
class MouseEventHandler implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
JPanel pnlClick = (JPanel)(e.getSource());
clicked(pnlClick);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}
public Puzzle1() {
MouseEventHandler handler = new MouseEventHandler();
this.addMouseListener(handler);
}
public void createSquares(){
sq = new Square[16];
for(int j = 0; j < 16; j++){
sq[j] = new Square();
}
for(int i = 0; i < 16; i++){
if(i == 0){
sq[i].realPos = 16;
sq[i].curPos = 0;
}
else{
sq[i].realPos = i;
sq[i].curPos = i;
}
}
}
public static void main(String[] args){
Puzzle1 Puzzle1 = new Puzzle1();
Puzzle1.buildGUI();
Puzzle1.createSquares();
}
}
I try to break anywhere the ‘sq’ variable might be erased once I click but the debugger always goes straight to the mouseClicked event and the debugger shows ‘sq’ is null.
Call createSquares in your Puzzle1 constructor before the call to addMouseListener