官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 洛浦县| 保定市| 日照市| 新乐市| 芒康县| 陇南市| 陇西县| 梧州市| 九江县| 灵寿县| 华容县| 渭源县| 弥勒县| 洱源县| 峡江县| 鄂伦春自治旗| 铁岭县| 英超| 昭平县| 乌拉特中旗| 金堂县| 温州市| 思茅市| 邯郸县| 太和县| 治县。| 富川| 宁国市| 闽清县| 呼和浩特市| 容城县| 出国| 新泰市| 新田县| 桐梓县| 沙雅县| 苍溪县| 五指山市| 商南县| 盐亭县| 湖北省|