Problema IU (Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException)

3 respostas
A

Galera,

Meu problema eh o seguinte: TEnho uma classe abstrata Funcionario e subclasses Horista e Mensalista. Fazendo os testes no metodo main, o metodo cadastraFuncionario está td ok. Porem estou implementando uma classe de InterfaceGrafica, e quando cadastro o funcionario parece que ele nao esta sendo inserido no ArrayList. O primeiro parece ser cadastrado, mas qdo pesso pra listar da erro. Qdo cadastro o segundo tbm da erro. Sempre a mesma exceção. O que pode ser?
Já sei que o problema ta nas classes da interface grafica, mas nao estou conseguindo enxergar onde! Alguem pode me ajudar?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at pgto.Empresa.cadastraFuncionario(Empresa.java:27) at UInterface$TelaCadastrar$2.actionPerformed(UInterface.java:246) 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$000(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$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.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$1.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.pumpEventsForFilter(Unknown Source) at java.awt.WaitDispatchSupport$2.run(Unknown Source) at java.awt.WaitDispatchSupport$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.awt.WaitDispatchSupport.enter(Unknown Source) at java.awt.Dialog.show(Unknown Source) at java.awt.Component.show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at java.awt.Window.setVisible(Unknown Source) at java.awt.Dialog.setVisible(Unknown Source) at UInterface$TelaCadastrar.<init>(UInterface.java:280) at UInterface$2.actionPerformed(UInterface.java:56) 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.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.menuDragMouseReleased(Unknown Source) at javax.swing.JMenuItem.fireMenuDragMouseReleased(Unknown Source) at javax.swing.JMenuItem.processMenuDragMouseEvent(Unknown Source) at javax.swing.JMenuItem.processMouseEvent(Unknown Source) at javax.swing.MenuSelectionManager.processMouseEvent(Unknown Source) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(Unknown Source) at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source) at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source) at java.awt.Toolkit.notifyAWTEventListeners(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$000(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$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.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$1.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)

O link dos arquivos do projeto no Eclipse ta aki http://www.4shared.com/file/xorabYzs/projeto.html?

Grato

3 Respostas

R

tenta fazer assim… nesse trecho:

public void cadastraFuncionario(Funcionario f) throws CPFCadastradoException{ int i = 0; while (i < listaFuncionarios.size()){ if (listaFuncionarios.get(i).getCpf().equals(f.getCpf())){ throw new CPFCadastradoException("CPF já cadastrado."); } i++; } listaFuncionarios.add(f); }

após o while, antes de testar o cpf, coloca esse trecho

if ( listaFuncionarios.get(i) == null ) { continue; }

e no getCPF da classe funcionário, trata para não vir valores NULL ( se não tiver CPF retorna uma string vazia ou um numero negativo por exemplo)

A

redr4gon

Fiz o que vc disse, mas qdo clico no botao cadastrar ele trava, algo como um loop infinito.

O problema acho q nao ta na implementação das classes do pacote pgto.

Se vc rodar o main da classe InterfaceFuncionario, os funcionarios estao sendo inseridos no ArrayList normalmente. Mas na classe Uinterface que implementa a interfaceGrafica ta dando esse erro.

A

Resolvido! Tava tentando obter o cpf de um objeto Funcionario sem instancia-lo.

Criado 13 de novembro de 2011
Ultima resposta 13 de nov. de 2011
Respostas 3
Participantes 2