so i've wrote this code in java and it compiles fine but there is a runtime error of which my knowledge of the language is not good enough to work it out. Here is the 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("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");
}
}
}
and here is 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.
btw im using a seperate program called KBInput to read keyboard strokes. Any help would be much appreciated thnx

