Determining whether String Array is empty

  • Thread starter news.amnet.net.au
  • Start date
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
 
M

Mike Schilling

news.amnet.net.au said:
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.

No. allfiles is a reference, and it's entirely valid to set it to null.
You might get an NPE later if, for examople, you tried to get the array's
length:

if (allfiles.length > 0) // NPE
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");\

This makes no sense. The value of allfiles at the time that
getParameterValues is called is irrelvant; it certainly won't determine
whether the call to getParameterValues throws a NullPointerException or not.
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.
Unless you have hundreds of possible tick boxes, I wouldn't worry about the
size of the array.
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..

}

That won't compile. Perhaps you have

if (allfiles == null)

or

if (allfiles.length == 0)
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.

Not at that statement you don't. What is the statement that gets the NPE?

Please show the exact code you're using, and show *exactly* where the NPEs
occur. I think you'll find it's where you're dereferencing a null pointer.
What you need is to be careful about that. Replace, for instance

if (allfiles.length == 0)

with

if (allfiles == null || allfiles.length == 0)
 
J

Jacob

news.amnet.net.au said:
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?

A java array can hold 0 elements.

String[] allFiles = new String[0]

is fine. It is empty if allFiles.length == 0.

However, it can also be "null": It is your responsibility
as programmer to associate these cases with something meaningful.
"null" typically mean non-existent and 0 mean exsistent and
empty. These are conceptually different and is a source of many
program misbehaviour, bad-coding and misunderstandings. In all
cases you must have a consise understanding of wether you allow
null, 0 or (which) n elements. Be particular careful during
initialization that you never violate your own rules.

As I read your post you probably should not allow "null". If your
"request.getParameters()" method return null, you probably should
initialize allFiles with 0.
 
N

Nigel Wade

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.

You need to post where the actual Exception is being thrown. The
invocation of request.getParameterValues() should not throw a null pointer
exception unless request is null. It can, however, return a null reference
and if you attempt to access a method or member via a null reference you
will get a NPE at that point.
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");

How is that supposed to help? The value stored into allfiles is exactly
the same as above. The fact that it previously had a value assigned to it
makes no difference. If getParameterValues returns null, allfiles will be
null.

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.

No, it contains exactly what it did before.
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..

You can't test an array (reference) for equality with 0. You need to test
if it's null. Your code won't compile.
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.

Not very likely as the code won't compile. Perhaps you test allfileslength
for 0, but that's a constant isn't it, so the test is pointless?
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?

It can either be null, or empty. If the request does not contain the
parameter "add_relationships" then the returned value from
getParameterValues is null (read the contract in the Javadocs). You need
to test for this after the call. It might also be that an empty array is
returned and allfiles.length is 0, but you need to test whether allfiles
is null first.
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?

There is no "best way" to initialise the array - the method returns an
array reference which you cannot initialise. You need to check whether the
returned array reference is null before you do anything else.
 
D

dar7yl

news.amnet.net.au said:
I have a bit of an unusual problem <snip...>
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.

Have you considered using a Vector instead of an array?

The Vector is ideal for sparse-array handling. but adds a slight overhead
for accessing elements.
Also, any index->member mapping would have to be done explicity, instead of
direct array[index], as the index of a member in Vector is determined by the
order of insertions (in the general case).

regards,
Dar7yl
 
M

Mark Wright

The Vector is ideal for sparse-array handling. but adds a slight overhead
for accessing elements.

It's also not recommended for new applications, though not deprecated.
Preference should be given to ArrayList or one of the other Collections
classes depending on requirements.
Also, any index->member mapping would have to be done explicity, instead of
direct array[index], as the index of a member in Vector is determined by the
order of insertions (in the general case).

In which cases is it not (in ways which differ from an array)?

Mark Wright
- (e-mail address removed)

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
===================================================
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top