?
Solved

writeObject, too large and caused stack overflow error

Posted on 2003-03-28
5
Medium Priority
?
552 Views
Last Modified: 2012-08-13
Hi folks,

   I'm trying to write an object to a file. This object is a class, which has some hashset and vector in it.
   When I test my program, I found that: When the size of the object is small, around 10K or so, everything worked just fine. But when the size is very large, it threw out a stack overflow error. Does anybody know how to resolve this problem? Thanks.

SR
0
Comment
Question by:summer_soccer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Expert Comment

by:makas
ID: 8243699
Post the stack trace, might be other problems not relating to the object size.
0
 
LVL 4

Expert Comment

by:kokchoon78
ID: 8252722
yup, post the error stack trace....
0
 

Author Comment

by:summer_soccer
ID: 8254581
Ok, what's I'm doing is to build a tree representation of a real Ethernet. It's a little bit hard to describe, but I'll give a try. The applicaton queries each of the switch in the Ethernet using SNMP protocol, and get the idea what host or what other switch is on which port of the this switch, then it use a TreeNode class instance to represent the switch.Inside the TreeNode class, there is a hashtable, which is a host/switch -> out port mapping. After the initial discovery, the application should have built up a tree, in which each node represents a switch in the Ethernet. These nodes have a Hashtable field called nodeToPort, which contains the mappings of hosts/switchs -> out ports.

Since this kind of topology discovery is very time consuming, what I need to do is to store the whole topology into a file. Later on I can read the topology back without do the discovery again. The problem: I can write the nodeToPort Hashtable now, but when I try to read it back, a null pointer exception is thrown out. I've spent days on it  trying to figure out what's wrong, but didn't succeed.

Part of the code and the stack trace:

public class Topology implements java.io.Serializable {
public NetworkSnapshot net; // another class
Set nodes;
private Set macsNamed;
private TreeNode root;
Set unmappedHosts;
String rootBridgeName = null;
public boolean useBridgesForTopology=true;
public boolean unifyBridgePorts = true;
public boolean useBridgeToBridgeForwarding=true;
int emptyBridges=0;
int mappedUsingNamesOnly=0;
int unsharedMappings=0;
int directMappingConflicts=0;
int indeterminantMappings=0;
int abandonedRootSearch=0;

.........

public void writeTop(ObjectOutputStream outToFile1, ObjectOutputStream outToFile2) {
try {
this.net.save(outToFile1); // save the network snapshot to file
outToFile1.writeObject(this.nodes); // save the topology nodes to file
outToFile1.flush();
outToFile1.writeObject(this.macsNamed); // save the named mac to file
outToFile1.flush();
outToFile1.writeObject(this.internalHosts);
outToFile1.flush();
outToFile1.writeObject(this.unmappedHosts);
outToFile1.flush();
outToFile2.writeObject(this.root);
outToFile2.flush();
outToFile2.writeObject(this.rootBridgeName);
outToFile2.flush();
outToFile2.writeBoolean(this.useBridgesForTopology);
outToFile2.flush();
outToFile2.writeBoolean(this.unifyBridgePorts);
outToFile2.flush();
outToFile2.writeBoolean(this.useBridgeToBridgeForwarding);
outToFile2.writeInt(this.emptyBridges);
outToFile2.writeInt(this.mappedUsingNamesOnly);
outToFile2.writeInt(this.unsharedMappings);
outToFile2.writeInt(this.directMappingConflicts);
outToFile2.writeInt(this.indeterminantMappings);
outToFile2.writeInt(this.abandonedRootSearch);
outToFile2.flush();
}
catch(Exception e) {
System.out.println("When trying to save topology into file, IO Exception: "+e);
}
}

public Topology(ObjectInputStream inFromFile1, ObjectInputStream inFromFile2) {
try {
this.net = new NetworkSnapshot(inFromFile1);
this.nodes = (Set) inFromFile1.readObject();
this.macsNamed = (Set) inFromFile1.readObject();
this.internalHosts = (Set) inFromFile1.readObject();
this.unmappedHosts = (Set) inFromFile1.readObject();

this.root = (TreeNode) inFromFile2.readObject();                                       // This line is causing a null pointer exception eventually

this.rootBridgeName = (String) inFromFile2.readObject();
this.useBridgesForTopology = inFromFile2.readBoolean();
this.unifyBridgePorts = inFromFile2.readBoolean();
this.useBridgeToBridgeForwarding = inFromFile2.readBoolean();
this.emptyBridges = inFromFile2.readInt();
this.mappedUsingNamesOnly = inFromFile2.readInt();
this.unsharedMappings = inFromFile2.readInt();
this.directMappingConflicts = inFromFile2.readInt();
this.indeterminantMappings = inFromFile2.readInt();
this.abandonedRootSearch = inFromFile2.readInt();
}
catch(Exception e) {
System.out.println("When trying to read from file, IO Exception: "+e);
}
}

Here TreeNode is another class, shown as follows:

class TreeNode implements java.io.Serializable {
Switch node;
int ports;
int rootPort;
TreeNode[] nextHop;
int[] hopsPort;
Hashtable nodeToPort;
Hashtable macToPort;

.........


public boolean equals(Object o){
if(!(o instanceof TreeNode))
return false;
return node.equals(((TreeNode)o).node);
}

/**
* hash is dependent only on nodes being identical
*/
public int hashCode(){
return node.hashCode();       // line 147
}

......
}


When trying to read from file, IO Exception: java.lang.NullPointerException
java.lang.NullPointerException
at remulac.BridgeColl.TreeNode.hashCode(TreeNode.java:147)
at java.util.Hashtable.put(Hashtable.java:394)
at java.util.Hashtable.readObject(Hashtable.java:838)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:824)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1746) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1603)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1271)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:18
45)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1603)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1271)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:18
45)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1603)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1271)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:18
45)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at java.util.Hashtable.readObject(Hashtable.java:836)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)

at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:824
)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1746)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:18
45)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1603)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1271)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:18
45)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at remulac.BridgeColl.Topology.<init>(Topology.java:1504)
at remulac.BridgeColl.BridgeCollector.<init>(BridgeCollector.java:40)
at remulac.BridgeColl.BridgeCollector.main(BridgeCollector.java:221)



0
 
LVL 4

Accepted Solution

by:
kokchoon78 earned 80 total points
ID: 8258020
Hi,

  I think that is no problem on the readObject() method. According to the stack trace, the NullPointerException happened on :

public int hashCode(){
return node.hashCode();       // line 147
}

that means node is null.
Just add a if statement to filter out the exception:

if( node != null )
{
   return( node.hasCode() );
}
else
{
   return 0;
}

And, remember to close all the ObjectOutputStream and ObjectInputStream instances.


regards,
Kok Choon.

0
 

Expert Comment

by:makas
ID: 8258203
It is not safe to assume that the field "node" can be null. Ensure that this behaviour is acceptable, if it is, you would need to check for null as well in your "equals" method.

If "node" cannot be null, you may need to trace the cause of it and fix that instead.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question