Reputation: 2137
I am trying to serialize an ArrayList<Prescription>
using an ObjectOutputStream
Here is the Prescription
class:
import java.io.Serializable;
import java.util.Calendar;
public class Prescription implements Serializable {
private static final long serialVersionUID = 4432845389029948144L;
private String name;
private String dosage;
private int originalQuantity = 0;
private int quantityRemaining = 0;
private String prescribingPharmacy;
private long dateStarted = 0;
private boolean taken_AM = false;
private boolean taken_Noon = false;
private boolean taken_PM = false;
private boolean taken_Mon = false;
private boolean taken_Tue = false;
private boolean taken_Wed = false;
private boolean taken_Thu = false;
private boolean taken_Fri = false;
private boolean taken_Sat = false;
private boolean taken_Sun = false;
public Prescription(){
this.name = "Unknown";
}
public Prescription(String name, String dosage, int quantity, String pharmacy){
this.name = name;
this.dosage = dosage;
this.originalQuantity = quantity;
this.quantityRemaining = quantity;
this.prescribingPharmacy = pharmacy;
this.dateStarted = Calendar.getInstance().getTimeInMillis();
}
public void setTaken(boolean AM, boolean Noon, boolean PM){
this.taken_AM = AM;
this.taken_Noon = Noon;
this.taken_PM = PM;
}
public void setTaken(boolean Mon, boolean Tue, boolean Wed, boolean Thu,
boolean Fri, boolean Sat, boolean Sun){
this.taken_Mon = Mon;
this.taken_Tue = Tue;
this.taken_Wed = Wed;
this.taken_Thu = Thu;
this.taken_Fri = Fri;
this.taken_Sat = Sat;
this.taken_Sun = Sun;
}
public String getName(){
return this.name;
}
}
Yet, when I try to do this, I get a NotSerializableException
on the Prescription
class. This doesn't make any sense to me, as I am only using primitive data types and String
.
Here is the function I am using to do the serialization:
public boolean saveToFile(){
try {
FileOutputStream fos = this.context.openFileOutput(LIST_SAVE_NAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.pList);
oos.close();
} catch(FileNotFoundException e){
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
Upvotes: 2
Views: 2619
Reputation: 1289
The code:
public static void main(String[] args) {
try {
List<Prescription> d = new ArrayList<Prescription>();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("C:\\temp\\Prescription.dat")));
d.add(new Prescription());
oos.writeObject(d);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
works without any exceptions with the class you posted, so there's something you're not telling us :-)
Upvotes: 5