Strange StringIndexOutOfBoundsException

D

dutone

I get the above exception ('String index out of range: -5') at random
points during readMultiLine()'s network read (listed below). According
to doc. the only method that could be throwing this is sb.delete() but
it doesnt meet the conditions for the exception. When I run it on a
file, no error, only when doing network io.
Any Ideas?????


main()
{
//Init......
String lines[] = readMultiLine();
// ....
}

private String readLine() throws IOException
{
int c;

sb.delete(0,sb.length());
c = is.read();

while( c > 0 ) {
sb.append((char)c);
if( (char)c == '\n' ) break;
c = is.read();
}

return ( (sb.length() > 0) ? sb.toString():null);

}

private String[] readMultiLine() throws IOException
{
String retval[] = null;
String x = readLine();


lineBuffer.removeAllElements();
while(x != null) {

if( x.equals("^\r\n") ) break;

lineBuffer.addElement(x);
x = readLine();
}

retval = new String[lineBuffer.size()];
lineBuffer.copyInto(retval);


return (retval);
}
 
P

Paul Lutus

dutone said:
I get the above exception ('String index out of range: -5') at random
points during readMultiLine()'s network read (listed below). According
to doc. the only method that could be throwing this is sb.delete() but
it doesnt meet the conditions for the exception. When I run it on a
file, no error, only when doing network io.
Any Ideas?????

Just one. Tell us which line of your code throws the exception.
 
J

John C. Bollinger

dutone said:
I get the above exception ('String index out of range: -5') at random
points during readMultiLine()'s network read (listed below). According
to doc. the only method that could be throwing this is sb.delete() but
it doesnt meet the conditions for the exception. When I run it on a
file, no error, only when doing network io.
Any Ideas?????

As others have written, provide a complete example and a full
description of the exception (preferably including the stack trace).
There are some problems and potential problems in your code that I can
still comment on without that, however:
private String readLine() throws IOException
{
int c;

sb.delete(0,sb.length());

Chances are you would be better off making sb a local variable (and
creating a new instance at each invocation of this method).
c = is.read();

while( c > 0 ) {
sb.append((char)c);

Reading a byte from an InputStream (the presumed type of "is") and
casting it to a char is fundamentally unsound. You should be wrapping
your InputStream in an InputStreamReader that specifies the correct
character encoding for the characters. You might still cast the
resulting ints to char, but you would know that they actually represent
chars.
if( (char)c == '\n' ) break;

The explicit comparison against '\n' is non-portable. It would fail on
systems that do not use '\n' or "\r\n" as a line terminator for text
files, such as Mac.
c = is.read();
}

return ( (sb.length() > 0) ? sb.toString():null);

}

private String[] readMultiLine() throws IOException
{
String retval[] = null;
String x = readLine();

See below regarding readLine().
lineBuffer.removeAllElements();

The lineBuffer variable really ought to be local to this method. It
looks like a Vector; you would probably be a bit better off using one of
the other List implementations (and the methods of the List interface
instead of the Vector-specific ones), but that doesn't look like it's
causing an actual problem.
while(x != null) {

if( x.equals("^\r\n") ) break;

Are you trying to stop at a line containing just a single '^' character?
That equals() comparison is wrong if you're trying to match a blank
line, and it will fail in many cases when the input was not generated on
Windows.
lineBuffer.addElement(x);
x = readLine();

If you wrap your input in a BufferedReader then you can use its
readLine() method, which solves many of the non-portable line
termination problems exhibited by your code. There is a difference that
you should be aware of, however: BufferedReader.readLine() does not
include the line termination characters (whatever they are) in the
Strings it returns. Using a BufferedReader is often a considerable
performance win over an unbuffered Reader or InputStream, too.
}

retval = new String[lineBuffer.size()];
lineBuffer.copyInto(retval);


return (retval);
}

I don't see the index problem in the code you posted. Most likely it is
in the code you did not post -- either in code that you cut out of these
methods or in other methods. Create a small, self-contained example
that exhibits the bug [check that!] and post it.


John Bollinger
(e-mail address removed)
 
D

dutone

Paul Lutus said:
Just one. Tell us which line of your code throws the exception.

It doesnt say, it just says uncought exception StringIndexOutOfBounds:
String Index out of range -5. I've tried catching it, but to no avail
-so I cant say for sure where its coming from. I want to say at the
x.equals("^\r\n") but only because the "^\r\n" string is 5 chars long
and the exception says the index is -5.
 
P

Paul Lutus

Andrew said:
I get the above exception ...

For help on those strange exceptions, see...
<http://www.physci.org/codes/javafaq.jsp#exact>

Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[ the people ther are *much* nicer, trust me.. ;-) ]

People new to Usenet should be aware that its status as an open,
non-commercial forum causes grievous anguish to those who would like to
make money from it, either by advertising in it, or by drawing people away
from it, or both.

Nearly the entire Internet has been transformed into a vast commercial
enterprise, coplete with carpetbaggers, virtual open-air carnivals, and
hucksters of every stripe. Usenet is one of the last vestiges of the old
Internet, before the transformation.

Now we see Usenet daily bombarded by what amount to echoes of the "other"
Internet -- the one that, instead of trying to tell you something, is
trying to sell you something.

The site being promoted above as a "better group" is in fact not part of
Usenet at all. It may have better, more to-the-point content (as seems tto
be so), But people should be aware that it also serves someone's commercial
interest, which Usenet cannot do.
... the people ther are *much* nicer, trust me ...

Yes, in the same way that a used car salesman is nicer than, say, a
scientist who by disposition is compelled to tell you the truth. It all
depends on what you want.

On visiting the site, one is immediately impressed by its values:

"Make a donation"

"This space 4 hire! We are currently inviting bids for this space on a
per-impression basis."

All I am saying is this site is not part of Usenet. It functions on
different principles. It is not fairly described as a "better group" as
though it were part of Usenet. Having said that, it has plenty of useful
content, representing countless hours of effort by many people.

Caveat emptor.
 
P

Paul Lutus

dutone said:
It doesnt say,

But it could, if you will provide a try ... catch clause.
it just says uncought exception StringIndexOutOfBounds:
String Index out of range -5. I've tried catching it, but to no avail

Try harder. Wrap the code section in a try .. catch clause.
-so I cant say for sure where its coming from. I want to say at the
x.equals("^\r\n") but only because the "^\r\n" string is 5 chars long
and the exception says the index is -5.

What is in x at that point?
 
C

Chris Uppal

Paul said:
Andrew said:
Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[ the people ther are *much* nicer, trust me.. ;-) ]
[...]
The site being promoted above as a "better group" is in fact not part of
Usenet at all. [...]

It seems to me that Paul has a good point here. If your (Andrew's) aim is to
direct people to comp.lang.java.help, then wouldn't a link like
(or whatever the URL syntax is) be better ? With, by
all means, an additional link to your faq as a side-order of extra information.

-- chris
 
A

Andrew Thompson

,,
The site being promoted above as a "better group" is in fact not part of
Usenet at all.

The site listed above is my site, it merely contains
further information on the group I am 'promoting',
< comp.lang.java.help >
...which BTW, is one that we both post to, (and
very much part of Usenet)..
..It may have better, more to-the-point content (as seems tto
be so), But people should be aware that it also serves someone's commercial
interest, which Usenet cannot do.

I was not proposing the OP post messages to my site.
I neither run, nor am interested in running, forums,

Usenet is just fine, and I discourage people from posting
through 'web-based interfaces' to Usenet, as these sites
generally do not give posters adequate information of the
chaotic, 'rough and tumble' nature of Usenet (nor point
out that Usenet is not a help-desk)

My only point, in that post, is that questions from
new Java programmers are better dealt with on the
*Usenet* group specifically intended for them.

I do not quite understand where you are coming
from Paul, did I/we get wires crossed somehow?
 
A

Andrew Thompson

Paul said:
Andrew said:
Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[ the people ther are *much* nicer, trust me.. ;-) ]
[...]
The site being promoted above as a "better group" is in fact not part of
Usenet at all. [...]

It seems to me that Paul has a good point here. If your (Andrew's) aim is to
direct people to comp.lang.java.help, then wouldn't a link like
news:comp.lang.java.help (or whatever the URL syntax is) be better ?

Feel free to do that Chris.

I, OTOH, will continue to post as I did, because I have
already considered this matter and come up with what I
feel is the best (all round, balanced) approach.

The page lists the major Java groups and gives
a sentence or two that provides further description
of each. Then it links to the easiest place to
browse/search those groups.
 
C

Chris Uppal

Andrew,
It seems to me that Paul has a good point here. If your (Andrew's) aim
is to direct people to comp.lang.java.help, then wouldn't a link like
news:comp.lang.java.help (or whatever the URL syntax is) be better ?

Feel free to do that Chris.

I, OTOH, will continue to post as I did, because I have
already considered this matter [...]

Fair enough. 'Twas only a suggestion.

(And, to be honest, an attempt to bring out what I saw as a valuable point in a
post that seemed almost designed to be misunderstood.)

-- chris
 
P

Paul Lutus

Andrew said:
The site listed above is my site, it merely contains
further information on the group I am 'promoting',
< comp.lang.java.help >
..which BTW, is one that we both post to, (and
very much part of Usenet)..

You very clearly describe your *Web* *site*, not comp.java.lang.help, as a
*group*. See above and below.
I was not proposing the OP post messages to my site.
I neither run, nor am interested in running, forums,

In that case, what does this mean:
Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[  the people ther are much nicer, trust me..  ;-)  ]

You describe it as a "group", and infer that it is a group like this one,
but nicer. What conclusion is a newbie meant to draw?.

The remedy is to describe your site as ... hmmm ... a Web site? Not a group,
and without any inference of comparability to Usenet?
Usenet is just fine, and I discourage people from posting
through 'web-based interfaces' to Usenet, as these sites
generally do not give posters adequate information of the
chaotic, 'rough and tumble' nature of Usenet (nor point
out that Usenet is not a help-desk)

Then in the future, you won't want to be describing your site as a "group"
and comparing your Web site to Usenet groups as though there is a
functional basis for comparison. Right?
My only point, in that post, is that questions from
new Java programmers are better dealt with on the
*Usenet* group specifically intended for them.

That is not what you said. By implication, you are trying to create a
distinction between things that are not comparable, as though your site
needed to be distinguished from Usenet. It doesn't. Your Web site is not a
group.
I do not quite understand where you are coming
from Paul, did I/we get wires crossed somehow?

I have been crystal clear.
 
P

Paul Lutus

Andrew said:
Paul said:
Andrew Thompson wrote:
Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[ the people ther are *much* nicer, trust me.. ;-) ]
[...]
The site being promoted above as a "better group" is in fact not part of
Usenet at all. [...]

It seems to me that Paul has a good point here. If your (Andrew's) aim
is to direct people to comp.lang.java.help, then wouldn't a link like
news:comp.lang.java.help (or whatever the URL syntax is) be better ?

Feel free to do that Chris.

I, OTOH, will continue to post as I did, because I have
already considered this matter and come up with what I
feel is the best (all round, balanced) approach.

Fine, as long as you do not continue to mislead people as you have been
doing.
The page lists the major Java groups and gives
a sentence or two that provides further description
of each. Then it links to the easiest place to
browse/search those groups.

That is not what you said. You directed a poster to your site, using the
word "group" and suggesting that your site is a "better group" than this
one. You are beginning to show an integrity chasm.
 
A

Andrew Thompson

Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[  the people ther are much nicer, trust me..  ;-)  ]

You describe it as a "group", and infer that it is a group like this one,
but nicer. What conclusion is a newbie meant to draw?.

The remedy is to describe your site as ... hmmm ... a Web site? Not a group,
and without any inference of comparability to Usenet?

You could refer to the above as
a) A string of letters describing an URL..
b) An URL to a web-page..
c) A web-page containing information on..
d) ..A newsgroup.

So I might, in fact, have said..
"Click the string of letters below, that represent
an URL pointing to my site, where I describe a better
group.."

I won't, since that is longer, & especially given
the number of times it needs saying..

But hey,.. feel free to follow me around 'warning'
readers that it is an URL to a web-site.
 
P

Paul Lutus

Andrew said:
Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[the people ther are much nicer, trust me..  ;-)  ]

You describe it as a "group", and infer that it is a group like this one,
but nicer. What conclusion is a newbie meant to draw?.

The remedy is to describe your site as ... hmmm ... a Web site? Not a
group, and without any inference of comparability to Usenet?

You could refer to the above as ...

Okay, your post clearly shows you are intent on avoiding any personal
accountability.
I won't, since that is longer, & especially given
the number of times it needs saying..

It certainly doesn't need saying as you have been saying it.
But hey,.. feel free to follow me around 'warning'
readers that it is an URL to a web-site.

And your conscience was surgically removed. Maybe used car sales is your
true calling after all.
 
A

Andrew Thompson

Andrew Thompson wrote: ....

And your conscience was surgically removed. Maybe used car sales is your
true calling after all.

For anyone that reads that comment in any future post..

"I invite you to point your mouse at (then click on)
the string of letters below which represent an URL
to the Google web-site that shows the first post to the
Usenet newsgroup comp.lang.java that led to that comment
<http://google.com/groups?th=5b25be50d1bd6126#link8>

Please feel free to read the words contained in
the abovementioned comp.lang.java thread,
and to come to your own (damn) conclusions."
 
P

Paul Lutus

zoopy said:
Andrew Thompson wrote:

On 25 Aug 2004 00:14:35 -0700, dutone wrote:


I get the above exception ...

For help on those strange exceptions, see...
<http://www.physci.org/codes/javafaq.jsp#exact>

Also, it seems a better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
[ the people ther are *much* nicer, trust me.. ;-) ]
.

On visiting the site, one is immediately impressed by its values:

Certanly not immediately.

That outcome depends on your ethical sensitivy, I guess.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top