Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes for loop prob - please HELP!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop prob - please HELP!!" Watch "for loop prob - please HELP!!" New topic
Author

for loop prob - please HELP!!

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I am having problems with a for loop. This for loop is nested within some other for loop. the problem I'm having is that the for loop executes once and then exits to execute the outer for loop. my initial reaction was that my condition (in the for loop ) was false the second time round thus resulting in the exit. I'm usually told to paste some code so i'll do that:

for (int k=0; k< column.size(); k++){ is the loop causing me problems. column.size() evaluates to 2 so what is the problem?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, I see a "try" block right around the for loop. Perhaps there's an empty "catch" associated with this "try", and the loop is throwing an exception (a NullPointerException or a ClassCastException, most likely) which is being ignored.


[Jess in Action][AskingGoodQuestions]
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Hi,
First thing to look at:
I see you have a "try" around the loop that is causing you the problem. Is it possible that an Exception is being thrown within you loop. If this was true you loop would not run the second time instead it would jump to your "catch".
Chris.


SCJP 1.2, SCWCD, SCBCD
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I have a catch block after the try block which catches ClassCastException. I can't think of any other exceptions that may be thrown. Any other ideas?
[ March 16, 2004: Message edited by: Naf Rash ]
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Your code could be throwing a NullPointerException or an ArrayIndexOurOfBoundsException
To find out if this is happening change you

If this an Excpetion is thrown then you should be a see the stack print to the screen. This will tell you if can exception has occured. If you are not used to reading th trace, post it here and we will have a look for you.
---
Reading your post again are your saying you have:

If this is true, what is happening is that a ClassCastException is being thrown and causing your for loop to break. If you want some thing to happen if this problem occurs you need to put code in where I have the comment "//Nothing in here???".
The reason this is happening is beacuse you are casting an row.get(i) as a Vector. Are you sure it is a Vector? You are also casting the result of ((Vector)row.get(i)).get(k) as a Long. Are you sure it is a Long?
You may be interested to know that you are able to check to type of Object before you cast. Below is a common way of doing this:

For debuging I find it useful to do

Hmm that call may be a little out as I am wirtting it in this editor. You may want to check, but you should be able to get the idea. What should happen is that the name of the class the object is an instance of should be printed.

Chris.
[ March 16, 2004: Message edited by: Chris Harris ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

And what do you do when catching?
At least - we hope - print a message - better stack trace while in development, so you know for sure what's happening...?
Hurry to your code, and don't answer without filling in the print-statements first!
Then you're welcomed back


http://home.arcor.de/hirnstrom/bewerbung
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
For more info about Exception handeling:
Sun Tech Tip
Chris
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Thanks for your help - but I made a really stupid mistake - my try block was throwing an exception and it was being caught. Within my try block there was a println statement which showed whether the value ok k in the inner loop was being updated but this was not being reflected since the exception was being thrown before reaching that statement. Sorry. Thanks for all your help though - I really do make the silliest of mistakes!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: for loop prob - please HELP!!