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.

21 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... ;-)

nasreen basu said...

interesting piece of information, I had come to know about your web-page from my friend pramod, jaipur,i have read atleast eight posts of yours by now, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regards Guidewire training in hyderabad

nasreen basu said...

interesting piece of information, I had come to know about your web-page from my friend pramod, jaipur,i have read atleast eight posts of yours by now, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regards Guidewire training in hyderabad

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.

for ict 99 said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

nasreen basu said...

Thank you. Your blog was very helpful and efficient For Me,Thanks for Sharing the information Regards
Guidewire training in hyderabad

David Smith said...

Great Article

Project Centers in Chennai
Final Year Projects for CSE

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

senthilpraveen said...

Blog was useful and informative.
Java Course in Chennai
Python Course 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.

Alexander Jacob said...

This is a wonderful inspiring article. I am practically satisfied with your great work. You have really put together extremely helpful data. Keep it up... Continue... The current process is hassle free, and you can read all kenya entry requirements through the Kenya Evisa website, and it only takes 5-10 minutes to complete the Kenya visa application online It will take

Unknown said...

tiktok jeton hilesi
tiktok jeton hilesi
referans kimliği nedir
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
yurtdışı kargo

Ava said...

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

Sophia said...

Thanks for all you do. I like the website themes and layout, you are posting amazing blogs.... What is the e-visa Turkey? Visa Turkish website, You can check here about Turkey e-Visa or Sticker Visa Application information. And you can read all guidelines

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.

Sophia said...

You wrote a very interesting and informative post. Thanks for taking the time to write it. Very useful for readers interested in travel information. Do you want to apply for a Turkish e visa, we provide Turkey e Visa information. You can visit our visa to Turkey page. Read more about e visa of Turkey.


Nathan said...

I like your posts since I am a new learner, and I find them very helpful. Thanks. Travelers have a question: can US citizen travel to India? Yes! An Indian tourist visa can be obtained in the form of a 30 day, a year, or five-year visa, which is easy to obtain and trackable online.

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.