• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop prob - please HELP!!

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic