so lets finish up our discussion about immutable objects before we go on to the
challenge I just wanna make it clear to the question what exactly is wrong with
allowing a class to be extended so have a look at the declaration of our location
map, one line 8 our locations map in main now the map is declared using generic so that accepts
an integer key and a value of type location but if location is extended then
instances of the new class could also be used so this is generally not a problem
in fact it's actually incredibly useful but if we use the location type for the
key instead of the value we wanted to ensure that only completely
immutable objects could be used as the keys then we have to prevent
subclasses over which we have got no control from being used so anyone who wants to use
the functionality of the locations class once we prevent them from extending it
can still to do so using composition they create methods that then call the
locations class methods from an instance of a location that could still do almost
everything that's subclassing would have allowed them to do except use their own
class where location object was expected and most of the time you do not want
your classes to be immutable a bank account class where the balanced where
the changes would be of little use no matter how we love to check on such an
account but even if we don't want an immutable class you can use these
techniques in various combinations to make your classes certainly more robust
than if you didn't do anything at all now the challenge is that there's still a
problem with location it's possible to crash the program as a result the
problem is with the location class itself so don't waste time trying to
crash it by entering invalid input there is also no consistency checking in the
main method so if we created exits that refer to a location say for
example location 6 that doesn't exist in our locations map then program will also
crash but this challenge is about finding a problem with the location
class, so here is your challenge and the challenges to work out what is wrong
with the location constructor that would allow the program to compile and crash
at runtime so when you identify the problem modify the code to fix it so I just i’ll just say that again
your challenge here is to work out what is wrong with the location
constructor that you can see on screen line 14 that will allow the program to
compile but crush on run time when you identify the problem modify the code to fix it
and so I’ll pause the video now and when you come back I'll show you what the
problem was and how to fix it
ok so did you figure it out what the problem was well the problem is that the
constructi crash with a NullPointerException at line 17 if
null is passed instead of the map and to see this lets modify the method, i’ll go back to the
main method, i’ll closed down the run window and will change temp exit
to null we're now passing a null map so if we run, we get a crash 10 NullPointer
Exception and we are getting a crash on line 17 but the program crashes as a result
so you might look at the code and think well it's probably reasonable
on line 22 to have a null passed because there aren't any exits because
it's a special sort of location that were using to indicate that the program has quit but
obviously having crashes is a bad thing so the solution, go back to the location
constructor is to test the constructors argument and don't try to
initialize the new map if null is passed for the exits
so consider what I mean there we put a test here we put if exits not equal to null so if
there is some valid map from that was pass to this method and we're going to
initialize it with that map else we put this…
with an empty constructor meaning it's going to create a new HashMap but obviously
because there is no valid data pass to this constructor
and everything else can remain the same the code on line 22 will still work because
it's got a valid map because it has been initialised correctly so to confirm that we
run that again we are no longer getting an error and we can still navigate
and do things as you have have normally done so that’s it I hope you got a lot out of immutability and out of
this adventure challenges and I'll see you in the next video