Java:Nullpointer errors
Code:
import java.text.DecimalFormat;
public class Account{
//class Variables
private static int NumAccounts = 0;
private static final double Interest = 0.05;
//Instant Variables
private long accNum;
private double balance;
DecimalFormat x = new DecimalFormat("$00,000.00");//Format Balance
public Account(long accN, double bal){//Constructor
accNum = accN;
balance = bal;
NumAccounts++;
}
public int GetNumAccounts(){
return NumAccounts;
}
public double getBalance(){
return balance;
}
public long getAccountNum(){
return accNum;
}
public void debit(double amount){
if(balance >= amount){
balance -= amount;
}
}
public void credit(double amount){
balance += amount;
}
public String toString(){
String Str;
Str ="Account Number :" + accNum + "\n"+
"Account Balance :" + x.format(balance) + "\n";
return Str;
}
}//End of class Account
import java.io.*;
import java.util.*;
public class TestAccountx{
private static final int Max = 1000;
static Account[]accList= new Account[Max];
static Account u;
public static Account getAccount(long accNum){//using binary search to find and return account if exist
int lo = 0;
int hi = accList.length-1;
while(lo <= hi){
int mid = (lo + hi)/2;
if(accList[mid].getAccountNum()== accNum){ //NULL POINTER EXCEPTION
return accList[mid];
}else if( accList[mid].getAccountNum() < accNum ){
hi = mid - 1;
}else{
lo = mid + 1;
}
}//end while
return null;
}//end getaccoutn;
public static void insertIntoArray(Account p)
{
int numAccounts = p.GetNumAccounts();
accList[numAccounts - 1] = p;
int lo = 0;
int hi = numAccounts -1;
for(int j = lo + 1; j <= hi; j++){
Account hold = accList[j];
int k = j - 1;
while (k >= 0 && hold.getAccountNum()< accList[k].getAccountNum()){
accList[k + 1] = accList[k];
--k;
}//End While
accList[k + 1] = hold;
}//End For
} //End Insertion Sort
public static void setNumAccounts(int numAccounts){//deincrement accounts
numAccounts--;
}
public static void transaction(int accNum, double amount,int transType)
{
Account h = getAccount(accNum); // NULL POINTER EXCEPTION
System.out.println("hhhhhhhhhhhhhhhhhh" + h);
if(h!=null){
if (transType == 1){
accList[accNum].debit(amount);
}else if (transType==2){
accList[accNum].credit(amount);
}else{
System.out.println("Account does not exist");
}
}
}//end transaction
public static void deleteAccount(long accNum){
int j=0;
for(int i = 0; j < u.GetNumAccounts(); i++){
if(accList[j].getAccountNum()==u.getAccountNum()){
setNumAccounts( j );
break;
}
if(i == u.GetNumAccounts()){
System.out.println("Print error message");
}
for(j = i; j < u.getAccountNum(); j++){
accList[j] = accList[j + 1];
}
}//end for
}//end function
public static void main(String[] args){
Account a = new Account(123456,5255.17);
insertIntoArray(a);
Account b = new Account(455555,19218.20);
insertIntoArray(b);
Account c = new Account(217794,1002.62);
insertIntoArray(c);
Account d = new Account(998283,20203.75);
insertIntoArray(d);
//Create Transactions
transaction(12345,1255.17,1); //NULL POINTER EXCEPTION
transaction(188468,2781.80,2);
transaction(217794,2000.00,1);
//deleteAccount(998283);
//deleteAccount(123457);
transaction(998283,125.00,1);
for(int i = 0; i < 4; i++){
System.out.println(accList[i].toString());
}
}//end main
}//end class TestAccount
When I run my code, I am getting a nullpointer exception error at the points indicated. My suspicions are the getAccount(long accNum) method that I am using to return an Account is returning a null value. It appears to me that the while loop is finishing before it gets to smaller values such as 0,1,2,3. Please help me rectify this problem. Everything I have tried so far has failed. (urgent)
|