Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why compile fine but run with exception?

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
I have a class:
class Hotel{
String hotelName;
}

and in another class I produce Hotel objects:
class ConnectDB {
public static Hotel[] getHotel(){
}
}

and now is my question,

class Data{
private Hotel[] hotels = ConnectDB.getHotel();
}

in Data I have the main method,in main i have a sentence :

Data da = new Data();
System.out.print(da.hotels[8].hotelName);

so Data.java compile fine, but while i run it,there is exception of java.lang.StringIndexOutOfBoundsException .
Why?Can anyone help me ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing here that would throw a StringIndexOutOfBoundsException; this is thrown by methods like String.charAt() that pick apart the characters of a String.

In any case, the compiler can only find so many problems; exceptions are due to problems that the compiler can't predict. It's perfectly normal for programs that compile fine to crash at runtime with an exception.
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
I want the result of System.out.print(da.hotels[8].hotelName) .But I failed to get it because of the exception.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the code that's throwing that exception. Can you show me the actual stack trace you get?
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
I have solved that problem by changing many codes. Thank you all the same.If I am more experienced in design class, problem like that will no longer happen.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect this actually belongs in the SCJD forum.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic