• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check if the value of an object is assigned  RSS feed

 
Dex Hunter
Greenhorn
Posts: 6
C++ Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have initialized an array of objects, and how can I check whether a play is assigned with some values. For example, supposing thePlayer[2] is just initialized but without any value input to it how can I check if the value of thePlayer[2] is empty?

Thanks in advance!
 
Dex Hunter
Greenhorn
Posts: 6
C++ Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my test file, and all output is "No Problem", which confused me. So I was thinking maybe there is a different approach?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dex Hunter wrote:
This is my test file, and all output is "No Problem", which confused me. So I was thinking maybe there is a different approach?


You created an array for Zoo instances. You filled the array with Zoo instances, for all the elements. Why are you confused that the elements of the array are not null?

Henry
 
Dex Hunter
Greenhorn
Posts: 6
C++ Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Dex Hunter wrote:
This is my test file, and all output is "No Problem", which confused me. So I was thinking maybe there is a different approach?


You created an array for Zoo instances. You filled the array with Zoo instances, for all the elements. Why are you confused that the elements of the array are not null?

Henry



But I didn't assign any value to any of Zoo, so I am assuming all instances of Zoo are empty. By empty I mean there is no value inside and if I assign like , then Zoo is not empty. So I was wondering if there is any method to check whether an obejct is empty?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Empty" is not an automatic feature of Java objects. But apparently for your Zoo objects you're allowed to pass null in the constructor and that counts as "empty" in your design. Whether you can tell whether a Zoo object is empty according to that definition depends on whether the Zoo class provides a method to look at whatever is passed in the constructor.

So you'd have to look at the code for the Zoo object to see if that's the case. If there is such a method then you'd have to call that and see whether it returns null, rather than comparing the Zoo reference itself to null. Or maybe the Zoo object has an isEmpty method... anyway, look at the code for Zoo to see what's available.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!