• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialized Objects - Reflection Inquiry

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exception does it throw?
 
Matthew Deichsel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic