0

So, I am writing a program imitating a checking account using GUI. I have a problem with one of my functions. I create a Transaction object and call a function in checking account to add the Transaction account into the Transaction arraylist in checkingaccount class. However, everytime the function is called I receive null pointer exception error. How can I fix this?

Code:

public class CheckingAccount 
{
    private double balance;
    private double totalServiceCharge;
    private ArrayList<Transaction> transList;
    private int transCount;

public CheckingAccount(double initialVal)
    {
        balance = initialVal;
        totalServiceCharge = 0;
        transCount = 0;
    }

    public double getBalance()
    {
        return balance;
    }

    public void setBalance(double transAmt, int tCode)
    {
        if(tCode == 1)
        {
            balance -= transAmt;

        }
        else if(tCode == 2)
        {
            balance += transAmt;
        }
    }

    public double getServiceCharge()
    {
        return totalServiceCharge;
    }

    public void setServiceCharge(double currentServiceCharge)
    {
        totalServiceCharge += currentServiceCharge;
    }

    public void addTrans(Transaction newTrans)
    {
        transList.add(newTrans);
        transCount++;
    }

    public int getTransCount()
    {
        return transCount;
    }

    public Transaction getTrans(int i)
    {
        return transList.get(i);
    }

}

public class Transaction 
{
    private int transNumber;
    private int transId;
    private double transAmt;

    public Transaction(int num, int id, double amt)
    {
        transNumber = num;
        transId = id;
        transAmt = amt;
    }

    public int getTransNumber()
    {
        return transNumber;
    }

    public int getTransId()
    {
        return transId;
    }

    public double getTransAmount()
    {
        return transAmt;
    }
}

public class OptPanel extends JPanel
{
    private JLabel label;
    private JRadioButton button1, button2, button3, button4;

    public OptPanel()
    {       
        label = new JLabel("Choose Action:");
        label.setFont(new Font("Helvetica", Font.BOLD, 24));

        button1 = new JRadioButton("enter transaction");
        button2 = new JRadioButton("list all transactions");
        button3 = new JRadioButton("list all checks");
        button4 = new JRadioButton("list all deposits");

        ButtonGroup group = new ButtonGroup();
        group.add(button1);
        group.add(button2);
        group.add(button3);
        group.add(button4);

        OptListener listener = new OptListener();

        button1.addActionListener(listener);
        button2.addActionListener(listener);
        button3.addActionListener(listener);
        button4.addActionListener(listener);

        add(label);
        add(button1);
        add(button2);
        add(button3);
        add(button4);

        setBackground(Color.yellow);
        setPreferredSize(new Dimension(300,150));
    }   

    private class OptListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            Object source = event.getSource();

            if(source == button1)
            {
                Main.doFunction();
            }
            else if(source == button2)
            {
                Main.listTrans();
            }
            else if(source == button3)
            {

            }
            else if(source == button4)
            {

            }
        }
    }
}

public class Main 
{   
    static String msg = JOptionPane.showInputDialog("Enter your initial balance:");
    static double amt = Double.parseDouble(msg);

    static CheckingAccount checkAcc = new CheckingAccount(amt);
    static Transaction trans = new Transaction(0, 0, 0);

