Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Programming for Beginners

You're reading from   Java Programming for Beginners Learn the fundamentals of programming with Java

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781788296298
Length 318 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
SkillSprints Inc., Mark Lassoff SkillSprints Inc., Mark Lassoff
Author Profile Icon SkillSprints Inc., Mark Lassoff
SkillSprints Inc., Mark Lassoff
Arrow right icon
View More author details
Toc

How to interpret errors detected by NetBeans?

As we write more and more complicated Java programs, we're inevitably going to make some mistakes. Some of these mistakes will be significant logic errors or misunderstandings on our part that we might have to further educate ourselves before we can solve them. But, especially while we're starting our programming, we're going to make a lot of small silly errors that are really easy to fix as long as we know where to look.

Fortunately, Java compilers are designed to point errors out to us when they come across them. To see this in action, let's simply make our HelloWorld program incorrect by removing the semicolon from the end of the println statement:

Now NetBeans red-jitters the line to let us know that it's pretty sure something's wrong, but we can ask our compiler to take a shot at it anyway. If we attempt to build this project, we don't get the COMPILATION SUCCESSFUL message we otherwise would; instead, we get an error message:

This error is ';' expected, which is a pretty handy and self-explanatory error message. Of equal importance is the number after the colon in this message, which is 4. This lets us know on what line the compiler came across this error. In NetBeans, if we click on an error message, the IDE will highlight that line of code:

If we add in our semicolon, then our program builds successfully as shown in the following screenshot:

That's all there is to it.

Of course, not all error messages are quite that self-explanatory. For the sake of argument, let's create a slightly more complicated error. What would have happened if we had forgotten to insert one of our parentheses in this program? This is illustrated in the following code:

When we press Build Project, we get not one but two errors, even though we really only made one mistake:

Our first error is not a statement, then it lets us know the line that it doesn't understand. If we look at the first error for a little bit, we'll probably notice that we're missing a pair of parentheses, so we will be able to fix this error; however, what about the second error? We got ';' expected again even though in this case we really do have a semicolon.

Well, once one error has occurred in the program, the compiler's ability to understand the lines of the code gets shattered very quickly. When we're debugging our code, the general rule of thumb is to address only the top error on our list; that's the first error that the compiler came across in our code. We might be able to glean some helpful information from errors further down, but more often than not, they're simply going to be errors generated by the first syntax mistake we made. Nothing too mind-blowing here, but I wanted to point this out to you because being able to track compiler errors can save us a lot of headaches while we're learning to program.

The code completion feature

While we're talking about NetBeans, let's quickly go over one other IDE feature. Let's say I wanted to write a new line of code and I'm going to use something from the System library:

Once I've typed System., NetBeans can suggest valid responses for me. Only one of these, of course, is going to be what I'm looking for. The NetBeans compiler has a lot of helpful features such as these. If you're the kind of person who thinks code completion is awesome, go ahead and leave these tools on. We can do this by going to Tools | Options | Code Completion and checking the features that we'd like:

If you'd rather NetBeans behave a little more like a text editor, go ahead and uncheck all the features.

There we go, lots of housecleaning in this section, but hopefully quick and not too painful.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image