• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

arrayindexoutofboundexception in HeadFirst "DotComBust"

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey
i started with java 4 days ago and i am a proud owner of the book "head first" (german version)
my first problem now at chapter 6 dotcombust
its no problem to compile, but when i try to start it shows me the first 3 lines and then:
Exception in thread "main" java.lang.arrayindexoutofboundexception: 55

in lines:
DotComVersenken.java:28 ArrayList<String> neuerOrt = helfer.platziereDotCom(3);
DotComVersenken.java:82 spiel.spielEinrichten();
SpielHelfer.java:46 if (raster[ort] == 0) {


here is the code



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't looked closely (all that german naming looks like encoded source ) but that line is doubtful :

should be
 
Thomas Mahl
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nah, didnt help
the code is the same like it is in the book.
i dont understand why its possible to compile but not to run
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have the book, and I don't read German, so I can only point to what I can figure out.

On line 8 of source for SpielHelfer, you set an int to 49; on line 9, you use that int to create an array.

on line 32 you declare an int called ort, and on line 41 you set it to the result of a random value multiplied by some other value.

so the int 'ort' can be any number resulting from that multiplication, which seems to become 55 at some point. That's too high an index for the array you've declared.

It compiles fine because the compiler does not attempt to figure out all possible values of ort and warn you if it might be getting too large for your arrays -- you have to do that yourself. Java is a strongly typed language, but it cannot tell you if your logic is correct.

rc
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thomas Mahl wrote:
i dont understand why its possible to compile but not to run



Of course, it possible. Code that compiles just means that the instructions are valid and the compiler can generate code to do what you want. You code did run, it just tried to do something that the JVM detected as illegal and generated a runtime exception.

Henry
 
Thomas Mahl
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ralph Cook wrote:

so the int 'ort' can be any number resulting from that multiplication, which seems to become 55 at some point. That's too high an index for the array you've declared.

rc



the array (you talked about) from before (size 49) * the random --> shouldnt be higher than 49
think i should write all in english that you can understand it better
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see what you mean now -- since random() returns 0 to less-than-1.0, and it is mutliplied by 49, then the result is going to be 0-49.

The message is telling you that ort is equal to 55, which is certainly out of range. I suggest that you trust the runtime is telling you that correctly, i.e., that it is becoming 55 because of something you wrote your code to do, and figure out where you tell it that. Set breakpoints or trace statements at the places where ort changes. I looked at it a while longer, but haven't found the bug. The combination of bad formatting and a language foreign to me don't help, I'm afraid.

rc
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's also some code that increments the variable too -- maybe that is somehow involved?

And I 100% agree. If the computer is telling me one thing, and I believe it's another -- it's a good idea to just get over the "it can't happen phase" and hunt down why it's happening.

Henry
 
What's that smell? I think this tiny ad may have stepped in something.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic