This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

arrayindexoutofboundexception in HeadFirst "DotComBust"  RSS feed

 
Thomas Mahl
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!