• 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

Why compile fine but run with exception?

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
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
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect this actually belongs in the SCJD forum.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic