因此,我正在编写一个使用GUI模仿支票帐户的程序。我的功能之一有问题。我创建了一个Transaction对象,并调用了一个支票帐户中的函数,以将该交易帐户添加到checkingaccount类中的Transaction数组列表中。但是,每次调用该函数时,我都会收到空指针异常错误。我怎样才能解决这个问题?

码:

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);
    }
}


提前致谢。

最佳答案

ArrayList变量transList为空。您需要在构造函数中对其进行初始化:

public CheckingAccount(double initialVal) {
    balance = initialVal;
    totalServiceCharge = 0;
    transCount = 0;
    transList = new ArrayList<Transaction>();//here
}

09-08 07:10