Results 1 to 7 of 7
  1. #1
    RoadRunner's Avatar
    RoadRunner is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Question Java Runtime Error! :(

    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

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    tempStringPrice seems to be null.

  3. #3
    RoadRunner's Avatar
    RoadRunner is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    thnx for the reply, but could u elaborate on this a bit as i've tried putting

    String tempStringPrice= (+ a string)

    but this doesn't seem to work

  4. #4
    oyunfixosa is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default oyun

    Thank you site admin.Very god :)

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by RoadRunner View Post
    thnx for the reply, but could u elaborate on this a bit as i've tried putting

    String tempStringPrice= (+ a string)

    but this doesn't seem to work
    Why don't you put some println statements for each value of the four variables you have declared inside your readList() method? That would tell you what really is getting read and stored into them.

    As per you code, it seems like the 3rd line of your "Car.txt" is messing it up. Because that's where tempStringPrice is getting it's value, which probably is null.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Fubarable View Post
    Check the date of the question as it's a re-animated zombie thread; one that I'm thinking of locking.
    OOPS, I didn't notice that.

    Early morning effect. I think I need some coffee :)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. runtime error problems. please help!
    By RoadRunner in forum Forum Lobby
    Replies: 0
    Last Post: 04-21-2009, 05:03 PM
  2. I can't fix this runtime error in my program
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 04:29 PM
  3. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  4. Weblogic 5.0 runtime error with EJB 1.0
    By somesh in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-25-2008, 03:09 PM
  5. Runtime error : BOOTSTRAP server
    By vishnuvardhan.h in forum New To Java
    Replies: 0
    Last Post: 08-12-2008, 10:59 AM

Posting Permissions

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