Go Back   Java Programming Forums > Java Standard Edition Programming Help > Exceptions


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 26-04-2009, 12:31 PM
RoadRunner's Avatar
Junior Member
 

Join Date: Apr 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
RoadRunner is on a distinguished road
Question Null Pointer Exception runtime error

Please can sum1 help me with the above error in this code:
Java 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
	readList(carList);
	
		//menu options
		do
			{
			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();
			System.out.println();
			
			switch(choice)
				{
				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)
	{
	try
		{
		//create a file writer
		FileWriter carFile = new FileWriter("Car.txt");
		//create a print writer
		PrintWriter carWriter = new PrintWriter(carFile);
		for(Car item: carListIn)
			{
			carWriter.println(item.getRegistration());
			carWriter.println(item.getMake());
			carWriter.println(item.getPrice());
			}
		carWriter.close();
		}
		
		//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;
	
		try
			{
			//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);
				carListIn.add
								(new Car(tempReg, tempMake, tempPrice));
								tempReg = carStream.readLine();
				}
				carStream.close();
						
			}
			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; 26-04-2009 at 01:24 PM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 26-04-2009, 06:21 PM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
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

Chris
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Navigate to a URL with Runtime.getRuntime().exec() Flash Java Code Snippets and Tutorials 4 06-10-2009 12:18 AM
[SOLVED] Interesting error cipher Koren3 Java Networking 0 29-04-2009 02:54 PM
[SOLVED] Error Koren3 What's Wrong With My Code? 15 02-04-2009 08:20 AM
Regarding logical error haygaurav What's Wrong With My Code? 0 04-03-2009 11:58 AM
Exception handling AnithaBabu1 Exceptions 6 27-08-2008 02:37 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:03 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.