Tuesday, August 11, 2015

Simplifying 'Null'- The Curious Case of 'Null Pointer' - Part 1

Ever wondered about something in life which leads us to nothing - the usual superficial stuff around, those junk old thoughts which makes you feel blue all the time?

That's exactly what the null pointer do in programming languages - it refers to nothing and when you try to access something which is referred by a null pointer it makes your application crash & unstable.

And don't confuse 'null pointer' with simply 'null value' that we come across in databases. 'Null value' means unknown, something which does not exist at this very point of the time. It might be known or not known, there is no surety about that. It might happen that the null value may reveal its nature in future or it may remain unknown forever. But 'Null Pointer' leads to disguise - it makes you believe that, things are there which actually do no exists. While 'Null values' may lead to curiosity, 'Null Pointer' leads to illusion of something which don't exist.

So enough of the pointer & life related philosophy, lets get back to the real thing.

Null pointer in programming languages does not refer to a valid existing object. So you may be wondering then why on earth they exist and what their practical uses are! This article will be concluded in two parts as the length and breadth of the null pointer’s scope cannot be covered in a single write up.

Just take the example of traversing a link list – err but now what is a link list?
I assume everyone from IT background are aware of that but for the non-IT readers – let’s dig a little bit into link list for the benefit of everyone.

Link list is one of the data structure types which define a way to store data. Consider link list like delhi metro train with the first coach which is reserved for ladies :D <3 is the main compartment of the train. And the driver of the Metro train is obviously in this compartment. Consider this driver as a programmer who always stores the data in the first very block of the link list(first coach of the metro) which is the starting point and connects to another block(coach) which is connected by a pointer(connecting hook) which simply connects to another block(coach) and so on. Now we usually have 4 coach metro train along with the 6 coach or even 8 coach metro train. So how does adding new coaches to the metro err new node to a link list is done? By malloc – which allocates the memory for all new nodes that are to be added.

Now hopefully relating Delhi metro with link list and pointers would have made understanding the situation bit simpler.

So when we traverse the link list, each node is referred by a pointer, the data item of the node is being accessed and after that it points to next node with the help of pointer. Now after traversing all nodes, guess what the pointer from the last node will be pointing to? It will reach the null pointer that means there are no further meaningful nodes and the list has ended. Try to relate null pointer with the connecting hook of the last metro coach which connects to nothing, though a new node/ a new coach can always be added to link list and metro rail respectively!

Did I just heard NullPointerException? Stay tuned for the next articles containing example codes, about nullpointerexception, and much more.