private class ButtonAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Verificação para registro
if (e.getSource() == button1) {
criar.setNome(txtField1.getText());
criar.setCpf(txtField2.getText());
Memento mementoToAdd = criar.storeInMemento();
lista.addMemento(mementoToAdd);
SalvarObjeto.salvar(mementoToAdd, "C:\\Users\\Renan\\Desktop\\Banco de dados\\Memento.sav");
System.out.println("Memento criado");
}
Boa noite pessoal, estava dando uma olhada em padrões de design java, entre outras coisas e me decidi fazer um programa que implementasse dois padrões conhecidos (State e Memento), todavia minha duvida não tem nada haver com isso.
No codigo acima, eu tenho a classe SalvarObjeto (essa classe escreve um objeto em um arquivo externo), todavia para um objeto em java poder ser escrito ele deve ser serializable.
Esse mementoToAdd me retorna um objeto do tipo Memento (nome engraçado), esse objeto já implementa a interface Serializable
public class Memento implements Serializable {
private State currentState;
private State bloqueadoState;
private State saldoNegState;
private SaldoPositivo saldoPosState;
private String nome;
private String CPF;
private double saldo;
public Memento(String nome, String cpf) {
bloqueadoState = new Bloqueado();
saldoNegState = new SaldoNegativo(this);
saldoPosState = new SaldoPositivo(this);
currentState = saldoPosState;
this.nome = nome;
this.CPF = cpf;
saldo = 0;
}
Mesmo implementando Serializable, ao tentar escrever esse objeto no arquivo externo eu recebo um erro
java.io.NotSerializableException: state_codes.Bloqueado
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 Inteface.SalvarObjeto.salvar(SalvarObjeto.java:17)
at Inteface.InterfaceLogin$ButtonAction.actionPerformed(InterfaceLogin.java:94)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Alguém com mais experiencia teria como me dar alguma luz aqui? Parece ser simples o problema, mas tô umas 2 horas direto tentando sair disso. Agradeço qualquer ajuda!