Results 1 to 5 of 5
Thread: NulPointerException wont go away
- 04-14-2012, 09:55 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
NulPointerException wont go away
This program is a currency converter there are two files the foreign.java does all the calculations and asks the questions and what not. That part works fine but now i have to is when the person decides they are done the program has to ask if they want a summary report and then if they do have it print out the conversion dollars and amount.
I know there are things that are missing I took some stuff out to get it so there were no more errors but now i get the NullPointerException and I dont know what to do any ideas? Also i didnt include the foreign.java that is being called because i havent made any changes to that.Java Code:import java.util.Scanner; import java.text.NumberFormat; public class lab9 { public static void main( String [] args) { //foreign exchange;// final int Max = 20; foreign[] exchange = new foreign[Max]; Scanner scan = new Scanner (System.in); String another = "Y"; int c=0; do { foreign.titles(); foreign.menu(); //static methods called before the object is initiated //allows us to call the other methods exchange[c].getchoice(); if (c != 0) { exchange[c]= new foreign(); exchange[c].dollars(); exchange[c].calculate(); exchange[c].value(); System.out.println(exchange); //above call methods from the other class } } while (c != 0); //tells the program to continue calculating as long as 0 is not selected System.out.println("display a summary report? Y/N"); another = scan.nextLine(); System.out.print("\nItemt\t\tConversion\t\t\tDollars\t\t\tAmount"); for (int i=0; i<c; i++) System.out.println(exchange[i]); foreign.displaycount(); //this static method is only called when the choice is 0 which is the end of the program } }
- 04-14-2012, 10:18 PM #2
Re: NulPointerException wont go away
if you want make a call to getchoice() in line 29 the element exchange[c] must exist. i can't see any initialization for the array exchange so a call with exchange[c] will cause a NullPointerException, because all array elements are initialized per default with null!
- 04-14-2012, 10:20 PM #3
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: NulPointerException wont go away
In case you have such problem again you should also get the line in which the exception occurrs, right? If not you should determine that line - it helps you and us... ;)
- 04-14-2012, 11:24 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: NulPointerException wont go away
ok i changed it to
this allows me to run it and make a selection based on the menu but it wont run the if statement. (ie i select country 1 it should then ask for the dollar amount i want to convert them run the appropriate calculation.) How can i get the choice selected from the getchoice method to be the value for c so that the if statement will work.Java Code:import java.util.Scanner; import java.text.NumberFormat; public class lab9 { public static void main( String [] args) { //foreign exchange;// final int Max = 20; foreign[] exchange = new foreign[Max]; Scanner scan = new Scanner (System.in); String another = "Y"; int c=0; do { foreign.titles(); foreign.menu(); //static methods called before the object is initiated exchange[c]= new foreign(); //allows us to call the other methods exchange[c].getchoice(); c = exchange[c].getchoice(); if (c != 0) { exchange[c].dollars(); exchange[c].calculate(); exchange[c].value(); System.out.println(exchange); //above call methods from the other class } } while (c != 0); //tells the program to continue calculating as long as 0 is not selected System.out.println("display a summary report? Y/N"); another = scan.nextLine(); System.out.print("\nItemt\t\tConversion\t\t\tDollars\t\t\tAmount"); for (int i=0; i<c; i++) System.out.println(exchange[i]); foreign.displaycount(); //this static method is only called when the choice is 0 which is the end of the program } }
- 04-15-2012, 11:48 AM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Similar Threads
-
Why wont it work???
By fredrik8719 in forum New To JavaReplies: 0Last Post: 04-08-2012, 07:38 PM -
this wont run again and again?? why?
By vibaviattigala in forum New To JavaReplies: 2Last Post: 07-16-2011, 08:30 AM -
.jar wont run?
By stevenpalomino in forum New To JavaReplies: 1Last Post: 06-30-2011, 10:11 AM -
Able to compile OK, But wont run..Help
By subiedude101 in forum New To JavaReplies: 2Last Post: 02-20-2011, 08:37 PM -
Why wont this compile?
By Student101 in forum New To JavaReplies: 8Last Post: 11-18-2010, 05:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks