Is there anything wrong with declaring collection transient? transient Map<String, Car> cars = new HashMap<>()
is declared in Owner
instance that is serialized, but the Car
class is not serialized.
When program runs for the first time Owner
instance it creates Car
and insert it into collection Cars
, however when running program for second time, Owner
is deserialized, it correctly creates Car
instance but when adding to collection cars.put(key, object)
it causes NullPointerException
. Also only when running after deserialization cars.containsKey(regNumIn)
causes exception instead of giving true or false. It seems that on second run after Owner
is recreated the new hashMap
is created.
Does it have to do anything with hasCode() and equals()? I havent declared those, and if they are automatically declared by Netbeans IDE, the program doesnt work at all.