How to stop a NullPointerException when using BufferedReader (readLine()) to check for any more values

StackOverflow https://stackoverflow.com/questions/21101358

Question

I have written some code to read in Vehicle data from a .txt file. At the moment I can read in all the data correctly and create the objects fine. I have a while loop that checks for the text "Vehicle Details" in the file and if it is present (i.e. there is another vehicle to add) it enters the loop to add the data.

The problem is that when there isn't anymore data the readLine() method is still called and this creates a NullPointerException (at least that's what I think is the cause)

Here is the method in use.

public static void addNewVehicleFromFile(Showroom s)
{
    try
    {
        FileInputStream fStream = new FileInputStream("AddVehicleFromFile.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fStream));
        String stringLine;

        while ((stringLine = br.readLine()).equalsIgnoreCase("Vehicle Details"))
        {
            System.out.println("P1");//Testing
            br.readLine();

            String[] manufacturer = br.readLine().split(" =");
            String[] model = br.readLine().split(" =");
            String[] vin = br.readLine().split(" =");
            String[] dateOfMan = br.readLine().split(" =");
            String[] taxBand = br.readLine().split(" =");
            String[] cost = br.readLine().split(" =");

            System.out.println("P2");//Testing
            System.out.println(manufacturer[0].toString());
            System.out.println(model[0].toString());
            System.out.println(vin[0].toString());
            System.out.println(dateOfMan[0].toString());
            System.out.println(taxBand[0].toString());

            br.readLine();

            System.out.println("P3");//Testing

            int strToInt = Integer.parseInt(cost[0]);

            Vehicle inputVehicle = new Vehicle(manufacturer[0], model[0], vin[0], dateOfMan[0],
                    taxBand[0].charAt(0), strToInt);

            System.out.println("P4");//Testing

            s.addVehicle(inputVehicle);

            System.out.println("P5");//Testing
        }

    }
    catch (FileNotFoundException fnfe)
    {
        System.out.println("File not found exception: " + fnfe.toString());
    }
    catch (IOException ioe)
    {
        System.out.println("I/O exception: " + ioe.toString());
    }

    System.out.println("addNewVehicleFromFile Complete");

}

Not sure if you need it but here is my file data.

Vehicle Details

Fordtest =manufacturer
Focus =model
ioCheck =VIN
09/01/1989 =DateOfMan
d =TaxBand
19900 =Cost

Vehicle Details

Fordtest2 =manufacturer
Focus2 =model
ioCheckVIN2 =VIN
09/01/1989 =DateOfMan
a =TaxBand
1922 =Cost

Finally, to make it clear where the program runs to I have added in some console output as testing. The while loops iterates twice outputting p1-p5 both times before the error occurs and it never reaches the final console output saying the method is complete.

Était-ce utile?

La solution

Check that the result of calling readLine() is not null (empty). If you check, and cause it not to do anything if its empty, this will solve your issue!

Autres conseils

Try checking if readLine() is null. while ((stringLine = br.readLine()) != null) { } This will make sure that there is something in that line to read, else it has reached end of file.

In your code you have 3 calls to readLine() each iteration. This could cause issues if for some reason the formatting in your text file is changed(missing a blank line for example). You may be better off making each vehicle on one line, seperated by commas. For example:

Vehicle Details

Fordtest =manufacturer
Focus =model
ioCheck =VIN
09/01/1989 =DateOfMan
d =TaxBand
19900 =Cost

Vehicle Details

Fordtest2 =manufacturer
Focus2 =model
ioCheckVIN2 =VIN
09/01/1989 =DateOfMan
a =TaxBand
1922 =Cost

Then becomes:

Fordtest, Focus, ioCheck, 09/01/1989, d, 19900
Fordtest2, Focus2, ioCheckVIN2, 09/01/1989, a, 1922

This would simplify the code somewhat, as well as reducing chance for error.

Also make sure to close the FileInputStream when you are finished with it. This insures that any resources associated with it are released properly.

try {
    FileInputStream fStream = new FileInputStream("AddVehicleFromFile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fStream));
    ...
} finally {
    try {
        if (fStream != null) {
            fStream.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top