Results 1 to 8 of 8
Thread: null pointer exception
- 04-06-2009, 02:14 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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); } }
-
what line throws the NPE?
The more information you can give about the problem, usually the easier it is to help you.
- 04-06-2009, 02:32 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
what you really want is thisJava 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;
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.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); }
Hope this helps,
Singing BoyoLast 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!
- 04-06-2009, 02:33 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
-
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.
- 04-06-2009, 02:45 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
- 04-06-2009, 03:08 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
- 04-06-2009, 03:25 AM #8
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 06:33 PM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks