gpt4 book ai didi

java - 运行 Java 代码时出错

转载 作者:行者123 更新时间:2023-12-02 05:48:32 25 4
gpt4 key购买 nike

我正在尝试学习 Java,并创建了一个 GUI 表单,最终看起来像 Yahtzee 评分表。目前,它具有所有字段,但是当我尝试运行它时,出现以下错误:

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)

想法?

这是我的代码:

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

最佳答案

第 97 行是你的问题。 BonusB 为空,因此您无法添加它:

add(BonusB);

关于java - 运行 Java 代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23790915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com