2009-10-17

Java Proxy instance throws a NullPointerException

This is something I just found out, and I figured I'd blog about it for those that might come across this in the future.

I'm writing some code in a project (JZBot specifically, http://jzbot.googlecode.com) that uses java.lang.reflect.Proxy. At one point, I was calling a method on a proxy instance, and it was throwing a NullPointerException. The weird thing about this was that the NullPointerException was being thrown from within the proxy instance itself, not from the InvocationHandler that it is supposed to call. Other methods on the proxy worked, indicating that there wasn't some error with the invocation handler somehow being set to null.

Eventually, I found the problem: I had declared the method on the interface that the proxy implemented to return a boolean, but the invocation handler was returning null. The auto-unboxing that was being done by the proxy instance internally was messing things up. I changed the method's return type to void (which is what it should have been), and everything worked.

This is yet another example of where to watch out for autoboxing and unboxing. Admittedly, it would be more helpful if the NPE had a message stating something like, for example, "null can't be unboxed to a boolean", which would most likely have saved me quite a bit of time. Oh well.

15 comments:

Ran said...

Thanks for this, I found it very helpful! (In my case, the method was hashCode, so the conversion was from Integer to int rather than Boolean to boolean, but, same idea.)

Vince said...

Thanks for posting this!

Unknown said...

Thanks for posting this! I had the same problem and was wondering and wondering... ;-)

siiilon said...

Thank you :)

Unknown said...

Thanks for posting this blog entry. I had a similar problem that I was banging my head against. I couldn't change the method signature to void, in my case, but returning a proper default value (0 or false) instead of null fixed the problem.

Rathinam said...

Thank you for your sharing and I want to more updates for my research..
Appium Training in Chennai
Appium Certification in Chennai
Pega Training in Chennai
Tableau Training in Chennai
Advanced Excel Training in Chennai
Spark Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
Power BI Training in Chennai
Corporate Training in Chennai
Placement Training in Chennai

Sophia Lucas said...

Good post. I learn something totally new and challenging on blog IS stumble upon everyday. It’s always helpful to read through articles from other authors and use a little something from their sites. Turkey tourism.You can check all Turkey tourism Guidelines Turkey through Turkey e-Visa website.

Ava said...

This is a very useful article… India etv visa fee depends on your visa type and your nationality

James said...

I appreciate your work... Thanks for sharing... There is good news for international travelers. The Indian government restored the Indian tourism sector and also India tourist visa for the long term, which means I have more time to explore India.

Lillian said...

Hii friends, I wanted to write a little Info related to Visa. Are you interested in traveling to any country? Yes, you can apply for a visa online. You can fill out your visa application form online within 5 to 10 minutes via our Visacent website. We offer visas to citizens of over 190 countries. You can read more info about visas via our website.

alex said...

"The quality of your writing and the logical flow of your ideas are remarkable. This post is a fantastic resource, and your passion for the subject shines through." Do you know  Egypt Introduces New Residency Fees, now need to furnish proof of converting all relevant fees from U.S. dollars or their equivalent foreign currencies into Egyptian pounds, as per the new regulation.

Josh Pompter said...

Your writing is truly remarkable! This paragraph is a shining example of your talent, and I'm eagerly looking forward to more posts of this caliber. Stay informed with the most recent Japan vaccine guidelines.Japan had introduced strict vaccine requirements for travelers.

alex said...

Your blog entry resonated deeply with my own experiences, highlighting the depth of human connections and the meaningful impact of simple acts of kindness."This is information I believe is worth sharing."
"Turkey entry requirements vary by nationality. It's essential to research and comply with the specific visa and documentation regulations before traveling. Adhering to Turkey's entry requirements ensures a smooth and enjoyable visit to this beautiful country."

steve said...

You're knocking it out of the park with your reporting, and I'm continually amazed by your professionalism and expertise. Keep up the excellent work, and know that I'm here to support you.
The India tourist visa fee for USA citizens varies depending on the duration of the visa and the type of entry required. As of the latest information available, the fee structure typically includes different tiers for various visa durations, such as 10 years, 5 years, or shorter stays.

Phineas said...

Your insights are valuable and easy to understand, making complex subjects clear and engaging. It feels like learning from a knowledgeable friend. I'm saving your content for future reference and can't wait for more great work! Apply for your Cameroon visa application easily with quick access to the application form. Complete it, attach the necessary documents, and submit. The simple interface and clear instructions make the process smooth, helping you get ready for your trip to Cameroon quickly and efficiently.