• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting a java.lang.StringIndexOutOfBoundsException Error  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following generates a java.lang.StringIndexOutOfBoundsException Error

The string being parsed is:

//sgai-fs02/qmsattachments$/QMSAttachments/Documents/2017/61487_Documents_1506621727828.pdf



I am wanting to point to a file to make an attachment in javax.mail I am not sure if I use a UNC or URL path.
 
Knute Snortum
Sheriff
Posts: 4210
124
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a good situation to illustrate why creating an SSCCE (short, self-contained, complete, compilable example) is such a good idea.  What happens when you execute this?
You get a better error, and there is nothing else to get in the way of seeing what's wrong.  Here's what I got:
Can you see what's wrong now?
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: Can you see what's wrong now?


I see it is line 7 but I thought I was using an escape in the replaceAll method "//". Do I have to make provision for the double "//" is the root string?

By the way,  I like your quote by the Apostle Paul in I Corinthians
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Can you see what's wrong now?


Ok, I changed the replaceAll("/", "\\") with replaceAll("/", "\\\\") and am not getting the exception.

But me attachment code is still failing:

UnionNotification: //sgai-fs02/qmsattachments$/QMSAttachments/Documents/2017/61487_Documents_1506621727828.pdf
UnionNotification: \\sgai-fs02\qmsattachments$\QMSAttachments\Documents\2017\61487_Documents_1506621727828.pdf
SystemOut     O HourBankWebApp: setSendEmailMessageJMailV6: 3: \\sgai-fs02\qmsattachments$\QMSAttachments\Documents\2017\61487_Documents_1506621727828.pdf
SystemOut     O HourBankWebApp: setSendEmailMessageJMailV6: 3: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
java.io.FileNotFoundException: \\sgai-fs02\qmsattachments$\QMSAttachments\Documents\2017\61487_Documents_1506621727828.pdf (A file or directory in the path name does not exist.)

Again, if I paste this path into browser it displays the contents of the file as expected.

 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need to replace / with \ ? What happens if you don't do the replaceAll?
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Do you really need to replace / with \ ? What happens if you don't do the replaceAll?


I get this:

SystemOut     O UnionNotification: //sgai-fs02/qmsattachments$/QMSAttachments/Documents/2017/61487_Documents_1506621727828.pdf
SystemOut     O HourBankWebApp: setSendEmailMessageJMailV6: 3: //sgai-fs02/qmsattachments$/QMSAttachments/Documents/2017/61487_Documents_1506621727828.pdf
SystemOut     O HourBankWebApp: setSendEmailMessageJMailV6: 3: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
java.io.FileNotFoundException: /sgai-fs02/qmsattachments$/QMSAttachments/Documents/2017/61487_Documents_1506621727828.pdf (A file or directory in the path name does not exist.)
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it figured out. It was the attachment code in the javax.mail class. I changed up how the class handled the URL path.

Thanks for all the help.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only use replaceAll if you need to replace something based on a regular expression. If you want to replace a simple string with another, use replace instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!