N
news.amnet.net.au
Hi
I have a bit of an unusual problem. I am obtaining String Array values from
a form, so that in the jsp that receives the post from the form, I have this
line:
String [] allfiles = request.getParameterValues("add_relationships");
The parameter values "add_relationships" are a set of tickboxes in a table,
each posting a string value when chosen. If however, the user selects
nothing and still presses the "submit" button, I get a
java.lang.NullPointerException. I can understand that, as I am initialising
as well as populating my String Array in the above line and if there is
nothing coming from "add_relationships" I suppose you could get such an
error.
So I changed the code so I first pass on another parameter, namely the total
possible number of tick boxes that can be selected - and I initialise my
String Array to that:
int allfileslength = Integer.parseInt(request.getParameter("itemslength"));
String [] allfiles = new String [allfileslength];
allfiles = request.getParameterValues("add_relationships");
I am not sure if I like this though, as the Array is now too large and
surely must contain a lot of null values apart from the tick boxes chosen by
the user. Apart from that, if the user chooses nothing and there is nothing
in allfiles, I would like to test for it, so I have the following statement:
if (allfiles == 0) {
.... do something..
}
However, when nothing is selected the statement if (allfiles == 0) { does
not seems to work, i.e. I get never inside that conditional statement to do
whatever I want to do. Instead I still get a NullPointerException.
This is perhaps not the most complicated question to ask, and perhaps a bit
stupid, but I am afraid this has me confused. Surely if nothing is passed
into to the Array and the whole array is empty, the Array can be said to be
equal to null?
So the question really is: what is the best way of initialising an Array
that obtains a variable number of ParameterValues and what is the best way
of checking whether the Array contains no values at all?
Any help will be greatly appreciated.
Thanks
Hugo
I have a bit of an unusual problem. I am obtaining String Array values from
a form, so that in the jsp that receives the post from the form, I have this
line:
String [] allfiles = request.getParameterValues("add_relationships");
The parameter values "add_relationships" are a set of tickboxes in a table,
each posting a string value when chosen. If however, the user selects
nothing and still presses the "submit" button, I get a
java.lang.NullPointerException. I can understand that, as I am initialising
as well as populating my String Array in the above line and if there is
nothing coming from "add_relationships" I suppose you could get such an
error.
So I changed the code so I first pass on another parameter, namely the total
possible number of tick boxes that can be selected - and I initialise my
String Array to that:
int allfileslength = Integer.parseInt(request.getParameter("itemslength"));
String [] allfiles = new String [allfileslength];
allfiles = request.getParameterValues("add_relationships");
I am not sure if I like this though, as the Array is now too large and
surely must contain a lot of null values apart from the tick boxes chosen by
the user. Apart from that, if the user chooses nothing and there is nothing
in allfiles, I would like to test for it, so I have the following statement:
if (allfiles == 0) {
.... do something..
}
However, when nothing is selected the statement if (allfiles == 0) { does
not seems to work, i.e. I get never inside that conditional statement to do
whatever I want to do. Instead I still get a NullPointerException.
This is perhaps not the most complicated question to ask, and perhaps a bit
stupid, but I am afraid this has me confused. Surely if nothing is passed
into to the Array and the whole array is empty, the Array can be said to be
equal to null?
So the question really is: what is the best way of initialising an Array
that obtains a variable number of ParameterValues and what is the best way
of checking whether the Array contains no values at all?
Any help will be greatly appreciated.
Thanks
Hugo