java扫描程序上的NotSerializableException?

时间:2013-04-03 22:43:20

标签: java

我在解决一个notSeriazable时遇到问题,我的所有类实现都是可串行的,但总是给我一个错误。

 java.io.NotSerializableException: java.util.Scanner
 at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Assign3.main(Assign3.java:119)

扫描仪是一个正确的课程我没有意义..我认为扫描仪也应该被序列化?但我无法触及Scanner课程。

Bank b = new Bank(100.0);
    try{
        ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("try.txt"));

        save.writeObject(b);


        save.close();

        }catch(Exception ex){ex.printStackTrace();}



import java.io.Serializable;
import java.util.Scanner;



public class Bank implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private int Maxtransaction = 1000;
public Transaction[] transactions = new Transaction[Maxtransaction];
private int numTransactions = 0;
private double balance;
Scanner sn = new Scanner(System.in);

public Bank()
{
}

public Bank(double balance)
{
    this.balance = balance;
}

public double getBalance()
{
    return balance;
}

public void display()
{
    System.out.print("\nTRANSACTION RECORD:\n");
    for(int i = 0; i<numTransactions;i++)
        {
        System.out.print(transactions[i].toString()+"\n"); 
        }
    System.out.print("BALANCE : $"+getBalance()+"\n");

}

public boolean addTransaction(Category categories)
{
    if(numTransactions>Maxtransaction){return false;}
    else{
    String[] chooseaccountletter = {"c","d"};
    String chooseaccount;
    boolean erroraccount = true;

    System.out.print("\n");
    do{
    System.out.print("Enter a c for credit or d for debit: ");
        chooseaccount = sn.nextLine();
        for(int i = 0 ; i<chooseaccountletter.length;i++)
            {
              if(chooseaccountletter[i].equals(chooseaccount))
              {
                  erroraccount = false;
                  break;
              }
            }
      }while(erroraccount);

    if(chooseaccountletter[0].equals(chooseaccount))
    {
        Transaction credit = new CreditTransaction();
        credit.getTransactionInfo(categories);
        credit.getAmout();
        balance += credit.getAmout();
        transactions[numTransactions] = credit;
    }//end credit

    if(chooseaccountletter[1].equals(chooseaccount))
    {
        Transaction debit = new DebitTransaction();
        debit.getTransactionInfo(categories);
        debit.getAmout();
        balance += debit.getAmout();
        transactions[numTransactions] = debit;
    }//end debit
    numTransactions++;
    return true;
    }

}

public void displayCategory(String Category)
{   
    double total = 0;
    System.out.print("\nTransactions for Category "+Category+" :\n");


    for(int i = 0; i<numTransactions;i++){
        if(transactions[i].getCategory() == Category)
            {
                System.out.print(transactions[i].toString()+"\n");
                total += transactions[i].getAmout(); 
            }
        }

    System.out.print("Category Balance : $"+total+"\n");
}

}

3 个答案:

答案 0 :(得分:3)

Scanner未实现Serializable,因此,如果您的Bank类包含Scanner对象,则它也不可序列化。您必须删除Scanner或将其标记为transientstatic才能使Bank个对象可序列化。 (请注意,将扫描仪标记为静态将使其属于类,而不是Bank的单个实例。您可能最好将其设置为瞬态。)

答案 1 :(得分:1)

请说明会员Bank有哪些内容。如果其任何成员未实现Serializable,则无法对类本身进行序列化,除非明确标记为具有瞬态排除。

答案 2 :(得分:1)

将Bank类中的Scanner对象标记为transient以使其可序列化,或将Scanner对象移动到方法中。