Java Debugging Tip: The Empty Statement

Introduction

A Java program might build properly but still contain a subtle logic error called an Empty Statement. The Empty Statement isn't serious enough to generate a syntax error. However, it's devious enough to cause frustration during the development process. In this tutorial we investigate the potential problems caused by Empty Statements and how to avoid them.

Example

Consider the following example in Figure 01. The simple for loop should print 100 lines as i is incremented from 0 to 99. Unfortunately, an extra semi-colon lurks just beyond the closing parenthesis. This is a classic example of a logic error.

Figure 01 - Note the semi-colon lurking just beyond the for loop. This is a prime example of a type of logic error referred to as an Empty Statement.
Figure 01 - Note the semi-colon lurking just beyond the for loop. This is a prime example of a type of logic error referred to as an Empty Statement.

The semi-colon terminates the loop. The output statement might appear to be part of the loop because it's indented, but it's not. Indenting provides only a visual cue for programmers.

We do get several visual cues that are easliy ignored, especially if the program is large and complicated. The suspicious line of code is underlined with a dreaded NetBeans squiggle and the margin is tagged with a warning icon. For better or worse, these cues are purely advisory; they do not prevent the program from building or executing.

Hovering the mouse over the line of code or the margin tag also brings up a related message.

In the following figure we see that the program builds with no errors or warnings. That's all most programmers need to know. If it built, it must be ready to run. :)

Figure 02 - The program builds properly, despite the Empty Statement
Figure 02 - The program builds properly, despite the Empty Statement

In the following figure we see that the program executes, although probably not as intended. The Empty Statement terminated the for loop and the print statement executed only once.

If you didn't know what the program was supposed to do, this error might slip past.

Knowing what the program is supposed to do, it's easy to identify the problem in such a small chunk of code. However, knowing how to spot the warning signs provided by NetBeans will save you a lot of time in the long run.

Figure 03 - The program executes, probably not as intended, despite the Empty Statement
Figure 03 - The program executes, probably not as intended, despite the Empty Statement

In Figure 04, below, the offending semi colon has been deleted and the program runs very differently.

Figure 04 - The semi colon has been removed and the program runs differently. The Empty Statement is gone.
Figure 04 - The semi colon has been removed and the program runs differently. The Empty Statement is gone.

Will braces solve the problem? Nope. The semi colon still terminates the loop, as illustrated in Figure 05. Interestingly enough, the braces do nothing at all. One wonders why Netbeans declines to warn about it.

Figure 05 - Adding braces doesn't solve the problem.
Figure 05 - Adding braces doesn't solve the problem.

Figure 06, below, illustrates the output of the program after adding the braces and leaving in the rogue semi colon.

Figure 06 - The program still produces unexpected results with braces added. The braces have no impact because the semi-colon has already terminated the loop.
Figure 06 - The program still produces unexpected results with braces added. The braces have no impact because the semi-colon has already terminated the loop.

More by this Author


Comments 3 comments

king214 6 years ago

I'm new to java and this will be useful!

Thanks!


Vannspreder 5 years ago

Thank you so much. I have been tearing my eyes out for just this extra ; in the back of the "for" line. This was very useful indeed:)


nicomp profile image

nicomp 5 years ago from Ohio, USA Author

@Vannspreder : Yep, that one will drive you nuts. Thanks for writing!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working