Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7578741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:30:06+00:00 2026-05-30T17:30:06+00:00

The problem: making an array of objects as soon as I click on the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:30:08+00:00Added an answer on May 30, 2026 at 5:30 pm

    Call createSquares in your Puzzle1 constructor before the call to addMouseListener

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making list of items in categories, problem is that item can be in
I have been asked to write a GUI that will allow objects to be
The problem: making a very simple style switcher with js (without inline event handling).
My only problem is making them line up three-across and have equal spacing. Apparently,
I've got a bit of a problem with making my data loading and filtering
I'm having some difficulties with the following problem: I'm making a little game where
I am making an application with Java Swing and i have a problem. I
currently im making some crawler script, one of problem is sometimes if i open
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
im making some script with mechanize.browser module. one of problem is all other thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.