Book Image

Java Programming for Beginners

By : Mark Lassoff
Book Image

Java Programming for Beginners

By: Mark Lassoff

Overview of this book

Java is an object-oriented programming language, and is one of the most widely accepted languages because of its design and programming features, particularly in its promise that you can write a program once and run it anywhere. Java Programming for Beginners is an excellent introduction to the world of Java programming, taking you through the basics of Java syntax and the complexities of object-oriented programming. You'll gain a full understanding of Java SE programming and will be able to write Java programs with graphical user interfaces that run on PC, Mac, or Linux machines. This book is full of informative and entertaining content, challenging exercises, and dozens of code examples you can run and learn from. By reading this book, you’ll move from understanding the data types in Java, through loops and conditionals, and on to functions, classes, and file handling. The book finishes with a look at GUI development and training on how to work with XML. The book takes an efficient route through the Java landscape, covering all of the core topics that a Java developer needs. Whether you’re an absolute beginner to programming, or a seasoned programmer approaching an object-oriented language for the first time, Java Programming for Beginners delivers the focused training you need to become a Java developer.
Table of Contents (12 chapters)

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.