Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Need some help with my Banking program

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Need some help with my Banking program

    Hey all. I'm having a little problem with my code. The program is supposed to keep track of different customers banking accounts in an arraylist, and have the ability to add a new customer and account, search for any account, make a deposit to any account, make a withdrawal from any account, and print out all accounts. There seems to be a problem with the addAccount method, because whenever I try to add an account it gives me the following error:

    Exception in thread "main" java.lang.NullPointerException
    at customer.Bank.search(Bank.java:22)
    at customer.Bank.addAccount(Bank.java:34)
    at customer.BankTest.choiceCheck(BankTest.java:56)
    at customer.BankTest.main(BankTest.java:27)

    It seems to be based in the search method in the Bank class, as thats where the error is pointing me too. Can anyone give me a clue as to what I am doing wrong. I've commented next to line of code where the problem starts.

    import java.util.*;
    public class BankTest {
        public static void main(String[] args){
            Scanner keyboard = new Scanner(System.in);
            String input;
     
            do{
                System.out.println("Welcome to Fictional Bank. \nPlease choose"
                        + "from one of the options below. \n");
                System.out.println("Type 'a' to add a new customer and account. "
                        + "\nType 'd' to make a deposit to an account. "
                        + "\nType 'w' to make a withdrawl from an account. "
                        + "\nType 'f' to find an account."
                        + "\nType 'p' to print all of the accounts."
                        + "\nType 'q' to quit.");
                input = keyboard.nextLine();
                choiceCheck(input, keyboard);
                }while(!input.equals("q"));
     
     
        }
     
        public static void choiceCheck(String input, Scanner keyboard){
            String name;
            String address;
            int age;
            String telephoneNumber;
            double balance;
     
            if (input.equals("a")) {
                System.out.println("What is the customer's name?");
                name = keyboard.nextLine();
                System.out.println("What is the customer's address?");
                address = keyboard.nextLine();
                System.out.println("What is the customer's age?");
                age = keyboard.nextInt();
                System.out.println("What is the  customer's telephone number?");
                telephoneNumber = keyboard.next();
     
                Customer newCust = new Customer(name, address, age,
                            telephoneNumber);
                System.out.println("How much did you want to add to the "
                            + "account");
                balance = keyboard.nextDouble();
     
                Bank.addAccount(newCust, balance, keyboard);
     
            }
            else if (input.equals("d")){
                System.out.println("What is the customer's name?");
                name = keyboard.nextLine();
     
                Customer tempCust = new Customer(name, null, 0, null);
     
                Bank.makeDeposit(tempCust, keyboard);
            }
            else if (input.equals("w")){
                System.out.println("What is the customer's name?");
                name = keyboard.nextLine();
     
                Customer tempCust = new Customer(name, null, 0, null);
     
                Bank.makeWithdrawl(tempCust, keyboard);
            }
            else if (input.equals("f")){
                System.out.println("What is the customer's name?");
                name = keyboard.nextLine();
     
                Customer tempCust = new Customer(name, null, 0, null);
     
                Bank.getAccount(tempCust, keyboard);
            }
        }
     
     
    }

    import java.util.*;
    public class Bank {
        private static ArrayList<Account> accountList;
     
        public Bank(){
            accountList = new ArrayList<>();
        }
     
        private static int search(Customer c){
            int i;
            Customer tempCust = new Customer(c.getName(), null, 0, null);
            for (i = 0; i < accountList.size(); i++){                      //****This is the problem code****//
                if (accountList.get(i).equals(tempCust)) {
                    return i;
                }
            }
            return -1;
        }
     
        public static boolean addAccount(Customer c, double b, Scanner keyboard){
            System.out.println("What kind of account would you like to open? "
                    + "\n Type 'c' for checking and 's' for saving.");
            String input = keyboard.nextLine();
            int index = search(c);
            if (input.equals("c")){
                if (index == -1){
                    accountList.add(new CheckingAccount(c, b));
                    return true;
                }
                else {
                    return false;
                }
            }
     
            if (input.equals("s")){
                if (index == -1){
                    accountList.add(new SavingAccount(c, b));
                    return true;
                }
                else {
                    return false;
                }
            }
            return false;
        }
     
        public static boolean makeDeposit(Customer c, Scanner keyboard){
            int index = search(c);
            if (index == -1){
                System.out.println("Customer does not exist in the system.");
                return false;
     
            }
            else{
                System.out.println("How much would you like to deposit?");
                double depositAmount = keyboard.nextDouble();
                double newBalance = (accountList.get(index).getBalance()) + 
                        depositAmount;
                accountList.get(index).setBalance(newBalance);
                System.out.println("The deposit was successful. Your new balance "
                        + "is $" + newBalance);
                return true;
            }
        }
     
        public static boolean makeWithdrawl(Customer c, Scanner keyboard){
            int index = search(c);
            if (index == -1){
                System.out.println("Customer does not exist in the system.");
                return false;
     
            }
            else{
                System.out.println("How much would you like to withdraw?");
                double withdrawalAmount = keyboard.nextDouble();
                double newBalance = (accountList.get(index).getBalance()) - 
                        withdrawalAmount;
                accountList.get(index).setBalance(newBalance);
                System.out.println("The withdraw was successful. Your new balance "
                        + "is $" + withdrawalAmount);
                return true;
            }
        }
     
        public static String getAccount(Customer c, Scanner keyboard){
            int index = search(c);
            if (index == -1) {
                System.out.println("Customer does not exist in the system.");
            }
            else {
                return accountList.get(index).toString();
            }
            return "Please try again";
        }
     
        public static void printAllAccounts(){
            int i;
            for (i = 0; i < accountList.size(); i++){
            System.out.println(accountList.get(i));
            }
        }
     
    }

    public class Customer {
        private String name;
        private String address;
        private int age;
        private String telephoneNumber;
        private int customerNumber;
     
        public Customer(){
     
        }
     
        public Customer(String n, String a, int y, String t){
            name = n;
            address = a;
            age = y;
            telephoneNumber = t;
            customerNumber = customerNumber + 1;
        }
     
        public String getName(){
            return name;
        }
     
        public String getAddress(){
            return address;
        }
     
        public int getAge(){
            return age;
        }
     
        public String getTelephoneNumber(){
            return telephoneNumber;
        }
     
        public int getCustomerNumber(){
            return customerNumber;
        }
     
        public void setname(String n){
            name = n;
        }
     
        public void setAddress(String a){
            address = a;
        }
     
        public void setAge(int y){
            age = y;
        }
     
        public void setTelephoneNumber(String t){
            telephoneNumber = t;
        }
     
        public String toString(){
            return null;
        }
     
        public boolean equals(Customer c){
            return name.equals(c.name);
        }
     
    }

    public abstract class Account{
        Customer customer;
        private double balance;
        private int accountNumber;
     
        public Account(){
     
        }
     
        public Account(Customer c, double b){
            customer = c;
            balance = b;
            accountNumber = accountNumber + 1;
        }
     
        public double getBalance(){
            return balance;
        }
     
        public int getAccountNumber(){
            return accountNumber;
        }
     
        public void setBalance(double b){
            balance = b;
        }
     
        public String toString(){
            return customer + "~" + balance;
        }
    }

    public class CheckingAccount extends Account {
        private final static double MONTHLY_FEE = 0.0;
     
        public CheckingAccount(Customer c, double b){
            super(c, b);
        }
     
        public double getMonthlyFee(){
            return MONTHLY_FEE;
        }
     
        public void deductFee(){
     
        }
     
    }

    public class SavingAccount extends Account {
        private final static double INTEREST_RATE = 0.0;
     
        public SavingAccount(Customer c, double b){
            super(c,b);
        }
        public double getInterestRate(){
            return INTEREST_RATE;
        }
     
        public void depositInterest(){
     
        }
     
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need some help with my Banking program

    The error is reporting a null value. Trace through the code and see which variable has a null value when you expected it to have a valid value. Add println() statements to see the value of the variables in question as you go.

  3. The Following User Says Thank You to jps For This Useful Post:

    bankston13 (September 13th, 2012)

  4. #3
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Need some help with my Banking program

    I can't seem to figure out which null value its referring too. The only nulls are the ones that I pass through to represent the string portion of the customer that I need, and I only pass in null because for the search method I'm only comparing the names on the accounts, so I don't need a value for the other strings. I'm going backwards but I can't seem to pinpoint exactly where the problem is coming from.

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need some help with my Banking program

    The NPE tells you which line is throwing the exception. Usually if you inspect that line carefully (or use println statements *before* the offending line as mentioned above), you'll find out which variable is null. Then check to see where you *think* you initialize the variable.

  6. The Following User Says Thank You to curmudgeon For This Useful Post:

    bankston13 (September 13th, 2012)

  7. #5
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Need some help with my Banking program

    I figured it out. What was confusing me was that it was saying that the for loop was wrong, so I thought it had something to do with the Customer object I was passing. However, since the arrayList is made in the Bank class and not the Customer or Account class, I realized that I had not made a new Bank object, and therefore it had no arrayList to search from. Thats also why none of my println()'s were printing anything.

  8. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need some help with my Banking program

    I'm glad you've got things figured out, but a question: why all the statics in the Bank class? Per my review nothing in Bank should be static, including all methods and all fields. Making all the methods static is likely what lead to your problem, because if none were static, you'd have been forced to create a Bank instance before using Bank methods (which is the correct sequence of things).
    Last edited by curmudgeon; September 13th, 2012 at 08:50 PM.

  9. #7
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Need some help with my Banking program

    Sorry, I had to go with the family somewhere where there was no internet, so I couldnt answer your question. The reason I used statics in my Bank class is because it wouldn't work without them at first. I've taken care of the null problem, and I have updated the original post with new code. My new problem is that I can't seem to figure out what's going on with the search method. I can add people just fine, but when I try searching for them they don't come up in the arrayList, and I also can't deposit or withdraw money. I can also add the same customer twice, so there's definately something wrong with how its searching. Any clues on this one?

  10. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need some help with my Banking program

    Quote Originally Posted by bankston13 View Post
    Sorry, I had to go with the family somewhere where there was no internet, so I couldnt answer your question. The reason I used statics in my Bank class is because it wouldn't work without them at first.
    Then you were fixing it the wrong way. The correct fix would allow the program to work *without* making the members of this class static. Likely you were trying to access one of its methods or fields in a static way, and the correct solution is to make an instance of the class first and access the methods through the instance.

    I've taken care of the null problem, and I have updated the original post with new code. My new problem is that I can't seem to figure out what's going on with the search method. I can add people just fine, but when I try searching for them they don't come up in the arrayList, and I also can't deposit or withdraw money. I can also add the same customer twice, so there's definately something wrong with how its searching. Any clues on this one?
    To prevent confusing future students of Java, please don't alter your original post as it makes the subsequent answers seem strange or irrelevant. Better perhaps is to ask a new question on this site for a new problem and changing your original post back to its original self.

Similar Threads

  1. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  2. Banking System UML Class Diagram in to code.
    By djl1990 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 13th, 2011, 11:55 AM
  3. banking system
    By preeti in forum Java Theory & Questions
    Replies: 3
    Last Post: August 11th, 2011, 01:25 PM
  4. Simple Banking System
    By ShadowKing98 in forum Java Theory & Questions
    Replies: 7
    Last Post: April 12th, 2011, 04:26 AM
  5. Banking Application
    By mbouster in forum Object Oriented Programming
    Replies: 2
    Last Post: January 9th, 2011, 11:23 AM