File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Serialized Objects - Reflection Inquiry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Serialized Objects - Reflection Inquiry" Watch "Serialized Objects - Reflection Inquiry" New topic

Serialized Objects - Reflection Inquiry

Matthew Deichsel

Joined: Mar 29, 2011
Posts: 3

In the class, it intializes a collection:

List<Object> list = new LinkedList<Object>;

When the message is written before sending across the socket/connection, the ObjectOutputStream have no issues but on other side of the connection, it throws the exception after it calls readObject(ObjectInputStream).

I went through the debug process and realized that the 'list' is null even it already got initialized as a class variable. I had to create a temporary List collection before collecting the data from ObjectInputStream then have it assigned to 'list'. That works fine.

I do not understand why the 'list' is null. Is it possible that Reflection, while running OutInputStream, access into the method, readObject(), without any data stored into the memory like 'list'?

For instance:

So, this will fix the exception....

Any idea why they skip the intialization part?

Thank you
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

What exception does it throw?

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Matthew Deichsel

Joined: Mar 29, 2011
Posts: 3
Wouter Oet wrote:What exception does it throw?

NullPointerException because the class variable, 'list' is null even it already got initialized in that class (private List<Object> list = new LinkedList<Object>();>

cause by: java.lang.NullPointerException
at <package name>.ClassName.readObject(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(

After doing some research in my own, I figured it out. Reflection do not need the accessor of the class object, just a string of class' location without knowing the names of the classes, methods etc. at compile time. Now that makes sense that Reflection skips all class variable and all class variables are null no matter if they all already instantiated, implemented or initialized outside the specific method implementation where reflection is accessing into.
I agree. Here's the link:
subject: Serialized Objects - Reflection Inquiry
It's not a secret anymore!