【发布时间】:2015-07-01 17:54:41
【问题描述】:
我收到一个运行时错误,上面写着:线程“主”java.lang.ClassCastException 中的异常:lab07.Loan 无法转换为 [Llab07.Loan; 在 lab07.TestLoanClass.main(TestLoanClass.java:27)。
错误可能出在可序列化类 Loan 中,但请先看看这个。因为如果我避免使用数组,一切都会很好。我正在使用 Eclipse IDE。
public class TestLoanClass {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
Loan[] loan = new Loan[5];
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream("Exercise19_06.dat"));
for (int i = 0; i < 5; i++) {
loan[i] = new Loan(1.5 * i,i * 2,10000 * i);
output.writeObject(loan[i]);
}
output.close();
ObjectInputStream input = new ObjectInputStream(new FileInputStream(
"Exercise19_06.dat"));
//Conversion below is perfectly legal to type, but I get an error when
//I run the program. Appearently it has something with Object being
//cast to a Loan. Shouldn't the error show up while writing the code?
Loan[] loanRead = (Loan[]) (input.readObject());
//However, it work's just fine if I avoid using an array
for (int j = 0; j < 5; j++) {
System.out
.printf("The loan was created on %s\n"
+ "The monthly payment is %.2f\nThe total payment is %.2f\n",
loanRead[j].getLoanDate().toString(),
loanRead[j].getMonthlyPayment(),
loanRead[j].getTotalPayment());
}
input.close();
}
}
【问题讨论】:
-
请用编程语言标记您的问题。
标签: arrays classcastexception serializable