I have an array: this.entries = []. I run some code and it runs through this if statement several times:
if(this.entries[2] != null)
Everything works fine until after all the code runs I reset the array:
I’ve tried doing this.entries = [] and this.entries.splice(0, this.entries.length);
I re-run the code and when it gets to the if statement I get this error:
Cannot read property '2' of null
As far as I can tell there is nothing different. Tips and help would be very appreciated.
There are several objects here. There is
thiswhich of course should never benull. Then there isthis.entriesWhich is an array. Think of it as a container that can contain things. Then, there isthis.entries[i]Which are the things in the container.when
this.entries[2]fails, it means that there is no object in slot 2 of the container.However, the error
Cannot read property '2' of nullmeans that there IS NO CONTAINER. That is, the array itself has been set to null.