Results 1 to 5 of 5
  1. #1
    javano0b is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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.

    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 
    			
    	}
    
    }
    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.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default 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!

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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... ;)

  4. #4
    javano0b is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: NulPointerException wont go away

    ok i changed it to

    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 
    			
    	}
    
    }
    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.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: NulPointerException wont go away

    And you call exchange[c].getchoice(); twice, because... of what?

    I find the whole structure disturbing... you have an array that uses c you get while using an object of that array. Is that really what you intend to do?

Similar Threads

  1. Why wont it work???
    By fredrik8719 in forum New To Java
    Replies: 0
    Last Post: 04-08-2012, 07:38 PM
  2. this wont run again and again?? why?
    By vibaviattigala in forum New To Java
    Replies: 2
    Last Post: 07-16-2011, 08:30 AM
  3. .jar wont run?
    By stevenpalomino in forum New To Java
    Replies: 1
    Last Post: 06-30-2011, 10:11 AM
  4. Able to compile OK, But wont run..Help
    By subiedude101 in forum New To Java
    Replies: 2
    Last Post: 02-20-2011, 08:37 PM
  5. Why wont this compile?
    By Student101 in forum New To Java
    Replies: 8
    Last Post: 11-18-2010, 05:33 AM

Tags for this Thread

Posting Permissions

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