This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization of ValueObject which contains static variables  RSS feed

 
Nikita Rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Facing problems when serializing a class containing static ArrayList variable

In the Value Object class the following lines of code are present including a static block as shown below :

----
public class TestRC extends RemoteCommand{

final static String JNDIDestination = "getTestRC";

public static ArrayList accessorMethodsSequenceList = new ArrayList();

int ipClientId = 0;
int custId = 0;

public TestRC() {

}
public TestRC(int clientId, String user) {
//load();
this.ipClientId = clientId;
this.userId = user;

}



public String getJNDIDestination() {
return TestRC.JNDIDestination;
}



/**
* Get Client Id
*/
public int getIpClientId() {
return this.ipClientId;
}

/**
* Set Client Id
*/
public void setIpClientId(int clientId) {
this.ipClientId= clientId;
}
static{
try{
accessorMethodsSequenceList.add(TestRC.class.getMethod("getIpClientId", (Class[])null));
accessorMethodsSequenceList.add(TestRC.class.getMethod("getCustId", (Class[])null));

}
catch(NoSuchMethodException e){
e.printStackTrace();
}
}


public int getParamListSize() {
return TestRC.accessorMethodsSequenceList.size();
}
public Method getMethodAtIndex(int index) throws Exception{
if(index >= accessorMethodsSequenceList.size())
throw new Exception("Method index is out of bounds");
return (Method)accessorMethodsSequenceList.get(index);
}

/**
* Gets Client Id
*/
public int getCustId() {
return this.custId;
}

/**
* Sets Client Id
*/
public void setCustId(int custId) {
this.custId= custId;
}
}

public abstract class RemoteCommand implements RemoteCommandI {
String userId = null;

/** Creates new RemoteCommand */
public RemoteCommand() {

}

public String getUserId(){
return this.userId;
}

public void setUserId(String userId){
this.userId = userId;
}

}

public interface RemoteCommandI extends Serializable{

public String getJNDIDestination();


/**
* Returns Method instance that must be invoked to setInParameter
* for the Stored Procedure or prepared statement
*/
public Method getMethodAtIndex(int index) throws Exception;
/*
* Returns static parameterMethodArrayList size
*/
public int getParamListSize();

public String getUserId();

public void setUserId(String userId);
}



---

When the object is written and Read using the following lines of code it works fine

----------
FileOutputStream fos = new java.io.FileOutputStream("C:/test/1.txt");
ObjectOutputStream outLocal = new ObjectOutputStream( fos );
outLocal.writeObject(request);

FileInputStream fis = new java.io.FileInputStream("C:/test/1.txt");
ObjectInputStream inLocal = new ObjectInputStream( fis );

---------------

But when it is read using URLOpenConnection it does not work fine :
The below is the servlet class which is used to read the object :


public class TestObjectSerializationServlet extends HttpServlet
{
ServletContext ctx = null;

public void init(ServletConfig config) throws ServletException{
super.init(config);
// this.logger = Logger.getLogger(config.getInitParameter("logger.LoggerName"));
ctx = config.getServletContext();

}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.getWriter().println("Hello and Welcome to HTTP Proxy!");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ObjectOutputStream out = null;
try {

ObjectInputStream in = new ObjectInputStream(req.getInputStream());

Object messageIn = null;
try{
messageIn = in.readObject();
}catch(Error e){
e.printStackTrace();
}
in.close();
in = null;
out = new ObjectOutputStream(res.getOutputStream());
System.out.println((messageIn instanceof TestRC) +" messageIn instanceof TestRC");
if (messageIn instanceof TestRC) {
out.writeObject(messageIn);
} else {
out.writeObject("Hello Not Resolved");
}
} catch (IOException ex) {

ex.printStackTrace();
throw ex;
} catch (ClassNotFoundException ex) {

ex.printStackTrace();
throw new ServletException(ex);
} catch (Exception ex) {

ex.printStackTrace();
throw new ServletException(ex);
} finally {
out.flush();
out.close();
}
}
}

and the URLConnection is established as shown below :

--------------

