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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:10:31+00:00 2026-05-30T13:10:31+00:00

I am programming in Java and am still rather new. Experimenting with GUI Basics,

  • 0

I am programming in Java and am still rather new. Experimenting with GUI Basics, I decided to make a checkerboard with all 64 cells being instances of JButtons with a filled in background. Sadly, when I tried to compile the code, only two buttons show up and neither of them are colored. I’m trying to understand the problem, but for now it seems a bit beyond me.

package checkerboard;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class Checkerboard extends JFrame {

public Checkerboard() {

    JButton black = new JButton("B");
    JButton white = new JButton("W");

            black.setBackground(Color.BLACK);
            white.setBackground(Color.WHITE);



    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(8, 8));
    p1.setSize(600, 600);

    for (int i = 0; i < 8; i++) {
        if (i % 2 == 0) {
            for (int j = 0; j < 4; j++) {
                p1.add(black);
                p1.add(white);
            }
        } else {
            for (int j = 0; j < 4; j++) {
                p1.add(white);
                p1.add(black);
            }
        }
    }
    super.add(p1);
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Checkerboard frame = new Checkerboard();
    frame.setTitle("Checkers");
    frame.setSize(600, 600);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    }
}

In experimenting with the JPanel, for some strange reason line 27 had a NullPointer Exception.

  • 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-30T13:10:32+00:00Added an answer on May 30, 2026 at 1:10 pm

    This is because you are using only one instance object of JButton as a black button, and only one instance object of JButton as a white button, and you are adding the same 2 references to the JPanel p1 more and more. Thus, this won’t work and you need to create an object for each button on the board. Here is how you can do such thing:

    enter image description here

    JButton[] blackButtons = new JButton[4 * 8];
    JButton[] whiteButtons = new JButton[4 * 8];
    
    for(int i = 0; i < blackButtons.length; i++)
    {
        blackButtons[i] = new JButton("B");
        blackButtons[i].setBackground(Color.BLACK);
    }
    for(int i = 0; i < whiteButtons.length; i++)
    {
        whiteButtons[i] = new JButton("W");
        whiteButtons[i].setBackground(Color.WHITE);
    }
    

    then you can add them like this:

    for (int i = 0; i < 8; i++) {
        if (i % 2 == 0) {
            for (int j = 0; j < 4; j++) {
                p1.add(blackButtons[4 * i + j]);
                p1.add(whiteButtons[4 * i + j]);
            }
        } else {
            for (int j = 0; j < 4; j++) {
                p1.add(whiteButtons[4 * i + j]);
                p1.add(blackButtons[4 * i + j]);
            }
        }
    }
    
    add(p1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still rather new to Java programming, so maybe there is an obvious answer
First, i'm new at Java-programming and my native lang is not english, but still
i am new to java GUI programming. i am adding a JEditor pane (with
I'm fairly new in Android/Java programming, so some of the basic stuff is still
I'm still quite new to Android and Java programming and even newer to draw
I am still fairly new to Java programming and I was looking over a
I've been programming in JAVA and C all my years at Uni, but now
I have a background of programming in Java and C++, so I know all
I've been casually programming in Java for a while, but I still have a
I'm just getting to grips with GUI programming in java. Here is a trivial

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.