Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the last section, we finished up our constructor function and tried to run our code, but we got
this nasty error message that said null underscore methods add is not a function.
And so just based on the presence of the word add right here, I'm going to assume that the error is
coming from this line of code just because I see the word add.
So let's talk about what's going wrong right here.
And it's going to tie back into a topic that we discussed much earlier inside this course.
So I want to remind you about this diagram that we looked at way before.
In this diagram, we broke down a single declaration and initialization of a variable.
We had said that to the left of the equals sign.
We went through a process called variable declaration.
And then by putting in the equals and some value over here to the right, we went through the process
of initialization, which was to actually assign a value to that variable we created.
We also took a look at a diagram that was this one right here, so we had said that when we create that
variable or when we declare it, what we're really doing is creating kind of a reference inside the
memory of our computer.
And that reference has the ability to kind of point over to some other value that also exists inside
of memory.
So how does this relate to the error message we just saw?
Well, let's look at this line of code right here.
On this line, we declared a new field tied to our deck class called Cartes, this line of code right
here is solely a variable or a field initialization.
So as we said just a moment ago, initialization is just the fact of saying, hey, there's a variable
out here called blah, and here is its type.
So what we really just did was to declare a new variable with a type of list that contains a bunch of
different cards.
And it has a name of Cartes.
But what we did not do was any type of variable initialization, we did not set any value to this variable.
So if we think about how this variable currently exists inside of memory, as our program stands right
now, it kind of looks a little something like this.
We've got that Kurts variable over here to the right left hand side, excuse me, but then it has not
been initialized yet.
We've not said, OK, and here's the value that is assigned to that variable.
Whenever we declare a variable and do not initialize it, which is what we're doing right now with cards,
that variable gets a default value of null inside of our application.
And so that's what you're seeing right here with this error message.
The error message, says James, which is short for JavaScript null underscore methods at so no right
here is really the keyword.
It says null because we declared this field right here or we declared this variable, but we did not
actually assign a value to it.
And so by default, if we do not assign a value, we get null for that value or for that variable instead.
So when we call it cardstock ad, we're essentially saying no dot ad and then we try to pass in the
card.
And of course, NULL does not have a function called AD, which is why we see this error message.
OK, so that might be way more detailed.
And you want to know, but I can almost guarantee you this is an error message that you're going to
see on your own, especially in your own projects as you get started with.
So I wanted to put in a little bit of discussion about why you're seeing this error message now to fix
this error.
We can do a very simple thing.
Remember what we did back here?
We had said that we do our variable declaration and then our initialization.
So that's exactly what we're going to do.
We're going to make sure that we initialize that variable.
So to initialize that variable.
Here's my class right here.
Here is the variable declaration to initialize it, we'll put in our equals and then we're going to
assign an empty list to this thing to assign an empty list.
We just put down square.
Brace's like so.
So now we've got the full process, we do our variable declaration, we then initialize it and then
down inside the constructor, when we call the add method that is supposed to be a part of any list,
object cards is no longer null.
It is now an initialized list and the ADD function exists on that list and we should be good to go.
So let's try running this code now and just verifying that we do not get an error message.
OK, so no message anymore.
We don't see any output, but that's totally fine because we're not currently doing any logged statements
or anything like that.
OK, so in this section, we spoke a little bit about how whenever we declare a variable, it gets a
default value of null and it's only when we actually initialize the variable do we get a usable value
assigned to it.
So now that we've covered that kind of corner case, let's continue in the next section.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.