• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeMap and null values  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a TreeMap as a Collection that sorts the keys into order automatically and may allow null values.

However I don't wish to have these null values allowed.

So my code in my test program is:



And my code in my VehicleDB (where I have declared the TreeMap) is:



And my print out to screen is as expected:

Test VehicleDB: Null values are allowed but should not be stored
================================================
NV52SSD reg is not Null so it will be allowed
qp43SSX reg is not Null so it will be allowed
ZZ43SSX reg is not Null so it will be allowed
null: reg is Null so it will NOT be allowed

Finished!!!

My question is, is this the best way to do it?

Thanking you in advance.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find the following line of code a little suspicious:

I think you have a problem with understanding what null is. When the documentation says that null is (or is not) allowed, it is referring to a null reference. Even if you were to use a Collection that disallowed null values, the String "null" would still be allowed because this is NOT a null value; it's a String with the characters n-u-l-l.

So my question is do you really mean the String "null" or do you mean to use a null reference instead? If you use the keyword null, the check in the store() method will be a little simpler. You would be able to change

to

This also means you will need to change the constructor call for the VehicleRecord object to use a null reference instead of the String "null".

HTH

Layne
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne

Many thanks for your reply. Much appreciated.

I used the following in the end (the String "null" was an error on my behalf been coding all day and got a little tired; sorry).



  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!