| Author |
TreeMap and null values
|
Maureen Charlton
Ranch Hand
Joined: Oct 04, 2004
Posts: 218
|
|
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
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
Java API Documentation
The Java Tutorial
|
 |
Maureen Charlton
Ranch Hand
Joined: Oct 04, 2004
Posts: 218
|
|
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).
|
 |
 |
|
|
subject: TreeMap and null values
|
|
|