如何在没有代码错误的情况下修复 .NullPointerException 错误?
How can i fix the .NullPointerException error without code errors?
很抱歉 post 是我第一次 post 访问 Whosebug。
我正在做我的家庭作业,它包括创建一个银行账户模拟、存款和退休,当我完成我的程序时,控制台向我显示:
Exception in thread "main" java.lang.NullPointerException
at java.desktop/java.awt.Container.addImpl(Container.java:1117)
at java.desktop/java.awt.Container.add(Container.java:436)
at Banco.createGUI(Banco.java:29)
at Banco.main(Banco.java:16)
Process finished with exit code 1
这是我的代码,我花了一些时间试图修复它,我找不到我的值为 null 的变量在哪里,这就是我寻求帮助的原因。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Banco extends JFrame implements ActionListener {
private JLabel label1, label2;
private JTextField Square1, Square2, Square3;
private JButton Button1, Button2;
private Account account;
public static void main(String[] args) {
Banco Ventana = new Banco();
Ventana.setSize(300,300);
Ventana.createGUI();
Ventana.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
label1 = new JLabel("Balance");
window.add(label1);
Square1 = new JTextField(5);
window.add(Square2);
label2 = new JLabel("State");
window.add(label2);
Square2 = new JTextField(9);
window.add(Square2);
Button1 = new JButton("Deposit");
Button1.addActionListener(this);
window.add(Button1);
Button2 = new JButton("Retire");
Button2.addActionListener(this);
window.add(Button2);
Square3 = new JTextField(5);
window.add(Square3);
account = new Account();
}
public void actionPerformed(ActionEvent event) {
int balance;
int Amount;
balance = account.getBalance();
Amount = Integer.parseInt(Square3.getText());
if(event.getSource() == Button1){
account.setBalance(balance);
account.depositBalance(Amount);
balance = account.getBalance();
}
if (event.getSource() == Button2){
account.setBalance(balance);
account.retireBalance(Amount);
balance = account.getBalance();
}
if (balance < 0){
Square2.setText("Overdrawn");
}
else {
Square2.setText("OK");
}
Square1.setText(Integer.toString(balance));
}
}
class Account{
int Balance=0;
public void setBalance(int Amount){
Balance = Amount;
}
public int getBalance(){
return Balance;
}
public void depositBalance(int Amount){
Balance = Balance + Amount;
}
public void retireBalance(int Amount){
Balance = Balance - Amount;
}
}
这看起来很可疑:
Square1 = new JTextField(5);
window.add(Square2);
您初始化 Square1
,但尝试将 Square2
添加到 window。
所以在堆栈跟踪中,它说
Exception in thread "main" java.lang.NullPointerException
at java.desktop/java.awt.Container.addImpl(Container.java:1117)
at java.desktop/java.awt.Container.add(Container.java:436)
at Banco.createGUI(Banco.java:29)
at Banco.main(Banco.java:16)
Process finished with exit code 1
您可以跳过前两个 at
,因为它不在您的代码中。
第三行说 (Banco.java:29)
这是您的代码中发生错误的地方。
检查该行,您在第 28 行中为 Square1
赋值,但您在第 34 行中用 Square2
调用 add()
。
这意味着,在第 29 行,Square2
的值为 null
,这就是为什么您得到 NullPointerException
很抱歉 post 是我第一次 post 访问 Whosebug。 我正在做我的家庭作业,它包括创建一个银行账户模拟、存款和退休,当我完成我的程序时,控制台向我显示:
Exception in thread "main" java.lang.NullPointerException
at java.desktop/java.awt.Container.addImpl(Container.java:1117)
at java.desktop/java.awt.Container.add(Container.java:436)
at Banco.createGUI(Banco.java:29)
at Banco.main(Banco.java:16)
Process finished with exit code 1
这是我的代码,我花了一些时间试图修复它,我找不到我的值为 null 的变量在哪里,这就是我寻求帮助的原因。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Banco extends JFrame implements ActionListener {
private JLabel label1, label2;
private JTextField Square1, Square2, Square3;
private JButton Button1, Button2;
private Account account;
public static void main(String[] args) {
Banco Ventana = new Banco();
Ventana.setSize(300,300);
Ventana.createGUI();
Ventana.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
label1 = new JLabel("Balance");
window.add(label1);
Square1 = new JTextField(5);
window.add(Square2);
label2 = new JLabel("State");
window.add(label2);
Square2 = new JTextField(9);
window.add(Square2);
Button1 = new JButton("Deposit");
Button1.addActionListener(this);
window.add(Button1);
Button2 = new JButton("Retire");
Button2.addActionListener(this);
window.add(Button2);
Square3 = new JTextField(5);
window.add(Square3);
account = new Account();
}
public void actionPerformed(ActionEvent event) {
int balance;
int Amount;
balance = account.getBalance();
Amount = Integer.parseInt(Square3.getText());
if(event.getSource() == Button1){
account.setBalance(balance);
account.depositBalance(Amount);
balance = account.getBalance();
}
if (event.getSource() == Button2){
account.setBalance(balance);
account.retireBalance(Amount);
balance = account.getBalance();
}
if (balance < 0){
Square2.setText("Overdrawn");
}
else {
Square2.setText("OK");
}
Square1.setText(Integer.toString(balance));
}
}
class Account{
int Balance=0;
public void setBalance(int Amount){
Balance = Amount;
}
public int getBalance(){
return Balance;
}
public void depositBalance(int Amount){
Balance = Balance + Amount;
}
public void retireBalance(int Amount){
Balance = Balance - Amount;
}
}
这看起来很可疑:
Square1 = new JTextField(5);
window.add(Square2);
您初始化 Square1
,但尝试将 Square2
添加到 window。
所以在堆栈跟踪中,它说
Exception in thread "main" java.lang.NullPointerException
at java.desktop/java.awt.Container.addImpl(Container.java:1117)
at java.desktop/java.awt.Container.add(Container.java:436)
at Banco.createGUI(Banco.java:29)
at Banco.main(Banco.java:16)
Process finished with exit code 1
您可以跳过前两个 at
,因为它不在您的代码中。
第三行说 (Banco.java:29)
这是您的代码中发生错误的地方。
检查该行,您在第 28 行中为 Square1
赋值,但您在第 34 行中用 Square2
调用 add()
。
这意味着,在第 29 行,Square2
的值为 null
,这就是为什么您得到 NullPointerException