0

This is how I am giving a passenger a seat

shuttle.seats[i].name = PASSENGERS[i].name;        // i is the index you know
shuttle.seats[i].house = PASSENGERS[i].house;

but it gives me the error

Uncaught TypeError: Cannot set property 'name' of null

How can I fix it?

1 Answer 1

1

The problem is caused when shuttle.seats[i] is equal to null as you're trying to set the name property of it to PASSENGERS[i]. In other words, there is no name property (or pretty much any other property) for an object whose value is null.

You probably should do something like

shuttle.seats[i] = passenger;

where shuttle.seats[i] is that seat that you wanna give to a passenger and passenger is the passenger you wanna give them this seat.

3
  • If i opt the approach you suggested, I would only be keeping track of names of the passengers while assigning the seats, How can I keep the record of houses too so that I may drop the passengers off later. Sep 26, 2014 at 11:38
  • 1
    @segmentationfaulter as I said, "passenger is the passenger you wanna give them the seat" NOT just the name of the passenger, but rather an object that encapsulates all the information you need for this particular passenger. It doesn't have to be literally set to passenger. passenger here is just a place holder. You should change that to the appropriate object whatever the way you're accessing it with.
    – kzidane
    Sep 26, 2014 at 17:28
  • Thank you, got it and my code is working now! Sep 27, 2014 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .