-2

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;
    }
}
Sturges
  • 3
  • 1
  • Have you stepped through the code in a debugger in your IDE? This will show you where the null occurs. – Kevin Hooke Oct 30 '20 at 23:26
  • 2
    Voted to close as **typo** *(or copy/paste error)*, because the first `window.add(Square2)` call should be `window.add(Square1)`. – Andreas Oct 30 '20 at 23:31

2 Answers2

0

This looks suspicious:

Square1 = new JTextField(5);
window.add(Square2);

You initialize Square1, but attempt to add Square2 to the window.

Hulk
  • 6,399
  • 1
  • 30
  • 52
0

so in stack trace, it says

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

You can skip the first two at because it's not in your code. The third line says (Banco.java:29) which is where the error happened in your code.

Checking that line, you're assigning a value to Square1 in line 28, but you are calling add() with Square2, which you're assigning in line 34. This means that, at line 29, Square2 has a value of null which is why you get the NullPointerException

kxrtik
  • 48
  • 6