This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop prob - please HELP!!

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For more info about Exception handeling:
Sun Tech Tip
Chris
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic