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 8072949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:10:57+00:00 2026-06-05T14:10:57+00:00

I’m terribly unfamiliar with swing, but here I’m trying to make a clacker game

  • 0

I’m terribly unfamiliar with swing, but here I’m trying to make a “clacker” game where each time two dice are rolled, either you click the two (or 1 if they’re the same number) buttons representing the two numbers or just one button representing the sum of the two numbers. If a button is clicked, visible is set to false. I have a JButton array to hold the 12 buttons, but every time a reference is made to the array buttons, it throws a NullPointerException. I’ve tried putting the code making the JButton array within the constructor and actionPerformed, but it still throws the same exception.

Here is the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Clacker implements ActionListener
{
    JPanel panel;
    JFrame frame;
    JLabel dieFace1, dieFace2, numTurns;
    JButton[] buttons;
    JButton rollDie, reset;
    String turn="Turns: ";
    int turns, numCovered;

public Clacker()
{
    frame = new JFrame("clacker");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel=new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(50,30,50,30));
    panel.setBackground(Color.gray);
    panel.setLayout(new GridLayout(5,10,1,1));

    dieFace1=new JLabel(new ImageIcon("die3.jpg"));
    dieFace1.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
    dieFace1.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
    panel.add(dieFace1);

    dieFace2=new JLabel(new ImageIcon("die3.jpg"));
    dieFace2.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
    dieFace2.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
    panel.add(dieFace2);

    rollDie=new JButton("Roll Die");
    rollDie.setAlignmentX(JButton.RIGHT_ALIGNMENT);
    rollDie.addActionListener(this);
    panel.add(rollDie);

    numTurns=new JLabel(turn+turns);
    numTurns.setAlignmentX(JLabel.LEFT_ALIGNMENT);
    numTurns.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    panel.add(numTurns);

    setArray();

    reset=new JButton("Reset");
    reset.setAlignmentX(JButton.LEFT_ALIGNMENT);
    reset.addActionListener(this);
    panel.add(reset);

    frame.setContentPane(panel);

    frame.pack();
    frame.setVisible(true);
}

public void setArray()
{
    JButton[] buttons = new JButton[12];
    for (int i=0; i<12; i++)
    {

        buttons[i]= new JButton(i+1+"");
        buttons[i].setActionCommand(i+"");
        buttons[i].addActionListener(this);
        panel.add(buttons[i]);
        buttons[i].setEnabled(false);
        System.out.println(buttons[i]); 
    }
}

public void actionPerformed(ActionEvent event)
{
    if (event.getActionCommand().equals("Roll Die"))
    {   
        Random asdf = new Random();
        int newRoll= asdf.nextInt(6)+1;
        dieFace1.setIcon(new ImageIcon("die"+newRoll+".jpg"));

        Random asdf2 = new Random();
        int newRoll2= asdf2.nextInt(6)+1;
        dieFace2.setIcon(new ImageIcon("die"+newRoll2+".jpg"));

        int sum=newRoll+newRoll2;
        turns++;
        numTurns.setText(turn+turns);

        for (int i=0; i<7; i++)                                    ////here                          
        {
            if (i==newRoll)
                buttons[i].setEnabled(true);
            if (i==newRoll2)
                buttons[i].setEnabled(true);
        }

        for (int i=7; i<13; i++)
        {
            if (sum==i)
                buttons[i].setEnabled(true);
        }

        String eventName = event.getActionCommand();
        for (int b=1; b<13; b++)
        {
        if (eventName.equals(buttons[b].getActionCommand()))       ////here            
            {
                if (Integer.parseInt(eventName)>6)
                {
                    buttons[b].setText("");
                    numCovered++;
                    buttons[b].setVisible(false);
                    for (int i=0; i<7; i++)
                        buttons[i].setEnabled(false);
                }
            }   
        }

        for (int i=0; i<buttons.length; i++)                       ////here                            
            buttons[i].setEnabled(false);

        if (numCovered==12)
        {
            rollDie.setEnabled(false);
            numTurns.setText("Win! "+turns+" turns");
        }

    }

    else if (event.getActionCommand().equals("Reset"))
    {
        turns=0;
        numTurns.setText(turn+turns);
        rollDie.setEnabled(true);
        numCovered=0;
        for (int i=0; i<buttons.length; i++)                       ////here                             
        {
            buttons[i].setVisible(true);
            buttons[i].setEnabled(false);
        }
    }
}

private static void runGUI()
{ JFrame.setDefaultLookAndFeelDecorated(true); Clacker game = new Clacker();}

public static void main(String[] args)
{ javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() { runGUI();}});}
}
  • 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-06-05T14:10:59+00:00Added an answer on June 5, 2026 at 2:10 pm

    In setArray you’re declaring a local variable instead of referencing the class member variable. Just change

    JButton[] buttons = new JButton[12];
    

    to

    this.buttons = new JButton[12];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.