-5

My program outputs a null when I'm trying to enter an amount in deposit, withdraw, balance inquiry, and account information when I try to select I already have an account but runs smoothly if I shoose to create a new account.

public class BankAtm {

    public static SavingsAccount accnt;
    
    public static void main(String[] args) {
        JTextField name = new JTextField();
        JTextField pin = new JTextField();
        JTextField aage = new JTextField();
        JTextField contactNo = new JTextField();
        
        String aNo;
        String aName;
        String aPin;
        int age;
        String aContactNo;
        
        String[] choices = {"Yes", "I already have an account", "Exit"};
        
        int y = JOptionPane.showOptionDialog(null, "Would you like to open a new acount?" , 
                "Bank ATM", 
                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
        
        switch (y) {
        case 0:
            int max = 500;
            int min = 100;
                            
            Object[] a = {"Account Name: ", name, "Age: ", aage, "Account Pin Number: ", pin, "Contact Number (please input parent's or guardian's contact number if below 18): ", contactNo};
                                
            JOptionPane op= new JOptionPane(a, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null);
            JDialog dialog= op.createDialog(null, "Create New Account");
            dialog.setVisible(true);
                                    
            aName = name.getText();             
            age = Integer.parseInt(aage.getText());
            aPin = pin.getText();
            aContactNo=contactNo.getText();
                        
            int no = (int) (Math.random()*(max-min+1)+max);         
            JOptionPane.showMessageDialog(null, "Your account number is "+no);
            aNo = Integer.toString(no);
            
            accnt = new SavingsAccount (aName, aNo, aPin);
            accnt.accntName=aName;
            accnt.accntNo= aNo;
            accnt.accntAge= age;
            accnt.accntPin= aPin;
            accnt.accntContactNo=aContactNo;
            
        case 1:
            int trans;
            String php;
            double amt;
            
                do {
                    
                    String s = JOptionPane.showInputDialog("1. Deposit"
                            + "\n2. Withdraw"
                            + "\n3. Balance Inquiry"
                            + "\n4. Display Account Info"
                            + "\n5. Exit");
                    trans = Integer.parseInt(s);
                    
                    switch (trans) {
                        case 1:
                            php = JOptionPane.showInputDialog("Enter amount to deposit: ");
                            amt = Double.parseDouble(php);
                            accnt.deposit(amt);
                            
                            break;
                        
                        case 2:
                            php = JOptionPane.showInputDialog("Enter amount to withdraw: ");
                            amt = Double.parseDouble(php);
                            accnt.withdraw(amt);
                            
                            break;
                        
                        case 3:
                            accnt.balInquiry();
                            
                            break;
                        
                        case 4:
                            accnt.displayAccntInfo();
                            
                            break;
                        
                        case 5:
                            JOptionPane.showMessageDialog(null, "Thank you for using our service!~");
                            
                            break;
                
                    }
            
                
                
                } while (trans!=5);
            

            }
        
    return;
    }
}

The program outputs Exception in thread "main" java.lang.NullPointerException: Cannot invoke "SavingsAccount.displayAccntInfo()" because "BankAtm.accnt" is null

Alexandra
  • 1
  • 1
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Sören Apr 01 '23 at 15:47
  • Breaking up your monolithic main method code into methods would be a good first start. Methods can be tested separately and independently. – Gilbert Le Blanc Apr 01 '23 at 19:18

1 Answers1

-1

In the case 0 of your switch, you're creating a new SavingsAccount through its constructor, thus giving him a value, but if you enter case 1 of that switch, the account constructor is never called since you just declare it so:

    public static SavingsAccount accnt;

When you initialise a variable without assigning it a value, it gets a default value, which is null. You cannot access the properties of a null object, since it doesn't have any. You have to call the constructor from somewhere before using any method on your object.

mi1000
  • 104
  • 9
  • 1
    Please don't answer obvious duplicates. Instead flag them as duplicates so they can be closed appropriately. – Sören Apr 01 '23 at 16:46
  • This is exactly why I don't like the atmosphere of that website, how about just helping newcomers instead of being unwelcoming ? – mi1000 Apr 01 '23 at 16:49
  • 1
    How is pointing to an existing answer unwelcoming? – Sören Apr 01 '23 at 16:52
  • Imagine if you asked someone in the street the closest store and he just told you "look on Google Maps", how would you react ? – mi1000 Apr 01 '23 at 16:59
  • 1
    That's comparing apples to oranges. A better comparison would be "There's already an answer to your question right there! All you have to do is read it. Cool, right?" – Sören Apr 01 '23 at 17:04
  • 2
    @Sören the generic 'hey here you go this explains NPEs' is not enitrely suitable here; the major issues with OPs code involve, clearly, problems with understanding their own code flow or what JOptionPane returns (should be using constants). At the _very least_ one can make an objective case for it. And here you are telling somebody trying to help to that they are doing it wrong. I concur; you are being needlessly harsh, and that is quite unwelcoming. This answer isn't great, for a different reason: It really _just_ explains the NPE part, without delving into OP's specific problem either. – rzwitserloot Apr 01 '23 at 17:38