0

I have an issue with a program I'm designing around searching for Multi-City flights. For whatever reason, my code is giving an error and I can't figure out for the life of me where this bug is. If you could please lend a hand, you will be my favorite human for the next week, possibly month. Thank you so much!

Code for MultiFactory.cs:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;

namespace Reservation_Library
{
    public class MultiFactory
    {
        StreamWriter sw;
        XmlSerializer serial;
        List<MultiCityFlight> finalMatches;
        List<FlightRequest> flightList;
        List<Flight> entireList;
        List<Flight> multiCityList;
        const string FLIGHT_REQUEST = @"..\..\flightRequest.xml";
        const string MULTI_FILE = @"..\..\multiFlights.xml";
        const string FLIGHT_FILE = @"..\..\flights.xml";
        const string MATCH_FILE = @"..\..\match.xml";

        DateTime date;
        Random random = new Random();

        public DateTime randomDate()  //Generates a random date for the flight within the provided timeframe
        {
            int month = random.Next(6, 8);
            int day = random.Next(1, 31);
            int Day = random.Next(1, day);

            if (month == 7 && day > 1)
            {
                month = random.Next(6, 7);
                day = random.Next(1, 31);
            }

            date = new DateTime(2017, month, Day);

            return date;
        }



       /*public void SerializeMultiFlight(List<MultiCityFlight> multiFlights) //Simple serializer for writing full list to xml
        {
            serial = new XmlSerializer(finalMatches.GetType());
            sw = new StreamWriter(MULTI_FILE);
            serial.Serialize(sw, multiFlights);
            sw.Close();
        }*/

        public List<MultiCityFlight> findConnections(int choice) //Method for comparing the entire list of flights against the selected flight for matches. All matching flights added to a matchingFlights list
        {
            FlightFactory flight = new FlightFactory();
            entireList = new List<Flight>();
            List<Flight> firstConnections = new List<Flight>(); //Flights on same date as request, must have matching departure point
            List<Flight> secondConnections = new List<Flight>();
            List<FlightRequest> flightList = generateFlightRequests();
            finalMatches = new List<MultiCityFlight>();

            entireList = flight.generateFlightList();
            foreach(Flight f in entireList)
            {
                Console.Write(f.ToString());
            }

            FlightRequest selected = flightList[choice];

            for (int i = 0; i < entireList.Count; i++)
            {

                if (entireList[i].DateAndTime == selected.date &&
                    entireList[i].OrigAirport == selected.origAirport)
                {
                    firstConnections.Add(entireList[i]);
                }
                Console.WriteLine(firstConnections[0]);


                if (entireList[i].DateAndTime == selected.date && entireList[i].DestAirport == selected.destAirport)
                {
                    secondConnections.Add(entireList[i]);
                }


                /*using (var e1 = firstConnections.GetEnumerator())
                using (var e2 = secondConnections.GetEnumerator())
                {
                    while (e1.MoveNext() && e2.MoveNext())
                    {


                        Flight f1 = e1.Current;
                        Console.WriteLine(f1.ToString());
                        Flight f2 = e2.Current;
                        Console.WriteLine(f2.ToString());

                        TimeSpan t = f2.DateAndTime - f1.DateAndTime;
                        int diff = (int)t.Hours;

                        if (diff > 1 && f1.DestAirport == f2.OrigAirport)
                        {
                            finalMatches.Add(new MultiCityFlight(f1, f2));
                        }
                    }
                }*/
            }

            return finalMatches;
        }

        public List<FlightRequest> DeserializeFlightList() //Simple method for deserializing from xml to a list
        {
            flightList = new List<FlightRequest>();
            StreamReader sr = new StreamReader(FLIGHT_REQUEST);
            serial = new XmlSerializer(flightList.GetType());
            flightList = (List<FlightRequest>)serial.Deserialize(sr);
            sr.Close();
            return flightList;

        }

        public List<FlightRequest> generateFlightRequests() //Generating 20 random flights to search for matching flights
        {
            flightList = new List<FlightRequest>();

            FlightRequest f = new FlightRequest("MIA", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "SEA", randomDate());
            flightList.Add(f);

            return flightList;
        }
    }
}

This is the line where the error occurs:

Console.WriteLine(firstConnections[0]);

If you need code from another class or method, please ask. Thanks so much!

  • Using your debugger, you might see that for your test data, the ``firstConnections`` list might be empty. it is possible, since you only add to that list if some criteria are met. Set a break point in the body of the if statement above and if it is never hit, you know the list is empty and why the error is thrown. – BitTickler Apr 15 '18 at 23:43
  • You get this exception because the list is smaler then you expect. Usually you only get his exception because you messed up the limitations on a loop, but as the other posters pointed out you are trying to access Element 0 when there is no guarantee there is even that much in the Collection. A obvious workaround is a check if the count/size is big enough. In order to keep the code short, you may opt to use the Conditional Operator. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator – Christopher Apr 15 '18 at 23:53
  • That looks to be the issue. You're amazing. I'll give it a shot and report back in a bit <3 – Max Cooper Apr 15 '18 at 23:53
  • 1
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Camilo Terevinto Apr 16 '18 at 00:17

1 Answers1

0

Obviously you get this error, because the List is empty.

Just debug some code ahead:

Console.WriteLine(entireList[i].DateAndTime + " == "+selected.date+" | "+entireList[i].OrigAirport+" == "+selected.origAirport);

There you should see, that this is not true. So there is nothing added and so you are out of range :D