URLConnection urlConnection = (URLConnection)getUrl().openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//Writing the outputstream which contains the TestRC object
ObjectOutputStream outLocal = new ObjectOutputStream( urlConnection.getOutputStream() );
outLocal.writeObject(request);

//Writing the outputstream which contains the TestRC object
ObjectInputStream inLocal = new ObjectInputStream( urlConnection.getInputStream() );
response = (Serializable) inLocal.readObject();

A java.lang.NullPointerException is thrown.

But when the static block in the TestRC object is removed it is working fine.Any ideas as why this is happening.
 
Poobhathy Kannan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no static or transient fields undergo default serialization.

That's why it's working perfectly when you remove the static block.
 
Nikita Rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas about how to overcome this?
Do let me know if there are any?
Thanks for the same.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the stack trace for the NPE, and try to show us how the line numbers in the stack trace correspond to your code -- i.e., where does the NPE happen? The static members won't be serialized, but they will be initialized when the class is loaded, so the previous answer isn't quite right.
 
Nikita Rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

The StackTrace :

<Mar 7, 2006 3:37:01 PM IST> <Error> <HTTP> <BEA-101020> <[ServletContext(id=393
92492,name=TestObjectWeb,context-path=/TestObjectWeb)] Servlet failed with Excep
tion
java.lang.NullPointerException
at fxm.proxy.TestObjectSerializationServlet.doPost(Ljavax.servlet.http.H
ttpServletRequest;Ljavax.servlet.http.HttpServletResponse ;) V(TestObjectSerializa
tionServlet.java:76)
at javax.servlet.http.HttpServlet.service(Ljavax.servlet.http.HttpServle
tRequest;Ljavax.servlet.http.HttpServletResponse ;) V(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(Ljavax.servlet.ServletRequest;
Ljavax.servlet.ServletResponse ;) V(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run
()Ljava.lang.Object;(ServletStubImpl.java:1006)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Ljavax.servle
t.ServletRequest;Ljavax.servlet.ServletResponse;Lweblogic.servlet.internal.Filte
rChainImpl ;) V(ServletStubImpl.java:419)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Ljavax.servle
t.ServletRequest;Ljavax.servlet.ServletResponse ;) V(ServletStubImpl.java:315)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActio
n.run()Ljava.lang.Object;(WebAppServletContext.java:6722)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Lweblogic.se
curity.subject.AbstractSubject;Ljava.security.PrivilegedAction ;) Ljava.lang.Objec
t;(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Lweblogic.security.ac
l.internal.AuthenticatedSubject;Lweblogic.security.acl.internal.AuthenticatedSub
ject;Ljava.security.PrivilegedAction ;) Ljava.lang.Object;(SecurityManager.java:12
1)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(Lweblogi
c.servlet.internal.ServletRequestImpl;Lweblogic.servlet.internal.ServletResponse
Impl ;) V(WebAppServletContext.java:3764)
at weblogic.servlet.internal.ServletRequestImpl.execute(Lweblogic.kernel
.ExecuteThread ;) V(ServletRequestImpl.java:2644)
at weblogic.kernel.ExecuteThread.execute(Lweblogic.kernel.ExecuteRequest
;) V(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run()V(ExecuteThread.java:178)
at java.lang.Thread.startThreadFromVM(Ljava.lang.Thread ;) V(Unknown Sourc
e)
>
As mentioned before
The above error occurs if the static block is present else it does not occur

Any inputs is greatly appreciated.
JCE: disabled smiles
[ March 07, 2006: Message edited by: Joe Ess ]
 
Nikita Rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

The actual reason for the above problem is because the Value Object Java files were compiled in two different environments. For the code in the Servlet side it was compiled in BEA weblogic environment and when accessing from the client side It was compiled in the Eclipse environment.

To overcome the problem I built the code in Weblogic environment and took the jar file to the eclipse environment and accessed the necessary method and it worked fine.

But still I would like to know the reason for the above issue and that too only for classes which contained the static block which contained a static ArrayList. To this arraylist the objects that were getting added were of type java.lang.reflect.Method.

Both the Eclipse and Weblogic were pointing to the same JDK Home

Thanks for the same
[ March 09, 2006: Message edited by: Nikita Rai ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!