Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialized Objects - Reflection Inquiry

 
Matthew Deichsel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:

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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exception does it throw?
 
Matthew Deichsel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(ClassName.java:#)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:39)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic