The Infamous NullPointerException

Photo by sarah b on Unsplash

The Infamous NullPointerException

Ah, null errors, I'm sure all developers have stumbled upon such an error at least a few times during their careers. Well, I'm no exception, which is why I have dedicated this article to my first experience in Android application development using Java.

Background

For my assignment, my group was involved in developing a shopping application for the Android platform, and I was responsible for implementing a payment system that allows users to purchase goods with their credit or debit cards. With this, I ended up choosing Stripe as my payment service provider simply due to the fact that they seem to have well-written documentation for their products, and I would prefer not to waste too much time digging for implementation steps online with a tight schedule on hand.

At first glance, the documentation made the whole process seem to be a quick walk in the park. Though, I quickly realized that was not the case as I have to set up an intermediary web server that handles requests from mobile clients as well as responses from Stripe's API gateway, which was not part of the assignment requirements, and hence, extra work.

The Error

At the expense of some caffeine and my sanity, I finally managed to get a web server with RESTful API up and running on the public internet, free of charge, by taking advantage of Microsoft Azure for Students, which gave me about $100 in credits to spend on their services.

Though, during the implementation of functions that were used to communicate with the web server, I ran into the notorious Null error or NullPointerException in Java, as seen below.

The thing is, Null errors are usually kind of useless...in a sense that such errors do not provide any useful information to help developers to debug and ascertain the cause of the issue, other than saying, "Hey! This variable has nothing." So, you can't just copy and paste some code from StackOverflow and expect it to work!

Solution

To debug such an issue, developers would have to understand the context of the code, as in my case, the error occurs specifically within functions that expect a response back from the web server. For instance, when the mobile client sends an HTTP request to the web server in an attempt to obtain the onboarding status of a seller on the platform, it expects a response from the web server, which takes time.

That is the root cause of my Null errors as the application did not take into account the time required for the webserver to process and send back a response. As a result, the function proceeded to write off the response as nothing or null.

Hence, with the problem identified, I quickly implemented asynchronous callbacks that allow the functions to wait for the server's response before executing the next line of code. Problem solved!

Conclusion

That basically sums up one of my debugging journeys! Being a developer is not as easy as how it is portrayed by the general public, or at least not as easy as simply copying and pasting code you found online and expecting stuff to work out magically.