试用JOptionPane。在执行期间获取空指针异常

时间:2013-02-10 15:18:04

标签: java swing

我想在存入一些钱之后提取一些钱,但我得到的是一个错误..为什么?例如,我存入100,之后我想退出90,但我得到的只是一个错误。另一个是,在存入100之后,我想打印我当前的余额,但它打印(0)零。为什么?请帮忙。

主类

import java.awt.HeadlessException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Banko{

    private static String name;
    private static double bal;
    private static double withdraw;
    private static BankAccount myAccount;

    public static void main(String args[]) {

        name = JOptionPane.showInputDialog(null, "Enter your name: ");

        String num;
        int pin;
        num = JOptionPane.showInputDialog("Enter your pin number: ");
        pin = Integer.parseInt(num);

        JOptionPane.showMessageDialog(null, "Login Success\n" + "Name : " + name + "\n" + "Pin Number : " + pin);

        BankAccount myAccount = new BankAccount(withdraw, name);

        int rc = getRC();
        processor(num, rc);
    }

    private static int getRC(){
        String[] buttons = { "Deposit", "Withdraw", "Print Balance", "Exit" };
        int rc = JOptionPane.showOptionDialog(
            null,
            "What would you like to do?",
            "Confirmation",
            JOptionPane.INFORMATION_MESSAGE,
            0,
            null,
            buttons,
            buttons[2]);
        return rc;
    }

    private static void processor(String num, int rc) {
        switch(rc) {
            case 0:
                processDeposit(num, rc);
                break;
            case 1:
                processWithdraw(num, myAccount, rc);
                break;
            case 2:
                processBalance(num, rc);
            default:
                processExit(rc);
                break;
        }
    }

    private static void processExit(int rc) throws HeadlessException {
        if(rc == -1) {
            JOptionPane.showMessageDialog(null, "\nThank you. Have a good day!");
            System.exit(0);
        }
    }


    private static void processDeposit(String num, int rc) throws HeadlessException, NumberFormatException {
        int deposit;
        String dep = JOptionPane.showInputDialog("How much would you like to deposit?\n\t$ ");
        deposit = Integer.parseInt(num);
        JOptionPane.showMessageDialog(null, "You have deposited $" + dep + " into the account of " + name);

        String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
        if(proceeds.equalsIgnoreCase("y")) {
            rc = getRC();
            processor(num, rc);
        } else {
            processExit(-1);
        }
    }


    private static void processBalance(String num, int rc) throws HeadlessException {
        JOptionPane.showMessageDialog(null, "The balance in the account of " + name + " with the pin number " + num
                + " is $" + bal);

         String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
        if(proceeds.equalsIgnoreCase("y")) {
            rc = getRC();
            processor(num, rc);
        } else {
            processExit(-1);
        }
    }



    private static void processWithdraw(String num, BankAccount myAccount, int rc) throws HeadlessException, NumberFormatException {
        double withdraw;
        String with = JOptionPane.showInputDialog("How much would you like to withdraw?\n\t$");
        withdraw = Integer.parseInt(num);
        if(bal - withdraw > 0) {
            myAccount.withdraw(withdraw);
            JOptionPane.showMessageDialog(null, "You have withdrawn $" + withdraw + " from the account of " + name
                    + ". The new balance is: " + myAccount.getBalance());
        } else {
            JOptionPane.showMessageDialog(
                null,
                "Sorry, you have insufficient funds for this operation. Your existing balance is $"
                        + myAccount.getBalance());
        }

        String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
        if(proceeds.equalsIgnoreCase("y")) {
            rc = getRC();
            processor(num, rc);
        } else {
            processExit(-1);
        }

    }
}

基类:

import java.util.Scanner;

public class BankAccount {

    private double balance;

    private String name;

    public BankAccount(double b, String n) {
        this.balance = b;
        this.name = n;
    }

    public void deposit(double d) {
        balance += d;
    }

    public void withdraw(double w) {
        balance -= w;
    }

    public String nickname() {
        System.out.print("Enter a new name: ");
        Scanner kbIn = new Scanner(System.in);
        String n = kbIn.nextLine();
        return n;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

} 

2 个答案:

答案 0 :(得分:1)

您正在声明一个新的BankAccount实例,而不是使用静态变量:

public static void main(String args[]) {
    // ...
    myAccount = new BankAccount(withdraw, name);
    int rc = getRC();
    processor(num, rc);
}

答案 1 :(得分:1)

好实验。只有轻微的错误。

  1. 不必要的变量bal,因为您在帐户中有余额
  2. 必须在任何地方使用对象myAccount
  3. 存款变动

        deposit = Integer.parseInt(dep);
        JOptionPane.showMessageDialog(null, "You have deposited $" 
                     + dep + " into the account of " + name);
        myAccount.setBalance(myAccount.getBalance() + deposit);
    

    撤回

        withdraw = Integer.parseInt(with);
        if (myAccount.getBalance() - withdraw > 0) {
    

    为getBalance

     JOptionPane.showMessageDialog(null, "The balance in the account of " 
        + name + " with the pin number " + num
        + " is $" + myAccount.getBalance());
    
相关问题