Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Getting Null Pointer Exception in runtime

  1. #1
    Junior Member RoadRunner's Avatar
    Join Date
    Apr 2009
    Thanked 0 Times in 0 Posts

    Question Getting Null Pointer Exception in runtime

    Please can sum1 help me with the above error in this code:
    import java.util.*; //required for List and ArrayList
    import java.io.*; //required for IOException
    public class TextFileTester
    public static void main(String[] args)
    	char choice;
    	//create an object of type list to store the cars in
    	List<Car> carList = new ArrayList<Car>();
    	//read the list from file when program starts
    		//menu options
    			System.out.println("\nText File Tester");
    			System.out.println("1. Add a car");
    			System.out.println("2. Remove a car");
    			System.out.println("3. List all cars");
    			System.out.println("4. Quit\n");
    			choice = KBInput.readChar();
    				case '1': addCar(carList);break;
    				case '2': removeCar(carList);break;
    				case '3': listAll(carList);break;
    				case '4': writeList(carList);break;
    				default: System.out.println("Choose an option between 1-4");
    		while(choice != '4');
    //method for adding a new car to the system
    private static void addCar(List<Car> carListIn)
    	String tempReg;
    	String tempMake;
    	double tempPrice;
    	System.out.println("Enter the veichle reg");
    	tempReg = KBInput.readString();
    	System.out.println("Enter the veichle make");
    	tempMake = KBInput.readString();
    	System.out.println("Enter veichle price");
    	tempPrice = KBInput.readDouble();
    	carListIn.add(new Car(tempReg, tempMake, tempPrice));
    //method for removing a car from the list
    private static void removeCar(List<Car> carListIn)
    	int pos;
    	System.out.println("Enter the position of the car to be removed");
    	pos = KBInput.readInt();
    	carListIn.remove(pos -1);
    //method for show all the cars in the list
    private static void listAll(List<Car> carListIn)
    		for(Car item: carListIn)
    			System.out.println(item.getRegistration() + ""
    			+ item.getMake() + "" + item.getPrice());
    //method for writing car list to file
    private static void writeList(List<Car> carListIn)
    		//create a file writer
    		FileWriter carFile = new FileWriter("Car.txt");
    		//create a print writer
    		PrintWriter carWriter = new PrintWriter(carFile);
    		for(Car item: carListIn)
    		//handle the exceptions thrown up by the file writer
    			catch(IOException e)	
    				System.out.println("There was a problem writing the file");
    //method for reading the car file
    private static void readList(List<Car> carListIn)
    	String tempReg;
    	String tempMake;
    	String tempStringPrice;
    	double tempPrice;
    			//create a file reader
    			FileReader carFile = new FileReader("Car.txt");
    			//create a buffer for reader
    			BufferedReader carStream = new BufferedReader(carFile);
    			tempReg = carStream.readLine();
    			while (tempReg != null);
    				tempMake = carStream.readLine();
    				tempStringPrice = carStream.readLine();
    				// converting price from text to double
    				tempPrice = Double.parseDouble(tempStringPrice);
    								(new Car(tempReg, tempMake, tempPrice));
    								tempReg = carStream.readLine();
    			catch (FileNotFoundException e)
    				System.out.println("File could not be found");
    			catch (IOException e)
    				System.out.println("There was a problem reading the file");
    The error:

    ----jGRASP exec: java TextFileTester

    Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:991)
    at java.lang.Double.parseDouble(Double.java:510)
    at TextFileTester.readList(TextFileTester.java:119)
    at TextFileTester.main(TextFileTester.java:12)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    from another forum i believe the variable tempStringPrice has a null value but i'm not sure what to set it to. Any help would be much appreciated
    Last edited by RoadRunner; April 26th, 2009 at 08:24 AM.

  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    My Mood
    Thanked 105 Times in 90 Posts

    Default Re: Null Pointer Exception runtime error

    Looks to me like you are trying to assign something within the file to it, but inactualy fact you are not assigning anything to it. Thus when you try to use it later (as you never initialised the String) it is throwing a null pointer exception


Similar Threads

  1. How to solve NullPointerException in runtime?
    By Koren3 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: September 20th, 2017, 11:25 PM
  2. How to Navigate to a URL with Runtime.getRuntime().exec()
    By Flash in forum Java SE API Tutorials
    Replies: 4
    Last Post: October 5th, 2009, 07:18 PM
  3. [SOLVED] Interesting error cipher while sending message
    By Koren3 in forum Java Networking
    Replies: 0
    Last Post: April 29th, 2009, 09:54 AM
  4. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 07:58 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM