I have a java client server program that works fine on a half a dozen computere but is causing a NegativeArraySizeException on site.
This is the code
location = message.indexOf("last");
location += 5;
end = message.indexOf('&', location);
int size = end - location; THIS IS THE ERROR LINE
char[] lastC = new char[size];
message.getChars(location, location+size, lastC, 0);
String firstS = new String(firstC);
String 开发者_开发百科lastS = new String(lastC);
message is an xml message I am reading. location is an integer that points the the location of a character in the message, the first name in this case. size is the length of the persons name.
As far as I can tell size is being set as a negative number and I don't know why.
Does anyone know how to fix this or a better was of finding the length of the name ?
This is part of the server side.
As far as I can tell size is being set as a negative number and I don't know why.
If the first call to indexOf
cannot locate "last"
in the message, then location will be set to -1
and then incremented by 5
to give 4
.
If the second call to indexOf
cannot find a '&'
then end
will be set to -1
, and size
will be negative.
Obviously, the input XML is not in the form you expect.
All in all, that code is pretty dodgy. As a minimum you should check the results of both calls to indexOf
and take appropriate error reporting / recovery steps if they are -1
.
But the real fix is to not attempt to "parse" XML using crufty string bashing. Use an XML parser, preferably with validation against the relevant schema or DTD. If the XML parser rejects the input, report the error back at who / whatever gave you the broken XML.
Does the message contain last
at all? If not, location
would be negative, and you should stop processing right there. This error might go unnoticed because you add 5 to location
after that, which makes it equal to at least 4 even if last
is not in the string (thanks SJuan76).
Even if the message contains last
, is it guaranteed that it is followed by at least one extra character? If not, adding 5 to location
would point outside the string for sure.
Moreover, end
may also be negative if there is no &
in the string after location
. You should handle that somehow (i.e. by setting end
to the length of the message in that case).
Also, if message
is a string, you can simply extract a substring of it using the substring
method, no need for the getChars
magic.
A better solution would probably be something like this (I'm assuming that message
is something like an URL and you are looking for the part between last>
and the next &
, based on your comment for one of the other answers):
location = message.indexOf("last>");
if (location >= 0) {
String lastS;
location += 5;
end = message.indexOf('&', location);
if (end == -1) {
// Handle the case when there is no "&" after "last>" in the message
} else {
lastS = message.substring(location, end);
}
} else {
// Handle the case when there is no "last>" in the message
}
The trouble is that end is less than location. The issue is what message are you expecting and which one you are receiving; the rest of the logic works for certain messages. Check from where you get your message String.
精彩评论