0

so for my program I am trying to sort a 2 dimensional array filled with the bank account information of customers. The array fills nicely and when i output the contents everything is correctly filled in. However when i try to sort it using the comparator, it says i have a null point exception. I can't seem to find out why i get this error.

(Sort method is at the bottom)

    public class Customer implements Serializable
{
  public boolean istrue;
  private String    name;
  private String    id; // 3 digits string
  private String    pin;    // 4 digits string
  private ArrayList<Account> acct;
  private String [][] array;
  private double total_bal;  // for all accounts
  private int row;



  public Customer()  //constructor
  { 
      acct = new ArrayList<>(100);
      row = 0;
      array = new String[80][5];
      istrue = true;
      name = "NoName";
      id = "000";
      pin = "0000";
      total_bal = 0;

  }

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getPin() {
    return pin;
}

public void setPin(String pin) {
    this.pin = pin;
    if (pin.length() != 4) // check to see if PIN is less or greater than 4 
    {
        System.out.println("That was not 4 digits.");
    }
    else istrue = false;

}

public void fill_acct(Account acct_obj)
{
    acct.add(acct_obj);
}

public ArrayList<Account> getAcct() {
    return acct;
}
public boolean acct_check(String account, boolean isfound)
{
    for (Account act : acct)
    {
        if (account.equals(act.getNumber()))
        {
            isfound = true;
        }

    }
    return isfound;
}

public void acct_deposit(String account, double deposit)
{
    for (Account act : acct )
    {
        if (account.equals(act.getNumber()))
        {
            act.setBalance(deposit);
            System.out.print("Account " + act.getNumber() + " balance: ");
            System.out.println(act.getBalance());
        }
    }
}
public void acct_info(String account)
{

        for (Account act : acct )
        {
            if (account.equals(act.getNumber()))
            {   
                if(act.getType().equals("checking"))
                {
                System.out.print("Checkings account number: ");
                System.out.println(act.getNumber());
                System.out.print("Checkings account balance: ");
                System.out.println(act.getBalance());
                }
                if(act.getType().equals("saving"))
                {
                System.out.print("Savings account number: ");
                System.out.println(act.getNumber());
                System.out.print("Savings account balance: ");
                System.out.println(act.getBalance());
                }
            }
        }   




}

public void acct_withdraw(String account, double amount)
{

    for (Account act : acct)
    {
        if (account.equals(act.getNumber()))
        {
            act.setBalance(-(amount));
            System.out.print("Account " + act.getNumber() + " balance: ");
            System.out.println(act.getBalance());
        }
    }
}

public void acct_transfer(String w_account, String d_account, double amount)
{
    acct_withdraw(w_account, amount);
    acct_deposit(d_account, amount);
}

public void close_account(String account)
{
    for (Account act : acct)
    {
        if (account.equals(act.getNumber()))
        {
            act.setZero();
            act.setActive(false);
            System.out.print("Account " + act.getNumber() + " balance: ");
            System.out.println(act.getBalance());
        }
    }


}
public void interest_check(int counter, int interest_rate)
{
    if (counter % 5 == 0)
    {
        System.out.println("5 Transactions: Compounding interest! ");
        for(Account act: acct)
        {
            if (act.getType().equals("saving"))
            {
                act.compound_interest(interest_rate);
            }
        }
    }

}

public void acct_num()
{
    for (Account act : acct)
    {
        System.out.println(name + "          " + act.getNumber() + "        " + pin + "          "
        + act.getBalance() + "        " + id);
    }
}

public void fill_array()
    {

    for (Account act : acct)
    {

        for(int col = 0; col < 5; col++)
        {
            if (col == 0)
            {
                array[row][col] = name;
            }
            if (col == 1)
            {
                array[row][col] = act.getNumber();
            }
            if (col == 2)
            {
                array[row][col] = pin;
            }
            if (col == 3)
            {
                array[row][col] = String.valueOf(act.getBalance());
            }
            if (col == 4)
            {
                array[row][col] = id;
            }   
        }   
        row++;
    }

    }

public void sort_array()
{   for (int x = 0; x < row; x++)
{
    for (int y = 0; y < 5; y++)
    {
        System.out.print(array[x][y] + "           ");
        if (y == 4)
        {
            System.out.println();
        }
    }
}
    Arrays.sort(array, new Comparator<String[]>(){

    @Override
    public int compare(final String[] first, final String[] second){
        return Double.valueOf(second[3]).compareTo(Double.valueOf(first[3])
        );
    }
});
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Albert Liu
  • 13
  • 4
  • Do you want to sort it based on name? – m0bi5 Oct 24 '15 at 06:42
  • Canonical NPE answer http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it provides good information on debugging it. If you can't find problem please simplify your code to fit in post without scrolling (check [MCVE] for guidance). – Alexei Levenkov Oct 24 '15 at 06:45
  • 1
    So where are you getting the NPE at? – Makoto Oct 24 '15 at 06:46
  • First thing you should do is define a class BankAccountInformation with fields having proper types (i.e. double when the information is a double, instead of a String) and use a 1-dimensional array (or better List) of BankAccountInformation instead of a 2D array. – JB Nizet Oct 24 '15 at 07:17
  • Please add stacktrace report to question – Shadow Droid Oct 24 '15 at 10:13
  • Based on what do you want to sort? – iskandarchacra Oct 24 '15 at 13:35
  • Sorry I'm trying to sort it by account balance, highest to lowest, also I'm getting the error at the Double.valueOf(second[3]) line – Albert Liu Oct 24 '15 at 14:21

0 Answers0