class QStorage{
Queue q = new PriorityQueue(5);
public Queue readQ(){
try{
FileInputStream fin = new FileInputStream("/home/requestQ.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
q = (Queue)ois.readObject();
}catch(Exception e){}
return q;
}
}
而在另一个类我打电话readq在上述类的()方法将文件的内容排队 存储到一些其他的队列。
class MyQ{
public static void main(String args[]){
QueueStorage qs = new QueueStorage();
Queue myQ = new PriorityQueue(5);
myQ = qs.readQ();
//...some other stuffs goes here
}
}
这里如果已经有队列的某些条目存储在文件中,readQ()会正确返回这些条目。但如果没有存储在文件中的条目(我的意思是空队列存储) 那么这种方法给nullPointerException和我的程序停止工作。
注:我有要求,我应该检查存储的内容,而不知道它是否为空或不是 。
请帮忙。谢谢。
2013-10-24
Winn
+0
你忘了实现'Serializable' –
+1
那么,我已经实现了'Serializable',忘了在这里显示。仍然得到相同的错误。 –
+0
如果在空文件的情况下问题是NPE,请将其捕获并返回一个“null”对象。 –