This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in simple for loop inside a method

 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a beginner and am learning how to use methods. The IDE I'm using is NetBeans.

Although I've spent a lot of time looking through the short program below, I can't figure out what's causing the errors in lines 13 and 14.



Could you please give me some suggestions as to how I can figure out what is causing these errors? I believe that the error is something very simple and really what I want to learn is how to go about finding the cause of those errors, rather than a solution for this particular program.

The error message from Netbeans is as follows;

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at liangch5.LiangCh5.sum(LiangCh5.java:14)
at liangch5.LiangCh5.main(LiangCh5.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


Thanks!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a close look at your for statement - particularly the final part - and compare it to the for loop in the Java tutorials. You should notice a crucial difference - you've added something.
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew, I see what I've done wrong by declaring the i variable at the end of the for statement. It was a stupid error that I just couldn't see.

Is there any way that I could have identified the exact cause of the error, from the error messages in Netbeans?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that error message was too vague to give you the hint. It is actually difficult to write good error messages; Java7 is better than earlier versions for its error messages.
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, I'm sure most programmers would be glad to see more helpful error messages.

I spent some time looking at that code without seeing the error. Would anyone have any tips on how to identify errors like that quicker?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry on not seeing it. It took me some time to see it as well.
Eclipse will actually put a little red squiggly line underneath the pieces of code that cause compiler errors. I'm pretty sure Netbeans can do the same.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse has better error messages than the Oracle compiler.
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, I might consider switching to Eclipse at some time to see if it provides better error reporting.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to copy it into eclipse to see the problem, in fact it underlined it for me.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the error messages I see when using the javac compiler on the command line:
While the text of the messages isn't much more informative than what you already saw, it's worth noting that they do provide a little ^ character which points to the approximate location in the line where the error occurred. Or at least, where the compiler thinks the error occurred. This isn't entirely reliable, since when the compiler doesn't understand what you're doing, its guess about what you are trying to do may be badly inaccurate. Still, the ^ can provide useful clues about what part of the line is the problem. In this case, the error at least points to the "int i++" part of the statement.

I don't know if NetBeans preserves the ^ part of the message that the compiler is giving you. If not, that's an extra reason to switch to another means for compiling and running your programs. Even using javac from the command line may give you more information. But maybe if you look closely, NetBeans might actually have some sort of visual indicator about which part of the line is the problem, the way eclipse and IntelliJ do.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might be better using the command-line and a decent text editor, leaving IDEs until you are more experienced.
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the sum function is not generalized. It will not work when k<j and for -ve inputs (j=-1 and k=-10). >
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that javac is giving three error messages for the price of one. If that happens, it is always worth looking at what appears to be the first error (marked by the caret symbol ^), correcting that and re-compiling.
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. Unfortunately my IDE doesn't show the " ^" symbol. I've been using an IDE for a little while now and find it easier than using a text editor and command line, so will probably stick with it at least for the moment.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but this suggests that NetBeans is a relatively crappy IDE if it loses the location info associated with those error messages. I think Eclipse or IntelliJ may serve you better in the long run. I've never used NetBeans much, but I can recommend either Eclipse or IntelliJ (I favor IntelliJ).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic