Sorry for the cuality of the post is my first time posting on StackOverflow. I was working on my homework, it consist in create a bank account simulation, makeing deposits and retires, when i finished my program the console show me this:
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
This is my code, i spend some time trying to fix it, i couldnt find where is my variable with the value null, thats why im looking for some help.
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;
}
}