Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,630 --> 00:00:04,920
In the last section, we finished up our constructor function and tried to run our code, but we got
2
00:00:04,920 --> 00:00:10,140
this nasty error message that said null underscore methods add is not a function.
3
00:00:10,680 --> 00:00:15,270
And so just based on the presence of the word add right here, I'm going to assume that the error is
4
00:00:15,270 --> 00:00:18,570
coming from this line of code just because I see the word add.
5
00:00:19,280 --> 00:00:21,360
So let's talk about what's going wrong right here.
6
00:00:21,360 --> 00:00:25,280
And it's going to tie back into a topic that we discussed much earlier inside this course.
7
00:00:26,010 --> 00:00:30,090
So I want to remind you about this diagram that we looked at way before.
8
00:00:30,780 --> 00:00:35,640
In this diagram, we broke down a single declaration and initialization of a variable.
9
00:00:36,300 --> 00:00:38,600
We had said that to the left of the equals sign.
10
00:00:38,640 --> 00:00:41,100
We went through a process called variable declaration.
11
00:00:41,640 --> 00:00:46,500
And then by putting in the equals and some value over here to the right, we went through the process
12
00:00:46,500 --> 00:00:51,330
of initialization, which was to actually assign a value to that variable we created.
13
00:00:52,160 --> 00:00:57,950
We also took a look at a diagram that was this one right here, so we had said that when we create that
14
00:00:57,950 --> 00:01:03,320
variable or when we declare it, what we're really doing is creating kind of a reference inside the
15
00:01:03,320 --> 00:01:04,540
memory of our computer.
16
00:01:05,180 --> 00:01:10,910
And that reference has the ability to kind of point over to some other value that also exists inside
17
00:01:10,910 --> 00:01:11,330
of memory.
18
00:01:12,020 --> 00:01:14,660
So how does this relate to the error message we just saw?
19
00:01:14,990 --> 00:01:17,060
Well, let's look at this line of code right here.
20
00:01:18,320 --> 00:01:24,950
On this line, we declared a new field tied to our deck class called Cartes, this line of code right
21
00:01:24,950 --> 00:01:29,470
here is solely a variable or a field initialization.
22
00:01:30,140 --> 00:01:35,090
So as we said just a moment ago, initialization is just the fact of saying, hey, there's a variable
23
00:01:35,090 --> 00:01:37,550
out here called blah, and here is its type.
24
00:01:38,180 --> 00:01:44,330
So what we really just did was to declare a new variable with a type of list that contains a bunch of
25
00:01:44,330 --> 00:01:45,400
different cards.
26
00:01:45,410 --> 00:01:46,820
And it has a name of Cartes.
27
00:01:47,710 --> 00:01:54,130
But what we did not do was any type of variable initialization, we did not set any value to this variable.
28
00:01:54,650 --> 00:01:59,200
So if we think about how this variable currently exists inside of memory, as our program stands right
29
00:01:59,200 --> 00:02:01,950
now, it kind of looks a little something like this.
30
00:02:02,560 --> 00:02:08,139
We've got that Kurts variable over here to the right left hand side, excuse me, but then it has not
31
00:02:08,139 --> 00:02:10,030
been initialized yet.
32
00:02:10,030 --> 00:02:13,930
We've not said, OK, and here's the value that is assigned to that variable.
33
00:02:14,790 --> 00:02:20,250
Whenever we declare a variable and do not initialize it, which is what we're doing right now with cards,
34
00:02:20,670 --> 00:02:26,030
that variable gets a default value of null inside of our application.
35
00:02:26,640 --> 00:02:29,470
And so that's what you're seeing right here with this error message.
36
00:02:29,940 --> 00:02:37,530
The error message, says James, which is short for JavaScript null underscore methods at so no right
37
00:02:37,530 --> 00:02:38,790
here is really the keyword.
38
00:02:39,300 --> 00:02:44,580
It says null because we declared this field right here or we declared this variable, but we did not
39
00:02:44,580 --> 00:02:46,620
actually assign a value to it.
40
00:02:46,920 --> 00:02:53,490
And so by default, if we do not assign a value, we get null for that value or for that variable instead.
41
00:02:54,300 --> 00:03:01,260
So when we call it cardstock ad, we're essentially saying no dot ad and then we try to pass in the
42
00:03:01,260 --> 00:03:01,720
card.
43
00:03:01,740 --> 00:03:06,570
And of course, NULL does not have a function called AD, which is why we see this error message.
44
00:03:07,320 --> 00:03:09,330
OK, so that might be way more detailed.
45
00:03:09,330 --> 00:03:12,900
And you want to know, but I can almost guarantee you this is an error message that you're going to
46
00:03:12,900 --> 00:03:16,020
see on your own, especially in your own projects as you get started with.
47
00:03:16,710 --> 00:03:21,480
So I wanted to put in a little bit of discussion about why you're seeing this error message now to fix
48
00:03:21,480 --> 00:03:22,050
this error.
49
00:03:22,260 --> 00:03:24,030
We can do a very simple thing.
50
00:03:24,270 --> 00:03:25,680
Remember what we did back here?
51
00:03:26,040 --> 00:03:30,200
We had said that we do our variable declaration and then our initialization.
52
00:03:30,540 --> 00:03:32,250
So that's exactly what we're going to do.
53
00:03:32,250 --> 00:03:34,890
We're going to make sure that we initialize that variable.
54
00:03:35,550 --> 00:03:37,080
So to initialize that variable.
55
00:03:38,090 --> 00:03:39,650
Here's my class right here.
56
00:03:40,050 --> 00:03:45,650
Here is the variable declaration to initialize it, we'll put in our equals and then we're going to
57
00:03:45,650 --> 00:03:49,970
assign an empty list to this thing to assign an empty list.
58
00:03:49,970 --> 00:03:50,960
We just put down square.
59
00:03:50,960 --> 00:03:52,070
Brace's like so.
60
00:03:53,510 --> 00:04:00,020
So now we've got the full process, we do our variable declaration, we then initialize it and then
61
00:04:00,020 --> 00:04:05,840
down inside the constructor, when we call the add method that is supposed to be a part of any list,
62
00:04:05,840 --> 00:04:08,710
object cards is no longer null.
63
00:04:08,720 --> 00:04:15,170
It is now an initialized list and the ADD function exists on that list and we should be good to go.
64
00:04:16,440 --> 00:04:20,410
So let's try running this code now and just verifying that we do not get an error message.
65
00:04:21,070 --> 00:04:23,240
OK, so no message anymore.
66
00:04:23,400 --> 00:04:28,230
We don't see any output, but that's totally fine because we're not currently doing any logged statements
67
00:04:28,230 --> 00:04:29,330
or anything like that.
68
00:04:30,290 --> 00:04:34,610
OK, so in this section, we spoke a little bit about how whenever we declare a variable, it gets a
69
00:04:34,610 --> 00:04:40,190
default value of null and it's only when we actually initialize the variable do we get a usable value
70
00:04:40,190 --> 00:04:40,970
assigned to it.
71
00:04:41,690 --> 00:04:45,830
So now that we've covered that kind of corner case, let's continue in the next section.
7209
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.