File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to check if vector is null? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to check if vector is null?" Watch "How to check if vector is null?" New topic
Author

How to check if vector is null?

Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Hi Friends,
I try to check if vbug which is vector is empty or not but the if line doesn't sound correct because I can not meet the line right after if and I am sure vbug is empty so could you please tell me what is the correct syntax if I want to check vbug is empty or not?
Many thanks,
Elahe


Thanks,
Elahe
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Elahe,
Try Vector.isEmpty()
That kind of information is always available in the
Java docs.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Ok, but if the Vector is null, you will run in problems. Try this:

Hope that helps
Rene
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Rene's post is an illustration that being null and being empty are two different things. If a Vector object reference is null, it means the reference is not pointing to any Vector object at all. However, a Vector object reference that points to an empty Vector object is not null.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3088
Elahe Shafie wrote:Hi Friends,
I try to check if vbug which is vector is empty or not but the if line doesn't sound correct because I can not meet the line right after if and I am sure vbug is empty so could you please tell me what is the correct syntax if I want to check vbug is empty or not?
Many thanks,
Elahe


Now that you've had it explained that there's a difference between null and empty, we need to back up a bit and go over a couple other things.

First, no Vector can ever be equal() to "". Vectors are not Strings and Strings are not Vectors, so that test just makes no sense.

Second, and more importantly, what are you really trying to do here? It looks like you want to put null if the Vector is empty, and put the Vector if it's not. But that makes no sense, and is not really a good approach. Just put the Vector, regardless of whether it's empty or not. Anything that's processing it will most likely be iterating over it, and you can iterate over an empty Vector just as you can a non-empty one--you loop body just never gets entered. Note, however, that if you put null, then the user of that result has to do an explicit check for null, so there's usually no good reason to do that.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 2686
Are you a bit bored Jeff ? Digging out 10 year old posts to answer.


Joanne
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Joanne Neal wrote:Are you a bit bored Jeff ? Digging out 10 year old posts to answer.


No digging was required -- another member apparently replied to the post, and then realized his mistake and edited away the answer. At some later time I noticed that and removed his accidental post. But for some time the thread would have been near the top of the "most recent" list.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 2686
That makes sense. I've noticed old threads near the top of the list before even if there was no recent post in them. Always wondered how that happened (but not enough to actually ask )
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3088
Ha! Never even noticed. And if there'd only been Joanne's comments without Paul's, I'd have been truly confused.
 
jQuery in Action, 2nd edition
 
subject: How to check if vector is null?
 
Threads others viewed
Can not solve my problem....
Need help! Request Headers!
How can I show part of my text in my template?
Question on cache control...
Check null value question?
IntelliJ Java IDE