Results 1 to 8 of 8
  1. #1
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Exclamation null pointer exception

    When I run it and make a deposit or withdrawal I get a null pointer exception. I was wondering how I could fix this??? Thanks

    Java Code:
    import javax.swing.JOptionPane;
    
    public class A5Q1ummadhoa{
         public static void main(String[] arguments){
           
           String[] accountNumber = new String[100];
           Double[] accountBalance = new Double[100];
           String[] firstName = new String[100];
           String[] lastName = new String[100];
           Double[] creditLimit = new Double[100];
           String[] choices = {"Create Account","Deposit","Withdraw","Quit"};
           String accountNum = "";
           int numAccounts = 0;
           double result;
           Boolean done = false;
           Boolean isValid = false;
    
           for(int i=0;i<1000 && !done;i++){
           String input = (String)JOptionPane.showInputDialog(null,"Select an option:","Main Menu",JOptionPane.QUESTION_MESSAGE,null,choices,choices[0]);
                System.out.println("* You selected: " + input);
                if(input.equals(choices[0])){
                  createNewCustomer(accountNumber[i],accountBalance[i],firstName[i],lastName[i],creditLimit[i]);
                  //accountNumber[numAccounts];
                  numAccounts++;
                  accountBalance[i]=0.00;
                  creditLimit[i]=100.00;
                }
                if(input.equals(choices[1])){
                  accountNum = JOptionPane.showInputDialog("What is your account number?");
                  getAccountIndex(accountNum, accountNumber);
                  result = makeDeposit(accountNumber[i],accountBalance[i], creditLimit[i]);
                  accountBalance[i]+=result;
                    creditLimit[i]=accountBalance[i]*0.10;
                }
                if(input.equals(choices[2])){
                  accountNum = JOptionPane.showInputDialog("What is your account number?");
                  getAccountIndex(accountNum, accountNumber);
                  result = makeWithdrawal(accountNumber[i],accountBalance[i], creditLimit[i]);
                  accountBalance[i]-=result;
                    creditLimit[i]=accountBalance[i]*0.10;
                  
                }
                if(input.equals(choices[3])){
                  printAllAccounts(accountNumber,accountBalance,firstName,lastName,creditLimit);
                  done = true;
                }
           }
           System.out.println();
           System.out.println("The program terminated normally.");
         }
         
         public static String createNewCustomer(String accountNumber,Double accountBalance,String firstName,String lastName,Double creditLimit){
           String result = "";
           
           final String BANK_ID = "A4326";
           firstName = JOptionPane.showInputDialog("Please enter your first name:");
           lastName = JOptionPane.showInputDialog("Please enter your last name:");
           accountNumber = generateAccountNumber(firstName, lastName);
           accountBalance = 0.00;
           creditLimit = 100.00;
           
           printAccount(accountNumber,accountBalance,firstName,lastName,creditLimit);
           
           return result;
         }
         
         public static void printAccount(String accountNumber,Double accountBalance,String firstName,String lastName,Double creditLimit){
           
           System.out.println(lastName + ", " + firstName);
           System.out.println("Account Number: " + accountNumber);
           System.out.println("Account Balance: " + "$" + roundDouble(accountBalance,2));
           System.out.println("Credit Limit: " + "$" + roundDouble(creditLimit,2));
           
         }
         
         public static void printAllAccounts(String[] accountNumber,Double[] accountBalance,String[] firstName,String[] lastName,Double[] creditLimit){
           
           System.out.println("*** Summary of Accounts ***" + "\nBank ID: A4326");
           System.out.println();
           System.out.println(lastName + ", " + firstName);
           System.out.println(accountNumber);
           System.out.println(accountBalance);
           System.out.println(creditLimit);
           
           
         }
         
         public static double makeDeposit(String accountNumber,Double accountBalance,Double creditLimit){
           Double result = 0.00;
           String deposit = "";
             
           deposit = JOptionPane.showInputDialog("How much money would you like to deposit? (e.g. ##.##)");
           result = Double.parseDouble(deposit);
             
             return result;
         }
         
         public static double makeWithdrawal(String accountNumber,Double accountBalance,Double creditLimit){
           Double result = 0.00;
           String withdraw = "";
             
           withdraw = JOptionPane.showInputDialog("How much money would you like to withdraw? (e.g. ##.##)");
           result = Double.parseDouble(withdraw);
           
             return result;
         }
         
         public static int getAccountIndex(String accountNum, String accountNumber[]){
           int result = -1;
           
           for(int i=0;i<accountNumber.length;i++){
            
             if(accountNum.equals(accountNumber[i])){
              result = i; 
             }
           }
           return result;
         }
         
         
         
         public static String generateAccountNumber(String firstName, String lastName){
        String bankId = "A4326";
          String result = "";
          final String BANK_ID = "A4326";
          int[]randNums4 = new int[1000];
          int[]randNums8 = new int[1000];
          int i;
          
          for(i = 0;i<randNums4.length-1;i++){
           randNums4[i] = (int)(9000.0*Math.random()+1000);
          }
          
          for(i = 0;i<randNums8.length-1;i++){
            randNums8[i] = (int)(90000000.0*Math.random()+10000000);
          }
           result += BANK_ID+"-"+lastName.charAt(0)+firstName.charAt(0)+"-"+randNums4[i-1]+"-"+Character.toUpperCase(lastName.charAt(1))+Character.toUpperCase(firstName.charAt(1))+"-"+randNums8[i-1];
    
         return result;  
         }
         
         public static final double roundDouble(double d, int places) {
            return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10,
                (double) places);
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    what line throws the NPE?

    The more information you can give about the problem, usually the easier it is to help you.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    you're forgetting to set the array values.
    when you declare your arrays you also need a for loop to set the values

    you set them using this
    Java Code:
           String[] accountNumber = new String[100];
           Double[] accountBalance = new Double[100];
           String[] firstName = new String[100];
           String[] lastName = new String[100];
           Double[] creditLimit = new Double[100];
           String[] choices = {"Create Account","Deposit","Withdraw","Quit"};
           String accountNum = "";
           int numAccounts = 0;
           double result;
           Boolean done = false;
           Boolean isValid = false;
    what you really want is this
    Java Code:
    String[] accountNumber = new String[100];
           Double[] accountBalance = new Double[100];
           String[] firstName = new String[100];
           String[] lastName = new String[100];
           Double[] creditLimit = new Double[100];
           String[] choices = {"Create Account","Deposit","Withdraw","Quit"};
           String accountNum = "";
           int numAccounts = 0;
           double result;
           Boolean done = false;
           Boolean isValid = false;
           
           for(int i=0; i<100; i++){
                  accountBalance[i] = new Double(0);
                  firstName[i] = "";
                  lastName[i] = "";
                  creditLimit[i] = new Double(0);
           }
    As it was explained to me, creating arrays using something like Double[] a = new Double[100] simply allocates memory. To use the array, you need to set the values as well.

    Hope this helps,
    Singing Boyo
    Last edited by Singing Boyo; 04-06-2009 at 02:36 AM. Reason: mistake in original, treated Double as primitive double
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    And next time, do indicate the line throwing the exception. I had to copy the code into my IDE and compile it to find the line.(a very tedious process, as my CPU is very slow right now)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Actually, the real problem is that you are using array of Double which is a wrapper class for the double primitive. Don't do this. Instead if you use arrays of double (note that it's not capitalized), your code should work fine.

    I would use a text editor to search out every occurrence of Double and change it to double.
    Last edited by Fubarable; 04-06-2009 at 02:40 AM.

  6. #6
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    what line throws the NPE?

    The more information you can give about the problem, usually the easier it is to help you.
    Java Code:
    accountBalance[i]+=result;

  7. #7
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Singing Boyo View Post
    you're forgetting to set the array values.
    when you declare your arrays you also need a for loop to set the values

    you set them using this
    Java Code:
           String[] accountNumber = new String[100];
           Double[] accountBalance = new Double[100];
           String[] firstName = new String[100];
           String[] lastName = new String[100];
           Double[] creditLimit = new Double[100];
           String[] choices = {"Create Account","Deposit","Withdraw","Quit"};
           String accountNum = "";
           int numAccounts = 0;
           double result;
           Boolean done = false;
           Boolean isValid = false;
    what you really want is this
    Java Code:
    String[] accountNumber = new String[100];
           Double[] accountBalance = new Double[100];
           String[] firstName = new String[100];
           String[] lastName = new String[100];
           Double[] creditLimit = new Double[100];
           String[] choices = {"Create Account","Deposit","Withdraw","Quit"};
           String accountNum = "";
           int numAccounts = 0;
           double result;
           Boolean done = false;
           Boolean isValid = false;
           
           for(int i=0; i<100; i++){
                  accountBalance[i] = new Double(0);
                  firstName[i] = "";
                  lastName[i] = "";
                  creditLimit[i] = new Double(0);
           }
    As it was explained to me, creating arrays using something like Double[] a = new Double[100] simply allocates memory. To use the array, you need to set the values as well.

    Hope this helps,
    Singing Boyo
    this seems to work and I don't have the NPE error. The problem is that it is not recording the account number into the array. When I want it to print accountNumber[i] it prints nothing!

  8. #8
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  3. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 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
  •