• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check if vector is null?  RSS feed

 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elahe,
Try Vector.isEmpty()
That kind of information is always available in the
Java docs.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but if the Vector is null, you will run in problems. Try this:

Hope that helps
Rene
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you a bit bored Jeff ? Digging out 10 year old posts to answer.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha! Never even noticed. And if there'd only been Joanne's comments without Paul's, I'd have been truly confused.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!