Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with strings

 
Sagarika Seetaraman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am facing a small problem while using the substring method.

I am using a simple statement as
if(url.substring(url.lastIndexOf("/")+1,url.indexOf(".")).equals("Some Value"))
{
// do something
}

I tested this statement by assigning different values for url. It works fine. But when i tested it on Prod, it gave me this exception.


Exception in thread "main" java.lang.StringIndexOutOfBoundsException
at 0x00362c70: java.lang.Throwable.Throwable() (/usr/lib/./libgcj.so.3)
at 0x00355b3f: java.lang.Exception.Exception() (/usr/lib/./libgcj.so.3)
at 0x003593f7: java.lang.RuntimeException.RuntimeException() (/usr/lib/./libgcj.so.3)
at 0x0035665b: java.lang.IndexOutOfBoundsException.IndexOutOfBoundsException() (/usr/lib/./libgcj.so.3)
at 0x0036018b: java.lang.StringIndexOutOfBoundsException.StringIndexOutOfBoundsException() (/usr/lib/./libgcj.so.3)
at 0x0034766f: java.lang.String.substring(int, int) (/usr/lib/./libgcj.so.3)
at 0x0047cff7: ffi_call_SYSV (/usr/lib/./libgcj.so.3)
at 0x0047cfb7: ffi_raw_call (/usr/lib/./libgcj.so.3)
at 0x0032a658: _Jv_InterpMethod.continue1(_Jv_InterpMethodInvocation) (/usr/lib/./libgcj.so.3)
at 0x0032af64: _Jv_InterpMethod.run(ffi_cif, void, ffi_raw, _Jv_InterpMethodInvocation) (/usr/lib/./libgcj.so.3)
at 0x00328474: _Jv_InterpMethod.run_normal(ffi_cif, void, ffi_raw, void) (/usr/lib/./libgcj.so.3)
at 0x0047ce6c: ?? (??:0)
at 0x0033cd68: gnu.gcj.runtime.FirstThread.call_main() (/usr/lib/./libgcj.so.3)
at 0x003a6e99: gnu.gcj.runtime.FirstThread.run() (/usr/lib/./libgcj.so.3)
at 0x00349aec: _Jv_ThreadRun(java.lang.Thread) (/usr/lib/./libgcj.so.3)
at 0x0031682c: _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/./libgcj.so.3)
at 0x08048930: ?? (??:0)
at 0x006fa79d: __libc_start_main (/lib/tls/libc.so.6)
at 0x080486e1: ?? (??:0)



Has anybody faced a similar problem?? Would appreciate quick response on this matter.

Thanks
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you test it with the URL ending with a "/" character? Take a look at the error (StringIndexOutOfBoundsException) and look at your code -- you're adding 1 to the index without checking to see if you exceed the length of the string.
 
Sagarika Seetaraman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur reply...

Actually I am testing with the right url... The url is of a standard form.. So the case that u cited may not occur.
 
Sagarika Seetaraman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel,

I feel that in the case that u mentioned, the stack trace would have been different... and would have pointed out the error location...
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sagarika Seetaraman:
Thanks for ur reply...

Actually I am testing with the right url... The url is of a standard form.. So the case that u cited may not occur.


It only takes 10 seconds to add a println() to your program just prior to the substring call. Better to be sure that something "will not occur" than "may not occur".

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic