java.io.NotSerializableException

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
nse.JPG


Code:
public class Implementation extends java.rmi.server.UnicastRemoteObject implements VotingInterface {
	private Election[] electionList = new Election[99];
	private int numberOfElections = 0;
	
	public Implementation() throws java.rmi.RemoteException {
		super();
	}
	
	public int createElection(String electionName, String[] candidates) throws java.rmi.RemoteException {
		numberOfElections += 1;
		electionList[numberOfElections] = new Election(candidates, electionName, numberOfElections);
		return numberOfElections;
	}
}

Code:
public class Election {
	private Candidate[] candidates;
	private String electionName;
	private int id;
	private boolean closed;
	
	public Election(String[] candidates, String electionName, int id) {
		this.electionName = electionName;
		this.id = id;
		this.candidates = new Candidate[candidates.length];
		for (int i = 0; i < candidates.length; i++) {
			this.candidates[i] = new Candidate(candidates[i]);
		}
	}
	
	public Candidate[] getResults() {
		return candidates;
	}
	
	public void closeElection() {
		closed = true;
	}
	
	public void vote(int id) {
		if (closed == false) {
		}
	}
}

Code:
public class Candidate {
	private int numberOfVotes;
	private String name;
	
	public Candidate(String name) {
		this.name = name;
	}
	
	public int getVotes() {
		return numberOfVotes;
	}
	
	public void vote() {
		numberOfVotes += 1;
	}
}

Help me understand this one guys. I understand that java can't serialize this class, but why is it trying to? The error occurs when I call the createElection method.
 
Say what? :p

Implementing the serializable interface in candidate fixes the problem but can you please explain what exactly is going on in this situation?
 
Back
Top Bottom