一下程序, 我输入1和3的时候, 发生异常,输出结果为:
input number: 1
input number: 3
write exception
原因也查到了,就是因为我在class data加了一个happy pp = new happy(aa, bb);如果去掉就正常了。但我想知道在保留这个语句的前提下怎样可以不发生异常呢?请大侠们指教! import java.util.*; import java.io.*;
public class TestDat2{
int aa;
double bb;
data[] cards;
public TestDat2(){
try{
FileInputStream fi = new FileInputStream("cards.dat");
ObjectInputStream input = new ObjectInputStream(fi);
cards = (data[])(input.readObject());
}
catch(IOException ex){
cards = new data[1];
}
catch(ClassNotFoundException a){}
}
public static void main(String [] args){
TestDat2 td = new TestDat2();
while (true){
Scanner kb = new Scanner(System.in);
System.out.print("input number: ");
int num = kb.nextInt();
if (num == 1)
td.add();
if (num == 2)
td.display();
if (num == 3)
td.save();
if (num == 4)
break;
}
}
public void add(){ //1
aa = 1;
bb = 2.2;
data da = new data(aa, bb);
cards[0] = da;
}
public void display(){ //2
System.out.println(cards[0]);
}
public void save(){//3
try{
FileOutputStream fo = new FileOutputStream("cards.dat");
ObjectOutputStream output = new ObjectOutputStream(fo);
output.writeObject(cards);
System.out.println("write succeed");//test
}
catch(IOException ex){
System.out.println("write exception");//test
}
}
}
import java.util.*; import java.io.*;
public class data implements Serializable {
int aa;
double bb;
happy pp = new happy(aa, bb);
public data(int aa, double bb){
this.aa = aa;
this.bb = bb;
}
public String toString(){
String info = "aa: " + aa + "\tbb" + bb;
return info;
}
}
public class happy{
int nn;
double mm;
public happy(int nn, double mm){
this.nn = nn;
this.mm = mm;
}
}
input number: 1
input number: 3
write exception
原因也查到了,就是因为我在class data加了一个happy pp = new happy(aa, bb);如果去掉就正常了。但我想知道在保留这个语句的前提下怎样可以不发生异常呢?请大侠们指教! import java.util.*; import java.io.*;
public class TestDat2{
int aa;
double bb;
data[] cards;
public TestDat2(){
try{
FileInputStream fi = new FileInputStream("cards.dat");
ObjectInputStream input = new ObjectInputStream(fi);
cards = (data[])(input.readObject());
}
catch(IOException ex){
cards = new data[1];
}
catch(ClassNotFoundException a){}
}
public static void main(String [] args){
TestDat2 td = new TestDat2();
while (true){
Scanner kb = new Scanner(System.in);
System.out.print("input number: ");
int num = kb.nextInt();
if (num == 1)
td.add();
if (num == 2)
td.display();
if (num == 3)
td.save();
if (num == 4)
break;
}
}
public void add(){ //1
aa = 1;
bb = 2.2;
data da = new data(aa, bb);
cards[0] = da;
}
public void display(){ //2
System.out.println(cards[0]);
}
public void save(){//3
try{
FileOutputStream fo = new FileOutputStream("cards.dat");
ObjectOutputStream output = new ObjectOutputStream(fo);
output.writeObject(cards);
System.out.println("write succeed");//test
}
catch(IOException ex){
System.out.println("write exception");//test
}
}
}
import java.util.*; import java.io.*;
public class data implements Serializable {
int aa;
double bb;
happy pp = new happy(aa, bb);
public data(int aa, double bb){
this.aa = aa;
this.bb = bb;
}
public String toString(){
String info = "aa: " + aa + "\tbb" + bb;
return info;
}
}
public class happy{
int nn;
double mm;
public happy(int nn, double mm){
this.nn = nn;
this.mm = mm;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货