1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Java:Nullpointer errors

Discussion in 'Java' started by cyrow, Sep 24, 2008.

  1. cyrow

    cyrow New Member

    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;
    	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;
    		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];
      			}//End While
      			accList[k + 1] = hold;
      		}//End For
      }	//End Insertion Sort	 
    	public static void setNumAccounts(int numAccounts){//deincrement accounts
    public static void transaction(int accNum, double amount,int transType)
         Account h = getAccount(accNum);          // NULL POINTER EXCEPTION
         System.out.println("hhhhhhhhhhhhhhhhhh" + h);
         	if (transType == 1){
           }else if (transType==2){
           	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++){
       			setNumAccounts(  j  );
       			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);
    	Account	b = new Account(455555,19218.20);
    	Account	c = new Account(217794,1002.62);
    	Account	d = new Account(998283,20203.75);
     //Create Transactions
     		transaction(12345,1255.17,1);	  //NULL POINTER EXCEPTION	
    	for(int i = 0; i < 4; i++){
    	}//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)

Share This Page