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

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.

主站蜘蛛池模板: 穆棱市| 渭南市| 郓城县| 五台县| 日土县| 临澧县| 登封市| 财经| 定兴县| 门头沟区| 女性| 新建县| 白山市| 手机| 肥东县| 岳池县| 邯郸县| 新乡县| 商河县| 呼图壁县| 康平县| 元朗区| 大埔县| 密云县| 马龙县| 荆州市| 隆昌县| 怀安县| 鹿邑县| 巴林右旗| 吉木乃县| 芦山县| 桐城市| 龙岩市| 泰和县| 金门县| 盐山县| 江山市| 宜阳县| 奉节县| 汨罗市|