Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Basics!
You have completed Java Basics!
Preview
Errors are bound to happen. In this video we discuss what to do when they occur.
Resources
- Stack Overflow post on "cannot find symbol"
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I got some tough news for you.
0:03
You're not perfect.
0:05
I'm not either.
0:06
No one is. You're going to make mistakes.
0:07
In fact, I'm still making mistakes
all the time.
0:10
Your code will have errors.
0:14
Whenever you watch someone coding
and think, wow, they never mess up,
0:15
it's almost always because they spent
a lot of time making mistakes.
0:19
They've learned how to avoid common traps
that all developers fall into.
0:24
The sooner you start
seeing mistakes, and errors as a natural
0:28
and positive part of experimenting,
the faster you'll master the language.
0:31
Remember, if you're not failing,
you're not learning.
0:36
I to think of F.A.I.L. as an acronym
for First Attempt In Learning.
0:39
And it also works again for
"Fourth" and "Fifth", so you're good.
0:43
There are different types of errors.
0:47
The most common are syntax errors.
0:49
These can be frustrating
when you're just starting out.
0:52
The good news is that Java's
compiler is great
0:54
at pointing them out,
and usually the fix is clear.
0:57
Now let's go break some stuff.
1:01
So hopefully you
1:04
haven't had any errors yet,
but let's create one.
1:05
I'm going to misspell the word string here
and go with sting like the famous singer
1:08
or that awesome glowing short sword.
Let's see what happens.
1:13
Now press the up arrow and bring back
the last command and run it. I'll leave it
1:17
here for a moment unless you closed out
your workspace or it timed out.
1:21
Okay.
1:25
You'll see an error
1:29
saying, cannot find symbol.
1:30
It will also show the line number
where the problem is, for example,
1:33
line 13, and an arrow
pointing exactly to the mistake.
1:37
This is very helpful.
1:41
But if you didn't understand the message,
or didn't see the arrow pointing
1:43
right to the singer of The Police, one
great place to turn is the internet.
1:46
Java has been around for a long time,
1:50
so Google has answers for almost every
Java problem you'll encounter.
1:53
It's seen them all.
1:57
I'll copy the error message, cannot find
symbol, and paste it into Google.
1:59
Let's look at the
2:07
first result here on Stack Overflow.
2:08
It looks this person
ran into a similar issue
2:11
as us.
2:13
The first reply here
explains that, when Java compiles
2:17
your program, it creates a list
of all the names it knows about,
2:21
variables, methods, classes, and so on.
2:24
If it can't find one of these names,
called identifiers, it can't finish compiling.
2:28
That's exactly what our error means.
Java doesn't recognize the word sting.
2:33
Under the part on what could cause
this error they explain that it generally
2:38
could be caused by misspelling a class
or a method name,
2:42
getting the case wrong,
2:45
and using a variable without declaring it.
2:48
In our case, it's indeed a misspelling.
2:51
Let's fix it by adding
the missing r back to make it string.
2:54
Let's
3:03
have a
look at another example of the same error.
3:03
If you write string,
but with a lowercase s,
3:06
Java won't recognize it.
3:09
You'll get the same
3:15
cannot find symbol error,
but this time it's pointing to string.
3:15
In this scenario, I would do the same type
of search as before.
3:20
And as we saw in that Stack
Overflow post, Java
3:23
identifiers are case sensitive,
so we should always try to remember that.
3:26
Here's one more common error
when getting started with Java.
3:31
Let's say I just wrote this line out and
forgot to put our semicolon at the end.
3:34
Let's see what happens.
3:38
Let's save and run this.
3:40
Awesome this one is super clear
3:49
It says on this line
3:52
it expected a semicolon and it pointing
right to the end of the line.
3:53
So when you encounter this error
it should be pretty straightforward.
3:57
Let's add that back.
4:00
And that was very similar
to if we happen to have forgotten
4:02
to close off our parentheses
in a method call this.
4:05
Save and run this again.
4:09
And you'll see
it was expecting either a closing paren
4:14
or a comma to continue adding arguments
like we did in the last video.
4:16
Before we go, let's add that back
and save to make sure
4:20
we're error-free again.
4:23
Awesome.
4:28
So this is a great example of what to do
when you get an error.
4:30
First, read the error message carefully.
4:33
If the error message isn't helpful
4:35
or clear, try to search for it
online or check the official sources.
4:37
Remember, syntax errors are the most
common and usually easy to fix.
4:41
As we go deeper in this Java journey,
I'll show you some other types of errors,
4:46
logic errors and runtime errors,
whether I'm planning to or not.
4:50
Because I definitely make mistakes,
and I want you to learn from mine,
4:54
so I'll leave them in these
videos when I make them.
4:58
Finally, I want to remind you
about the Treehouse community.
5:01
Your fellow students are here to help.
5:04
And also,
sharing your errors and questions
5:06
not only helps you, but can also help
others learn from your experience.
5:09
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up