-3

i am new to JAVA. I think all my code is corect. I dont actually know what is wrong with my code but it shows error.

Error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "F28DA_CW2.FlightsReader.getAirports()" because "this.flightReader" is null
    at F28DA_CW2.FlyingPlanner.leastCost(FlyingPlanner.java:206)
    at F28DA_CW2.FlyingPlannerMainPartBC.main(FlyingPlannerMainPartBC.java:29)

Below is the code i am working on:

Main file

    import java.io.FileNotFoundException;

    public class FlyingPlannerMainPartBC {

    public static void main(String[] args) throws FlyingPlannerException, FileNotFoundException {

        FlyingPlanner fi;
        fi = new FlyingPlanner();
        try {
            fi.populate(new FlightsReader());

            // Implement here your user interface using the methods of Part B. You could
            // optionally expand it to use the methods of Part C.

        } catch (FileNotFoundException | FlyingPlannerException e) {
            e.printStackTrace();
        }

        FlightsReader flightReader = new FlightsReader();
        flightReader.getAirports();
        flightReader.getFlights();
        FlyingPlanner pr = new FlyingPlanner();
        System.out.println(pr.leastCost("EDI", "DXB")); // line: 29
        
    }

}

a snippet of the error source.

    @Override
    public Journey leastCost(String from, String to) throws FlyingPlannerException {
        // TODO Auto-generated method stub
        HashSet<String[]> airports = flightReader.getAirports(); //line206
        HashSet<String[]> flights = flightReader.getFlights();

        DefaultDirectedWeightedGraph<String, DefaultWeightedEdge> leastCost = new DefaultDirectedWeightedGraph<>(
                DefaultWeightedEdge.class);

        for (String[] flight : flights) {
            leastCost.addVertex(flight[1]);
            leastCost.addVertex(flight[3]);
            leastCost.addEdge(flight[1], flight[3]);
            leastCost.setEdgeWeight(flight[1], flight[3], Integer.parseInt(flight[5]));
        }

        DijkstraShortestPath<String, DefaultWeightedEdge> dijkstraAlg = new DijkstraShortestPath<>(leastCost);
        GraphPath<String, DefaultWeightedEdge> shortestPath = dijkstraAlg.getPath(from, to);

        int totalCost = 0;
        int totalHop = 0;
        int totalTime = 0;

        for (DefaultWeightedEdge defaultWeightedEdge : shortestPath.getEdgeList()) {
            totalCost = totalCost + (int) leastCost.getEdgeWeight(defaultWeightedEdge);
            totalHop = totalHop + 1;
            String v1 = leastCost.getEdgeSource(defaultWeightedEdge);
            String v2 = leastCost.getEdgeTarget(defaultWeightedEdge);
            System.out.println(totalHop + ". " + v1 + "--->" + v2);
        }

        Journey journey = new Journey();
        journey.setTotalCost(totalCost);
        journey.setTotalHop(totalHop);

        System.out.println(totalCost);
        System.out.println(totalHop);

        return journey;
    }

Last the Scanner file that reads the file (where i think is completely fine)

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.HashSet;
    import java.util.Scanner;

    public class FlightsReader {

    private static final File flightsDatasetFolder = new File("FlightsDataset");
    private static final File flightsDatasetFlights = new File(flightsDatasetFolder, "flights.csv");
    private static final File flightsDatasetAirports = new File(flightsDatasetFolder, "airports.csv");

    private HashSet<String[]> airports;
    private HashSet<String[]> flights;

    public FlightsReader() throws FileNotFoundException, FlyingPlannerException {
        this.airports = new HashSet<String[]>();
        this.flights = new HashSet<String[]>();

        // Adding flights, and building list of needed airport
        HashSet<String> airportsNeeded = new HashSet<String>();
        Scanner flightsScanner = new Scanner(flightsDatasetFlights);
        while (flightsScanner.hasNextLine()) {
            String line = flightsScanner.nextLine();
            String[] fields = line.split(",");
            String from = fields[1];
            String to = fields[3];
            airportsNeeded.add(from);
            airportsNeeded.add(to);
            this.flights.add(fields);
        }
        flightsScanner.close();

        // Adding airports
        Scanner airportsScanner = new Scanner(flightsDatasetAirports);
        while (airportsScanner.hasNextLine()) {
            String line = airportsScanner.nextLine();
            String[] fields = line.split(",");
            if (airportsNeeded.contains(fields[0])) {
                this.airports.add(fields);
            }
        }
        airportsScanner.close();
    }

    /**
     * Returns a hash set of airport details (0: airport code, 1: city, 2: airport
     * name)
     */
    public HashSet<String[]> getAirports() {
        return this.airports;
    }

    /**
     * Returns a hash set of flight details (0: flight code, 1: airport code of
     * departure, 2: departure time GMT, 3: airport code of arrival, 4: arrival time
     * GMT, 5: flight cost)
     */
    public HashSet<String[]> getFlights() {
        return this.flights;
    }

}

I think the flight reader codes works well. I not really sure what the problem is.

Thank you to any kind JAVA master out there that could help me.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • 4
    The exception text tells you exactly what caused the NullPointerException -- `...because "this.flightReader" is null`. – Andy Thomas Apr 04 '23 at 23:07

1 Answers1

-1

I think you forgot to initialize flightReader inside the object FlyingPlanner. I guess you must call something like pr.setflightReader(flightReader) before you call pr.leastCost("EDI", "DXB") or there must be a constructor that take a flightReader as input.

scatolone
  • 733
  • 1
  • 5
  • 21
  • Yea ... in the `main` method the OP (also) has declared and initialized a local variable called `flightReader`. But that is *different* to the instance variable that the code is using in the `FlightReader` constructor. – Stephen C Apr 04 '23 at 23:18