E
elingtse
Hi all,
I am a newbie of Java and am working on my homework. I got an error
message "NullPointerException" after compilation. I know this means
values cannot be passed to array but it seems not my case because it
can generate a correct output on screen. The program terminate
abnormally after this error message. Could anyone help to point out
what I did wrong, thanks a lot.
et
Compilation Output :
------------------------------
Accounts of Amy
1234 : $1000.0
1122 : $5000.0
Exception in thread "main" java.lang.NullPointerException
at Person.displayAccount(Person.java:29)
at TestPerson.main(TestPerson.java:23)
Person Class
--------------------
public class Person {
//attributes
private String name;
private Account[] account = new Account[10];
//constructor
public Person(String name, Account[] account){
this.name = name;
this.account = account;
}
//create getter for name
public String getName(){
return name;
}
//create setter for name
public void setName(String setName){
name = setName;
}
//create displayAccount method
public void displayAccount(){
System.out.println("Accounts of " + name);
for(int i=0; i<account.length; i++){
System.out.println(account.getNumber() +" : $"+
account.getBalance());
}
}
//create lowBalanceAccounts method
public String lowBalanceAccounts(double lowBalance){
String msg = "No account's balance is lower than Low balance";
for(int i=0; i<account.length; i++){
if(account.getBalance() < lowBalance)
return account.getNumber();
}
return msg;
}
//create highestBalanceAccount Method
public String highestBalanceAccount(){
double hBal = 0;
String acNo = null;
for(int i=0; i<account.length; i++){
if(account.getBalance() > hBal){
hBal = account.getBalance();
acNo = account.getNumber();
}
}
return acNo;
}
}
TestPerson Class
--------------------------
//The class of TestPerson creates an environment
//for testing Person object
public class TestPerson {
public static void main(String Arg[]) {
//Attribute
int accountCount = 0;
Account[] account = new Account[10];
//create account and person objects for Amy
Account account1 = new Account("1234", 1000);
Account account2 = new Account("1122", 5000);
account[0] = account1;
account[1] = account2;
Person amy = new Person("Amy", account);
//System.exit(-1);
//call displayAccounts method
amy.displayAccount();
//call lowBalance accounts
System.out.println("Low balance account(s) :
"+amy.lowBalanceAccounts(2000));
//call highestBalanceAccount
System.out.println("The highest account is :
"+amy.highestBalanceAccount());
}
}
I am a newbie of Java and am working on my homework. I got an error
message "NullPointerException" after compilation. I know this means
values cannot be passed to array but it seems not my case because it
can generate a correct output on screen. The program terminate
abnormally after this error message. Could anyone help to point out
what I did wrong, thanks a lot.
et
Compilation Output :
------------------------------
Accounts of Amy
1234 : $1000.0
1122 : $5000.0
Exception in thread "main" java.lang.NullPointerException
at Person.displayAccount(Person.java:29)
at TestPerson.main(TestPerson.java:23)
Person Class
--------------------
public class Person {
//attributes
private String name;
private Account[] account = new Account[10];
//constructor
public Person(String name, Account[] account){
this.name = name;
this.account = account;
}
//create getter for name
public String getName(){
return name;
}
//create setter for name
public void setName(String setName){
name = setName;
}
//create displayAccount method
public void displayAccount(){
System.out.println("Accounts of " + name);
for(int i=0; i<account.length; i++){
System.out.println(account.getNumber() +" : $"+
account.getBalance());
}
}
//create lowBalanceAccounts method
public String lowBalanceAccounts(double lowBalance){
String msg = "No account's balance is lower than Low balance";
for(int i=0; i<account.length; i++){
if(account.getBalance() < lowBalance)
return account.getNumber();
}
return msg;
}
//create highestBalanceAccount Method
public String highestBalanceAccount(){
double hBal = 0;
String acNo = null;
for(int i=0; i<account.length; i++){
if(account.getBalance() > hBal){
hBal = account.getBalance();
acNo = account.getNumber();
}
}
return acNo;
}
}
TestPerson Class
--------------------------
//The class of TestPerson creates an environment
//for testing Person object
public class TestPerson {
public static void main(String Arg[]) {
//Attribute
int accountCount = 0;
Account[] account = new Account[10];
//create account and person objects for Amy
Account account1 = new Account("1234", 1000);
Account account2 = new Account("1122", 5000);
account[0] = account1;
account[1] = account2;
Person amy = new Person("Amy", account);
//System.exit(-1);
//call displayAccounts method
amy.displayAccount();
//call lowBalance accounts
System.out.println("Low balance account(s) :
"+amy.lowBalanceAccounts(2000));
//call highestBalanceAccount
System.out.println("The highest account is :
"+amy.highestBalanceAccount());
}
}