0

So basically what I'm trying to do is making a parking ticket system. I've got two classes one that contains info for a Car and one for a Ticket. I want the system to show all the tickets a car registration has gotten.

public class Car
    {

        public string registrationNumber { get; set; }


        public string carBrand { get; set; }


        public string carColor { get; set; }

        public List<Ticket> ticketlist {get; set;}
        
        public void AddParkingTicket(Ticket newTicket)
        {
            ticketList.Add(newTicket);
        }
    }
}
        

 public class Ticket
    {
        public Ticket(int TicketID, DateTime date, string comment, int parkeringareaID, int parkingOfficerID)
        {
        }

      
        public int TicketID { get; set; } = 0;

        
        public DateTime date { get; set; }

       
        public string commenter { get; set; }


       
        public int parkingsAreaID { get; set; }

       
        public int parkingsOfficerID { get; set; }

    }
}
  

List<Car> list = new List<Car>();
    public TicketController()
    {
        

        Ticket ticket1 = new Ticket(1, DateTime.Now, "handicap parking", 1, 2);
        Car car1 = new Car { registrationNumber = "BT66358", carBrand = "BMW", carColor = "Gul" };
        
        car1.AddParkingTicket(ticket1)

        list.Add(car1);
    }

my current output is this

[
    {
        "registrationNumber": "BT66358",
        "carBrand": "BMW",
        "carColor: "Yellow",
        "ticketlist": null
    }
]

I'm not sure what I'm doing wrong, I've tried to change the class contructors aswell, but no success. I want the output to also show the info of the ticket for that given registration number

  • 2
    Is your `Car` code as posted accurate? I see you're doing a `ticketList.Add` but the property is named `ticketlist`. – Jacob Dec 11 '20 at 01:05
  • @Jacob Oh sorry a typo when I posted. it should be ticketList, but thats not the issue – Suzdar Ibrahim Dec 11 '20 at 01:11
  • I think your code is missing something. I would think the `car1.AddParkingTicket(ticket1)` line would have thrown an error since `car1.ticketList` is null. Did you leave out your constructor where you're initializing `ticketList`? – Jacob Dec 11 '20 at 01:14
  • @Jacob I just tested with postman and the code doesn't execute, getting this error ''System.NullReferenceException: 'Object reference not set to an instance of an object.' WebApplication.Models.Car.ticketList.get returned null'' – Suzdar Ibrahim Dec 11 '20 at 01:16
  • OK, that makes more sense than the output you originally posted. You simply need to initialize the list property in your constructor. – Jacob Dec 11 '20 at 01:17
  • @Jacob Thanks, I'm getting closer now. the output is [ { "registrationNumber": "Bt66358", "carBrand": "BMW", "carColor": "Gul", "ticketList": [ { "ticketID": 0, "date": "0001-01-01T00:00:00", "comment": null, "parkingAreaID": 0, "parkingOfficerID": 0 } ] } ] Dont know why its empty though, when I've created a ticket – Suzdar Ibrahim Dec 11 '20 at 01:22
  • 2
    The constructor for `Ticket` doesn't assign any of the constructor arguments to the properties of the `Ticket` object. – ProgrammingLlama Dec 11 '20 at 01:23
  • @John Yes its there but the values are wrong as you can see. I've added a comment but its showing ''null'' – Suzdar Ibrahim Dec 11 '20 at 01:23
  • 2
    What he's saying is that your constructor isn't doing anything for `Ticket`. If you want the properties to be set from your constructor parameters, you have to write that assignment code. Since your constructor doesn't do anything, everything is going to have default values. – Jacob Dec 11 '20 at 01:25
  • @Jacob Oh yes, I figured it out. now the output is showing the ticket info, but its only showing the last ticket. So if that registrationnumber has 3 tickets it only shows the last one. Any tips? – Suzdar Ibrahim Dec 11 '20 at 01:30
  • I recommend posting a new question about that with a [mcve] since that seems to be a outside the scope of this question. If it's showing the same ticket 3 times, perhaps you're reusing the ticket object rather than creating a new one, or maybe you put the code to initialize the list in `AddParkingTicket` rather than in `Car` (see Nonik's answer). – ProgrammingLlama Dec 11 '20 at 01:33
  • Oh its starting to get late and my brain isnt where its supposed to be haha. Thanks to everyone for the help – Suzdar Ibrahim Dec 11 '20 at 01:37
  • you need to assign values passing in ticket in your ticket controller, example: public Ticket(int TicketID, DateTime date, string comment, int parkeringareaID, int parkingOfficerID) { this.TicketID=TicketID; } – Nonik Dec 11 '20 at 01:45

1 Answers1

3

change ticketList to ticketlist

 public void AddParkingTicket(Ticket newTicket)
        {
            ticketlist.Add(newTicket);
        }

you also need to add constructor to Car to initialize collection, otherwise you get error

public Car()
        {
            this.ticketlist = new List<Ticket>();
        }
Nonik
  • 645
  • 4
  • 11