• 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

Error printing out from a read in file.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error I am having here is that my code is only printing the first and second number from the file and it is set out like this
-------------------
12 2
? ?
? ?
? ?
? ?
? ?
-------------------
Without the ---- and I can only remember the 12 2 because they print, but everything else isn't appearing and it is letting me compile HOWEVER when it runs it catches an error that says "Error: Index 2:, Size: 2"

When I was just reading and printing the file using:

It was working correctly.


But this is the main code and 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
HI Ryan,

Welcome to the Ranch!

You've only shown half your program, and I don't believe the error is coming from this half. Can we see the rest, please?

Also, if you're getting the whole stack trace from that error message , then the top few lines of the trace should tell you exactly on which line of your program the error occurs; if you don't know how to interpret that, you can paste it into your message too and we can explain it to you.
 
Ryan Tracey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I edited the code above so it shows all the Class. The reason I didn't enter it though is because I made a random For Loop that should of run for plenty of time but stuck at the same error so I didn't want to throw uneccesary code :S.

As for the error I am getting -- The program compiles BUT when I run it the Try/Catch throws me the error from above :S.

I know when I get compile errors to try and hunt it down to the line and fix it NP but I have never really used Try/Catch lol and it's just giving me the error from above.

But as requested mate, the full Class is now there thanks for taking an interest!

/|RT
 
Ernest Friedman-Hill
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 can't say I can quite follow the logic in that last "while" loop, but that's clearly where the problems are coming in. The line

ArrayWP[i] = new Waypoint(AListWP.get(i),AListWP.get(i+1));

makes a lot of assumptions about the relationship between the size of the list and of the array and the value of "i", and what's happening is that one of those assumptions just isn't true. Try adding some "println"s to help you visualize what's happening -- for example



I'll guarantee you'll be surprised by something in the results!">
 
Ryan Tracey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm I have an infinite loop lol must be to do with my flag not becoming false due to problem you mentioned :S.

Could you help me write what I meant?

I need a new array of my class Waypoints it should be 1/2 the size of the AListWP because that is where its reading in the info 2 at a time.

Then I made a for loop to populate the Waypoint array. The way I figured the logic on this was to increase i by 2 each time so I can use i to get out info sequentially from the AListWP ArrayList.

Then once j is the same as the size of the ArrayList it should end the while loop.

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That for loop with j + 2 looks a very flaky construction to me. It would appear you are using a loop which creates one Waypoint.
Why don't you create your Waypoints as you read the file?
What format is the file in? Is it text? If so, why don't you use a Scanner to read it? You can use its nextXXX and hasNextXXX methods, and call nextXXX twice to create a Waypoint. Then you can add the Waypoint directly to the list.
If you have a List, why are you using an array as well?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: you need to ensure you have an even number of entries in your data file, otherwise you will end up creating half a waypoint, and (if you are lucky) suffering an Exception which closes your application.
 
reply
    Bookmark Topic Watch Topic
  • New Topic