4 Replies - 782 Views - Last Post: 25 September 2014 - 03:43 PM Rate Topic: -----

#1 O0104  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 14

Why is this method returning null?

Posted 25 September 2014 - 01:05 PM

Hello, I am working on a project which manages an airport's airplanes and flights based on user input. The method printFlights() - lines 133-134 - is returning null and I can't figure out why. The method is supposed to print information about each flight. The logic is identical to the printPlanes() method which is working successfully. Any help would be appreciated. The link to entire code on Pastebin is posted below. Thank you.

http://pastebin.com/B3y8aNE7

Is This A Good Question/Topic? 0
  • +

Replies To: Why is this method returning null?

#2 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,291
  • Joined: 19-March 11

Re: Why is this method returning null?

Posted 25 September 2014 - 01:07 PM

Please post the relevant section of your code here. Thanks!
Was This Post Helpful? 1
  • +
  • -

#3 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Why is this method returning null?

Posted 25 September 2014 - 02:00 PM

It looks like you're having a scope issue, look how you're adding the planes to your arrayList.
Was This Post Helpful? 1
  • +
  • -

#4 O0104  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 14

Re: Why is this method returning null?

Posted 25 September 2014 - 02:21 PM

That makes sense. Any ideas for where to add it though? When I included it as below - taking out the for/each loop - I also got a nullpointerexception error. Plus, the solution below doesn't solve the problem of how to ensure that the plane whose flight information is added to the flights ArrayList is the same plane that the user had previously added to the database.
System.out.print("Give plane ID: ");
            String idNumberAgain = reader.nextLine();
            System.out.print("Give departure airport code: ");
            String departureCode = reader.nextLine();
            System.out.print("Give destination airport code: ");
            String arrivalCode = reader.nextLine();
            Flight flight = new Flight(planez, departureCode, arrivalCode);
            addFlight(flight, flights);
            break;
                }


Okay, thank you!
Was This Post Helpful? 0
  • +
  • -

#5 O0104  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-August 14

Re: Why is this method returning null?

Posted 25 September 2014 - 03:43 PM

Here is the entire code in acceptable format - sorry for the Pastebin link earlier:

import java.util.ArrayList;
public class Plane {
    private String idNumber;
    private int capacity;
    private ArrayList<Plane>planes;
   
    public Plane(String idNumber, int capacity) {
        this.idNumber = idNumber;
        this.capacity = capacity;
        this.planes = new ArrayList<Plane>();
    }
   
    public Plane(String idNumber) {
        this.idNumber = idNumber;
    }
   
    public Plane() {
       
    }
   
    public void setidNumber(Plane plane, String idNumber) {
        plane.idNumber = idNumber;
                }
   
    public void setCapacity(Plane plane, int capacity) {
        plane.capacity = capacity;
    }
    public String getidNumber() {
        return this.idNumber;
    }
   
    public int getCapacity() {
        return this.capacity;
    }
   
   
    public String toString(Plane plane) {
        return plane.getidNumber() + " (" + plane.getCapacity() + ")";
    }
   
}
 
public class Flight {
    private Plane plane = new Plane();
    private String departureCode;
    private String arrivalCode;
    private ArrayList<Flight>flights;
   
    public Flight(Plane plane, String departureCode, String arrivalCode) {
        plane = this.plane;
        departureCode = this.departureCode;
        arrivalCode = this.arrivalCode;
        this.flights = new ArrayList<Flight>();
    }
   
    public String getPlane() {
        return this.plane.getidNumber() + " (" + this.plane.getCapacity() + ")";
    }
   
    public String getDepartureCode() {
        return this.departureCode;
    }
   
    public String getArrivalCode() {
        return this.arrivalCode;
    }
   
    public String toString(Flight flight) {
        return flight.getPlane() + " (" + flight.getDepartureCode() + ") (" + flight.getArrivalCode() + ")";
    }
   
}
 
 
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
    ArrayList<Plane> planes = new ArrayList<Plane>();
    ArrayList<Flight> flights = new ArrayList<Flight>();
    System.out.println("Airport panel");
    System.out.println("--------------------");
    System.out.println();
    Scanner reader = new Scanner(System.in);
    while (true) {
        System.out.println("Choose operation: ");
        System.out.println("[1] Add airplane");
        System.out.println("[2] Add flight");
        System.out.println("[x] Exit");
        String command = reader.nextLine();
        if (command.equals("1")) {
            System.out.print("Give plane ID: ");
            String idNumber = reader.nextLine();
            System.out.print("Give plane capacity: ");
            int capacity = Integer.parseInt(reader.nextLine());
            Plane plane = new Plane(idNumber, capacity);
            addPlane(plane, planes);
        }
        else if (command.equals("2")) {
            System.out.print("Give plane ID: ");
            String idNumberAgain = reader.nextLine();
            System.out.print("Give departure airport code: ");
            String departureCode = reader.nextLine();
            System.out.print("Give destination airport code: ");
            String arrivalCode = reader.nextLine();
            for (Plane planez: planes) {
                if (planez.getidNumber().equals(idNumberAgain)) {
                    Flight flight = new Flight(planez, departureCode, arrivalCode);
                    addFlight(flight, flights);
                    break;
                }
               
            }  
        }
        else if (command.equals("x")) {
            break;
        }
    }  
            System.out.println();
            System.out.println("Flight service");
            System.out.println("------------");
            System.out.println();
            System.out.println("Choose operation: ");
            System.out.println("[1] Print planes");
            System.out.println("[2] Print flights");
            System.out.println("[3] Print plane info");
            System.out.println("[x] Quit");
            String command2 = reader.nextLine();
            while(!command2.equals("x")) {
        if (command2.equals("1")) {
            printPlanes(planes);
        }
         else if (command2.equals("2")) {
           printFlights(flights);
         }
         else if (command2.equals("3")) {
             System.out.print("Give plane ID: ");
             String planeRequest = reader.nextLine();
             Plane plane = new Plane(planeRequest);
             for (Plane planez: planes) {
             if (planez.getidNumber().equals(planeRequest)) {
                 System.out.println(planez.getidNumber() + " (" + planez.getCapacity() + " ppl)");
             }
             }
         }
            System.out.println("Choose operation: ");
            System.out.println("[1] Print planes");
            System.out.println("[2] Print flights");
            System.out.println("[3] Print plane info");
            System.out.println("[x] Quit");
            command2 = reader.nextLine();
            }
     if (command2.equals("x")) {
       //do nothing
      }
   
    }
    public static void printPlanes(ArrayList<Plane> planes) {
        for (Plane plane: planes) {
            System.out.println(plane.getidNumber() + " (" + plane.getCapacity() + " ppl)");
        }
    }
   
    public static void printFlights(ArrayList<Flight> flights) {
        for (Flight flightz: flights) {
            System.out.println(flightz.getPlane() + " (" + flightz.getArrivalCode() + ") (" + flightz.getDepartureCode() + ")");
        }
    }
   
   
    public static void addPlane(Plane plane, ArrayList<Plane> planes) {
        planes.add(plane);
    }
   
       public static void addFlight(Flight flight, ArrayList<Flight> flights) {
        flights.add(flight);
    }
   
    

This post has been edited by jon.kiparsky: 25 September 2014 - 03:45 PM
Reason for edit:: Removed gratuitous quote of original post

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1