我正在进行一项练习,我建立了一个银行系统,我创建了一个帐户类,一个客户类和一个银行类,但是当我检查程序时,我得到了:
Exception in thread "main" java.lang.NullPointerException.
以下是我的课程:
package bank.elements;
public class Account {
private int id;
private float balance;
//constructors
public Account(int id, float balance){
this.setId(id);
this.setBalance(balance);
}
public Account (int id){
this.id=id;
this.setBalance(0);
}
//getters
public int getId(){
return this.id;
}
public float getBalance(){
return this.balance;
}
//setters
public void setBalance(float balance){
this.balance+=balance;
}
public void setId(int id){
this.id=id;
}
}
package bank.elements;
public class Client {
private int id;
private String name;
private String rank;
private float balance;
private Account[] accounts = new Account[100];
public Client(){
}
//getters
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getRank(){
return rank;
}
public float getBalance(){
return balance;
}
public float getAccountBalance(int id){
float balance = 0;
for (int i = 0; i < accounts.length; i++) {
if(accounts[i].getId()==id){
balance = accounts[i].getBalance();
}
}
return balance;
}
public float getFortune(){
float sum = 0;
for (int i = 0; i < accounts.length; i++) {
sum+=getAccountBalance(i);
}
sum+=getBalance();
return sum;
}
//setters
public void setName(String name){
this.name = name;
}
public void setBalance(float balance){
this.balance+=balance;
}
public void setId(int id){
this.id=id;
}
public void addAccount(int id){
for (int i = 0; i < accounts.length; i++) {
if(accounts[i]==null){
accounts[i].setId(id);
}
}
}
}
package bank.elements;
public class Bank {
public Bank(){
}
public Client[] clients = new Client[100];
public float getBalance(){
float sum = 0;
for (int i = 0; i < clients.length; i++) {
sum+= clients[i].getFortune();
}
return sum;
}
public void addClient(){
for (int i = 0; i < clients.length; i++) {
if(clients[i] == null){
clients[i].setId(i);
}
}
}
public void removeClient(int id){
for (int i = 0; i < clients.length; i++) {
if(clients[i].getId()==id){
clients[i]=null;
for (int j = (i+1); j < clients.length; j++) {
clients[j] = clients[j-1];
}
}
}
}
}
这是程序主题:
package bank.program;
import bank.elements.Account;
import bank.elements.Client;
import bank.elements.Bank;
public class Program {
public static void main(String[] args) {
Bank b = new Bank();
b.addClient();
b.clients[0].setBalance(100);
b.clients[0].addAccount(2);
System.out.println(b.clients[0].getFortune());
}
}
我错过了什么?
答案 0 :(得分:3)
在addClient()
方法中:
for (int i = 0; i < clients.length; i++) {
if(clients[i] == null){
clients[i].setId(i);
}
}
因为你检查数组的第i个位置的Client对象是否为null
然后你试图从这个对象访问一个方法,从而检查NPE,所以抛出NPE是完全正常的。
执行public Client[] clients = new Client[100];
时,数组的100个插槽初始化为null
(Java中对象的默认值初始化)。
我会在你的构造函数中初始化它:
public Bank(){
for(int i = 0; i < clients.length; i++){
clients[i] = new Client();
}
}
在addClient()
方法中更改您的if(执行if client[i] != null
以阻止NPE),即:
for (int i = 0; i < clients.length; i++) {
if (client[i] != null){
clients[i].setId(i);
}
}
Client
类的Account
数组的相同评论。