0

I am trying to learn Java and have created a GUI form that will eventually look like a Yahtzee score sheet. For now, it has all the fields, but when I try to run it, I get the following error:

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at Board.<init>(FiveDice.java:97)
    at FiveDice.main(FiveDice.java:9)

Ideas?

Here is my code:

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

public class FiveDice {

    public static void main(String[] args)
    {
        new Board();
    } // main
} // FiveDice

class Board extends JFrame {
    private static final long serialVersionUID = 1L;
    private JLabel  l_Ones, l_Twos, l_Threes, l_Fours, l_Fives, l_Sixes, 
                    l_SubtotalT, l_BonusT, l_TotalT;
    private JLabel  l_ThreeKind, l_FourKind, l_FullHouse, l_SmStr8, l_LgStr8, l_FiveDice,
                    l_Chance, l_SubtotalB, l_BonusB, l_GrandTotal;
    private JTextField  Ones, Twos, Threes, Fours, Fives, Sixes,
                        SubtotalT, BonusT, TotalT;
    private JTextField  ThreeKind, FourKind, FullHouse, SmStr8, LgStr8, FiveDice, 
                        Chance, SubtotalB, BonusB, GrandTotal;
    private JButton btnClear;

    public Board() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Six Dice!!");

        // Labels - Top Section
        l_Ones      = new JLabel("1s: ");
        l_Twos      = new JLabel("2s: ");
        l_Threes    = new JLabel("3s: ");
        l_Fours     = new JLabel("4s: ");
        l_Fives     = new JLabel("5s: ");
        l_Sixes     = new JLabel("6s: ");
        l_SubtotalT = new JLabel("Total: ");
        l_BonusT    = new JLabel("Bonus: ");
        l_TotalT    = new JLabel("Grand Total: ");

    // Input Fields - Top Section
        Ones        = new JTextField(2);
        Twos        = new JTextField(2);
        Threes      = new JTextField(2);
        Fours       = new JTextField(2);
        Fives       = new JTextField(2);
        Sixes       = new JTextField(2);
        SubtotalT   = new JTextField(3);
        BonusT      = new JTextField(3);
        TotalT      = new JTextField(3);

        // Labels - Bottom Section
        l_ThreeKind     = new JLabel("3 of a Kind: ");
        l_FourKind      = new JLabel("4 of a Kind: ");
        l_FullHouse     = new JLabel("Full House: ");
        l_SmStr8        = new JLabel("Sm. Straight: ");
        l_LgStr8        = new JLabel("Lg. Straight: ");
        l_FiveDice      = new JLabel("Five Dice: ");
        l_Chance        = new JLabel("Chance: ");
        l_SubtotalB     = new JLabel("Total: ");
        l_BonusB        = new JLabel("Bonus: ");
        l_GrandTotal    = new JLabel("Grand Total: ");

    // Input Fields - Bottom Section
        ThreeKind   = new JTextField(2);
        FourKind    = new JTextField(2);
        FullHouse   = new JTextField(2);
        SmStr8      = new JTextField(2);
        LgStr8      = new JTextField(2);
        FiveDice    = new JTextField(2);
        Chance      = new JTextField(2);
        SubtotalB   = new JTextField(3);
        BonusT      = new JTextField(3);
        GrandTotal  = new JTextField(3);

        // Add to Frame - Top Section
        add(l_Ones);        add(Ones);
        add(l_Twos);        add(Twos);
        add(l_Threes);      add(Threes);
        add(l_Fours);       add(Fours);
        add(l_Fives);       add(Fives);
        add(l_Sixes);       add(Sixes);
        add(l_SubtotalT);   add(SubtotalT);
        add(l_BonusT);      add(BonusT);
        add(l_TotalT);      add(TotalT);

        // Add to Frame - Bottom Section
        add(l_ThreeKind);   add(ThreeKind);
        add(l_FourKind);    add(FourKind);
        add(l_FullHouse);   add(FullHouse);
        add(l_SmStr8);      add(SmStr8);
        add(l_LgStr8);      add(LgStr8);
        add(l_FiveDice);    add(FiveDice);
        add(l_Chance);      add(Chance);
        add(l_SubtotalB);
        add(SubtotalB);
        add(l_BonusB);
        add(BonusB);
        add(l_GrandTotal);
        add(GrandTotal);

        // Buttons
        btnClear = new JButton( "Clear" );
        btnClear.addActionListener(
            new ActionListener()
            {
                public void actionPerformed( ActionEvent e )
                {
                    clear();
                }
            }); add( btnClear );

        setLayout( new FlowLayout() ); // Choose a layout
        pack(); // Get rid of extra space
        setLocationRelativeTo(null); // Centers the window
        setVisible(true);
  } // Board Method

    private void clear() {
        Ones.setText( "" );
        Twos.setText( "" );
        Threes.setText( "" );
        Fours.setText( "" );
        Fives.setText( "" );
        Sixes.setText( "" );
        SubtotalT.setText( "" );
        BonusT.setText( "" );
        TotalT.setText( "" );
        ThreeKind.setText( "" );
        FourKind.setText( "" );
        FullHouse.setText( "" );
        SmStr8.setText( "" );
        LgStr8.setText( "" );
        FiveDice.setText( "" );
        Chance.setText( "" );
        SubtotalB.setText( "" );
        BonusB.setText( "" );
        GrandTotal.setText( "" );
    } // clear
} // Board Class
6
  • The stack trace is telling you line 97 of <init> (The classes constructor) is throwing the NullPointerException. Check out that line and you'll have your answer Commented May 21, 2014 at 18:20
  • You're passing something wrong on this line: at Board.<init>(FiveDice.java:97). Commented May 21, 2014 at 18:21
  • Do you know what a NullPointerException is and when it happens? Commented May 21, 2014 at 18:21
  • 2
    In general, you should not, in C, C++, or Java, use names with LeadingUpperCase for variable names. Reserve LeadingUpperCase for class names, and use leadingLowerCase for variable names. As you can see above, the formatter thinks the LeadingUpperCase names are class names, and so will many of your readers. Commented May 21, 2014 at 18:24
  • Try to call super() ad the beginning od Board constructor Commented May 21, 2014 at 18:25

1 Answer 1

2

Line 97 is your problem. BonusB is null, so you cannot add it:

add(BonusB);
Sign up to request clarification or add additional context in comments.

4 Comments

But it is just an empty JTextField like many of the other ones that the compiler is accepting. In addition, when I remove it, the same thing happens on the next line. This happens on any and all of the following lines: add(l_SubtotalB); add(SubtotalB); add(l_BonusB); add(BonusB); add(l_GrandTotal); add(GrandTotal);
Actually, now it is only happening on the BonusB line. I do not understand what is different about that JTextField.
I got it figured out. It was a typo. I had to JTextFields named BonusT and none named BonusB. Thanks for the help.
You have to create the JTextField object before you can add it to your JFrame: BonusB = new JTextField(); add(BonusB);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.