    public static void main(String[] args)
    {       
        JFrame frame = new JFrame("Checking Account Actions");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        OptPanel panel = new OptPanel();
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static int getTransCode()
    {
        String msg = JOptionPane.showInputDialog("Enter trans code: ");
        return Integer.parseInt(msg);
    }

    public static double getTransAmt()
    {
        String msg = JOptionPane.showInputDialog("Enter trans amt: ");
        return Double.parseDouble(msg);
    }

    public static void processCheck(CheckingAccount acc, double amt, int tcode)
    {
        acc.setBalance(amt, tcode);
        if(acc.getBalance() < 0)
        {
            acc.setServiceCharge(10.15);
        }
        else
        {
            acc.setServiceCharge(0.15);
        }
    }

    public static void processDeposit(CheckingAccount acc, double amt, int tcode)
    {
        acc.setBalance(amt, tcode);
        acc.setServiceCharge(0.10);
    }

    public static void doFunction()
    {
        String msg;
        int code;
        double amt;

        DecimalFormat fmt = new DecimalFormat ("$#,###,###.00;($#,###,###.00)");

        do
        {
            code = getTransCode();

            if(code == 1)
            {
                amt = getTransAmt();
                processCheck(checkAcc, amt, code);

                if(checkAcc.getTransCount() == 0)
                {
                    checkAcc.setServiceCharge(5);
                    msg = "Transaction: Check in amount of " + fmt.format(amt) + "\n Current Balance: " +
                             fmt.format(checkAcc.getBalance()) + "\n" +
                             "Service Charge: Check -- charge $0.15" +
                             (checkAcc.getBalance() < 50 ? "\nWarning: Balance below $50" : "") +
                             (checkAcc.getBalance() < 0 ? "\nService Charge: Below $0 -- charge $10.00" : "") +
                             "\nService Charge: Below $500 -- charge $5.00\nTotal Service Charge: " + 
                             fmt.format(checkAcc.getServiceCharge());
                    JOptionPane.showMessageDialog (null, msg);
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 1, amt));
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 3, 0.15));
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 3, 5.00));
                }
                else
                {
                    msg = "Transaction: Check in amount of " + fmt.format(amt) + "\n Current Balance: " + 
                             fmt.format(checkAcc.getBalance()) + "\n" +
                             "Service Charge: Check -- charge $0.15" +
                             (checkAcc.getBalance() < 50 ? "\nWarning: Balance below $50" : "") +
                             (checkAcc.getBalance() < 0 ? "\nService Charge: Below $0 -- charge $10.00" : "") +
                             "\nTotal Service Charge: " + fmt.format(checkAcc.getServiceCharge());
                    JOptionPane.showMessageDialog (null, msg);
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 1, amt));
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 3, 0.15));
                }

            }
            else if(code == 2)
            {
                amt = getTransAmt();
                processDeposit(checkAcc, amt, code);

                if(checkAcc.getTransCount() == 0)
                {
                    System.out.println(checkAcc.getTransCount());
                    checkAcc.setServiceCharge(5);
                    msg = "Transaction: Deposit in amount of " + fmt.format(amt) + "\n Current Balance: " + 
                            fmt.format(checkAcc.getBalance()) + "\n" +
                             "Service Charge: Deposit -- charge $0.10" +
                             (checkAcc.getBalance() < 50 ? "\nWarning: Balance below $50" : "") +
                             (checkAcc.getBalance() < 0 ? "\nService Charge: Below $0 -- charge $10.00" : "") +
                             "\nService Charge: Below $500 -- charge $5.00\nTotal Service Charge: " + 
                             fmt.format(checkAcc.getServiceCharge());
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 2, amt));
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 3, 0.10));
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 3, 5.00));
                    JOptionPane.showMessageDialog (null, msg);
                }
                else
                {
                    msg = "Transaction: Deposit in amount of " + fmt.format(amt) + "\n Current Balance: " + 
                            fmt.format(checkAcc.getBalance()) + "\n" +
                             "Service Charge: Deposit -- charge $0.10" +
                             (checkAcc.getBalance() < 50 ? "\nWarning: Balance below $50" : "") +
                             (checkAcc.getBalance() < 0 ? "\nService Charge: Below $0 -- charge $10.00" : "") +
                             "\nTotal Service Charge: " + fmt.format(checkAcc.getServiceCharge());
                    JOptionPane.showMessageDialog (null, msg);
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 2, amt));
                    checkAcc.addTrans(new Transaction(checkAcc.getTransCount(), 3, 0.10));
                }
            }

        }while(code != 0);

        msg = "Transaction: End\n Current Balance: " + fmt.format(checkAcc.getBalance()) +
                 "\nTotal Service Charge: " + fmt.format(checkAcc.getServiceCharge()) + "\n" +
                 "Final Balance: " + fmt.format(checkAcc.getBalance() - checkAcc.getServiceCharge());
        JOptionPane.showMessageDialog (null, msg);
    }

    public static void listTrans()
    {  
        JTextArea text = new JTextArea();
        String message = "";
        int num;
        text.setOpaque(false);
        text.setFont(new Font("Monospaced", Font.PLAIN, 14));
        text.setBorder(null);
        message+="ID       Type       Amount\n\n";
        for(num=0; num < checkAcc.getTransCount(); num++)
        {  
            message += String.format("%3d            %-9s              %6d\n", num, 
                    (checkAcc.getTrans(num).getTransId() == 1 ? "Check" : 
                        (checkAcc.getTrans(num).getTransId() == 2 ? "Deposit" : "Svc. Charge")),
                    checkAcc.getTrans(num).getTransAmount());
        }
        text.setText(message);
        JOptionPane.showMessageDialog(null, text);
    }
}

Thanks in advance.

1
  • Can you share with use your stack trace? Commented Apr 2, 2015 at 4:26

2 Answers 2

1

ArrayList variable transList is null. You need to initialize it in the constructor:

public CheckingAccount(double initialVal) {
    balance = initialVal;
    totalServiceCharge = 0;
    transCount = 0;
    transList = new ArrayList<Transaction>();//here
}
Sign up to request clarification or add additional context in comments.

2 Comments

You are fast, I was answering the same! :p
@None, glad it helped you, now you may accept my answer.
0

Null pointer exception is coming, since you have not created a ArrayList object to add.

Insert the following line in "public CheckingAccount(double initialVal)" constructor.

transList = new  ArrayList<Transaction> ();

FYI: Whenever there is a null pointer exception, check whether the assigning object is already available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.