帐户类错误

时间:2015-12-13 22:50:55

标签: java account

由于某种原因,我在这个程序上收到错误,我不知道这意味着什么。我试过谷歌搜索,但没有任何与我的问题相关的问题。这有可能与IOException有关吗?此外,输入存储在哪里?在类文件中?例如:如果我要在账户中输入资金,输入的值是否会保存在不同的文件中,或者我为30个账户创建了30个账户的数量?

主要课程:

        import java.util.Scanner;
import java.text.NumberFormat;
public class Account2
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    Account[] acct = new Account[30];
    System.out.println("Enter your account number (1-30): ");
    int key = scan.nextInt() - 1;
    int reset = 0;
    while (reset == 0)
    {
      System.out.println("Enter W for withdrawl; D for deposit; X to escape");
      char choice = scan.next().charAt(0);

      if  (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd' || choice == 'x' || choice == 'X')
      {
        if (choice == 'W' || choice == 'w')
        {
          System.out.println("Enter amount to withdraw: ");
          Double withdraw1 = scan.nextDouble();
          if (withdraw1 <= acct[key].getBalance())
          {
            acct[key].withdraw(withdraw1);
            System.out.println("User # " + key++ + " funds after withdraw: " + acct[key].getBalance() + "$");
            System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$");
            reset++;
          }
          else
            System.out.println("Insufficient funds.");
        }

        if (choice == 'D' || choice == 'd')
        {
          System.out.println("Enter amount to deposit: ");
          Double deposit1 = scan.nextDouble();
          if (deposit1 > 0)
          {
            acct[key].deposit(deposit1);
            System.out.println("User # " + key++ + " funds after deposit: " + acct[key].getBalance() + "$");
            System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$");
            reset++;
          }
          else
            System.out.println("Use the withdrawl feature to withdrawl money.");

        }
        if (choice == 'x' || choice == 'X')
          System.out.println("Thank You for using this bank.");
          reset++;
      }
      else
        {
          System.out.println("Invalid entry, please try again");
          reset = 0;
        }
    }
  }
}

第二课:

import java.text.NumberFormat; //links to Part2

public class Account
{
  private final double RATE = 0.03; //Interest is 3%

  private long acctNumber;
  private double balance;
  private String name;

  //Defines owner, account number, and initial balance.
  public Account(String owner, long account, double initial)
  {
    name = owner;
    acctNumber = account;
    balance = initial;
  }

  //deposits a specified amount and returns new balance
  public double deposit(double amount)
  {
    balance = balance + amount;
    return balance;
  }

  //withdraws the specified amount from the account and applies the fee
  //                                                  + returns balance
  public double withdraw(double amount)
  {
    int fee = 1;
    balance = balance - amount - fee;
    return balance;
  }

  //Adds interest to the account
  public double addInterest()
  {
    balance += (balance * RATE);
    return balance;
  }
  public double getBalance()
  {
    return balance;
  }

  //returns a one line description of the account as a string
  public String toString()
  {

    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    return acctNumber + "/t" + name + "/t" + fmt.format(balance);
  }
}

错误讯息:

Enter your account number (1-30):
5
Enter W for withdrawl; D for deposit; X to escape
d
Enter amount to deposit:
50
Exception in thread "main" java.lang.NullPointerException
        at Account2.main(Account2.java:40)

1 个答案:

答案 0 :(得分:0)

您尝试访问acct的索引,但数组为空Account[] acct = new Account[30];

首先应该填充数组,尝试让用户先创建一个帐户。