Results 1 to 6 of 6
  1. #1
    andre s is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default NPE passing Hashmap

    homework due in 8 hrs and stuck for 3 days now :(
    getting npe when trying to pass a HashMap to a method.
    specs are to use a raw Hashmap as parameter.

    here is 2 classes of code.
    Java Code:
    package a00645410_andresiefert_assignment2_driver;
    import java.util.HashMap;
    import a00645410_andresiefert_assignment2_data.*;
    import a00645410_andresiefert_assignment2_reader.InputReader;
    
    /**
     * ATM class, The COMP 1451 Assignment application driver
     */
    
    /**
     * @author Andre Siefert
     * @version 1.0
     *
     */
    public class ATM {
    
    	private InputReader reader;
    	private String accountNumber;
    	private String passcode;
    	private boolean customerVerified;
    
    	private Bank theBank;
    	private BankCustomer currentCustomer;
    	private BankReport report;
    
    	/**
    	 * Default constructor. Calls the initialize() method to seed the Bank with
    	 * some BankCustomers. Calls the run() method to perform the primary program
    	 * functions.
    	 */
    	public ATM() {
    		super();
    		initialize();
    		run();
    	}
    
    	/**
    	 * Main method calls the class default constructor.
    	 * @param args for program arguments (not used)
    	 */
    	public static void main(String[] args) {
    
    		new ATM();
    
    	}
    
    	/**
    	 * The primary application processor. All application functions are called
    	 * from here. Uses a loop to prompt users to perform banking transactions. 
    	 * Must use switch/case selection to determine uses choices.
    	 */
    	public void run() {
    
    		reader = new InputReader();
    		boolean exit = false;		
    		
    		System.out.println("Welcome to Broke Mans Bank.");
    
    		while (!exit) {
    			System.out.println("Choose one of the following options:");
    			System.out.println("1 - Sign In");
    			System.out.println("2 - Deposit");
    			System.out.println("3 - Withdraw");
    			System.out.println("4 - Display Account Info");
    			System.out.println("5 - Exit");
    			System.out.print("> ");
    			int choice = reader.getIntInput();
    
    			switch (choice) {
    
    			case 1:
    				choice = 1;
    				verifyCustomer();
    				break;
    			case 2:
    				choice = 2;
    				transactDeposit();
    				break;
    			case 3:
    				choice = 3;
    				transactWithdraw();
    				break;
    			case 4:
    				choice = 4;
    				displayAccountInformation();
    				break;
    			case 5:
    				choice = 5;
    				System.out.println("Thank you for banking at Bullwinkle's Bank");
    				System.out.println("DEBUG: Displaying all the accounts in the bank.");
    				theBank.displayAllCustomers();
    				System.exit(0);
    			}
    		}
    	}
    
    	/**
    	 * Adds Customer references to the Back HashMap as seed data for testing.
    	 */
    	public void initialize() {
    
    		theBank = new Bank();
    		theBank.createAccount(new BankCustomer("Darby", "Dog", "123", new SavingsAccount(120, "SA-123"), 75));
    		theBank.createAccount(new BankCustomer("Myia", "Dog", "123", new ChequingAccount(26, "CH-123"), 53));
    		theBank.createAccount(new BankCustomer("Freckle", "Cat", "123", new GoldAccount(69, "GL-123"), 66));
    		theBank.createAccount(new BankCustomer("Dan", "Duck", "123", new GoldAccount(466, "GL-1234"), 46));
    		theBank.deactivateAccount("SA-1234");
    	}
    
    	/**
    	 * Performs a deposit into a BankCustomer's account. Checks to see if the
    	 * user has signed in. If not, then verifyCustomer() is called and the menu
    	 * is displayed again.
    	 */
    	public void transactDeposit() {
    
    		if (customerVerified) {
    			System.out.println("Enter the amount to deposit: ");
    			double amount = reader.getDoubleInput();
    			currentCustomer.myAccount.addToBalance(amount);
    						
    		} else {
    			System.out.println("ERROR: You must LOGIN before you can perform a transaction.");
    			verifyCustomer();
    		}
    	}
    
    	/**
    	 * Performs a withdrawal from a BankCustomer's account. Checks to see if the
    	 * user has signed in. If not, then verifyCustomer() is called and the menu
    	 * is displayed again.
    	 */
    	public void transactWithdraw() {
    
    		if (customerVerified) {
    			System.out.println("Enter the amount to withdraw: ");
    			double amount = reader.getDoubleInput();
    			currentCustomer.myAccount.subtractFromBalance(amount);
    		
    		} else {
    			System.out.println("ERROR: You must LOGIN before you can perform a transaction.");
    			verifyCustomer();
    		}
    
    	}
    
    	/**
    	 * Displays a BankCustomer's account information if the customer has been
    	 * previously verified.
    	 */
    	public void displayAccountInformation() {
    
    		if (customerVerified) {
    			System.out.println("Here is your information.");
    			theBank.displayCustomerInformation(currentCustomer);
    		
    		} else {
    			System.out.println("ERROR: You must LOGIN before you can perform a transaction.");
    			verifyCustomer();
    		}
    
    	}
    
    	/**
    	 * To confirm a BankCustomer's account number and passcode. Called when the
    	 * user is required to sign in to the application. Will set a boolean so the
    	 * user does not have to sign in again during the session.
    	 */
    	public void verifyCustomer() {
    
    		System.out.println("Enter Account Number: ");
    		accountNumber = reader.getStringInput();
    		System.out.println("Enter Passcode: ");
    		passcode = reader.getStringInput();
    
    		if (passcode.equalsIgnoreCase("ADMIN") && accountNumber.equalsIgnoreCase("ADMIN"))
    		{
    			subMenu();
    		}
    
    		BankCustomer customer = Bank.theBank.get(accountNumber.toUpperCase());
    
    		if (customer != null) {
    			
    			if (passcode.equals(customer.getPasscode())) {
    
    				customerVerified = true;
    				currentCustomer = customer;
    			} 
    			else {
    				System.out.println("ERROR: Either account number or passcode is not correct.");
    				run();
    			}
    		} 
    		else {
    			System.out.println("ERROR: Either account number or passcode is not correct.");
    			run();
    		}
    	}
    	
    	public void subMenu() {
    		
    		
    		boolean exit = false;
    
    		while (!exit) {
    			System.out.println("Choose one of the following options:");
    			System.out.println("1 - Display by Code");
    			System.out.println("2 - Display all Codes");
    			System.out.println("3 - Display Inactive Codes");
    			System.out.println("4 - Display Account Totals");
    			System.out.println("5 - Exit");
    			System.out.print("> ");
    			
    			int choice = reader.getIntInput();
    			
    			
    			switch (choice) {
    
    			case 1:
    				choice = 1;
    				System.out.println("Enter Your prefix");
    				System.out.print("> ");
    				String prefix = reader.getStringInput().toUpperCase();
    				HashMap map = theBank.getTheBank();
    				for (Object object : map.values()) {
    					System.out.println(object);
    				}
    	--------->   	if (map.isEmpty() != true) {
    					report.displayByCode(map, prefix);
    				}
    				
    				break;
    			case 2:
    				choice = 2;
    				transactDeposit();
    				break;
    			case 3:
    				choice = 3;
    				transactWithdraw();
    				break;
    			case 4:
    				choice = 4;
    				displayAccountInformation();
    				break;
    			case 5:
    				choice = 5;
    				System.out.println("Thank you for banking at Broke Mans Bank");
    				System.exit(0);
    			}
    			
    		}
    	}
    }
    
    
    /**
     * class to implement the Reporter interface 
     */
    package a00645410_andresiefert_assignment2_data;
    
    import java.util.HashMap;
    import interfaces.Reporter;
    
    /**
     * @author A00645410 Andre Siefert
     * @version 16/03/17
     */
    public class BankReport implements Reporter {
    	
    	private HashMap<String, BankCustomer> map = new HashMap<String, BankCustomer>();
    	/**
    	 * default constructor
    	 */
    	public BankReport() {
    		super();
    	}
    	
    	/**
    	 * displays all Accounts with a specific prefix
    	 * @param records the HashMap to search
    	 * @param prefix the prefix to search for
    	 */
    	@Override
    	public void displayByCode(HashMap records, String prefix) {
    		if (records != null) {
    			System.out.println(prefix);
    			
    		}
    		else {
    			System.out.println("error");
    		}
    	}
    
    	/**
    	 * display and sort all accounts that are active
    	 * @param records the HashMap to search
    	 */
    	@Override
    	public void displayAllCodes(HashMap records) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	/**
    	 * display and sort all inactive Accounts
    	 * @param records the HashMap to search
    	 */
    	@Override
    	public void displayInactiveCodes(HashMap records) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	/**
    	 * @return the map
    	 */
    	public HashMap<String, BankCustomer> getMap() {
    		return map;
    	}
    
    	/**
    	 * @param map the map to set
    	 */
    	public void setMap(HashMap<String, BankCustomer> map) {
    		if (map != null) {
    			this.map = map;
    		}
    	}
    	
    	
    }
    Last edited by Norm; 03-26-2017 at 11:09 PM. Reason: Added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: NPE passing Hashmap

    getting npe
    Please copy the full text of the error message and paste it here. It has important info about the error.

    What variable has the null value?

    In the future Please wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    andre s is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default Re: NPE passing Hashmap

    Welcome to Broke Mans Bank.
    Choose one of the following options:
    1 - Sign In
    2 - Deposit
    3 - Withdraw
    4 - Display Account Info
    5 - Exit
    > 1
    Enter Account Number:
    admin
    Enter Passcode:
    admin
    Choose one of the following options:
    1 - Display by Code
    2 - Display all Codes
    3 - Display Inactive Codes
    4 - Display Account Totals
    5 - Exit
    > 1
    Enter Your prefix
    > sa
    BankCustomer [firstName=DAN, lastName=DUCK, passcode=123, age=46]
    BankCustomer [firstName=FRECKLE, lastName=CAT, passcode=123, age=66]
    BankCustomer [firstName=DARBY, lastName=DOG, passcode=123, age=75]
    BankCustomer [firstName=MYIA, lastName=DOG, passcode=123, age=53]
    Exception in thread "main" java.lang.NullPointerException
    at a00645410_andresiefert_assignment2_driver.ATM.subM enu(ATM.java:234)
    at a00645410_andresiefert_assignment2_driver.ATM.veri fyCustomer(ATM.java:182)
    at a00645410_andresiefert_assignment2_driver.ATM.run( ATM.java:77)
    at a00645410_andresiefert_assignment2_driver.ATM.<ini t>(ATM.java:38)
    at a00645410_andresiefert_assignment2_driver.ATM.main (ATM.java:47)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: NPE passing Hashmap

    Line 230 - Where do you create an instance of the class report?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: NPE passing Hashmap

    At line 234, What variable has the null value?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: NPE passing Hashmap

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Help with HashMap?
    By sim18 in forum New To Java
    Replies: 7
    Last Post: 05-03-2012, 08:36 AM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  3. passing hashmap from a method and call it within main
    By rajuchacha007 in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 09:41 AM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •