Como corrigir esse tipo de excepção, e porquê ela ocorre?

3 respostas
rasinc
run gui.SplashJProgressBar

Exception in thread AWT-EventQueue-0 java.lang.NullPointerException

at gui.Conta$1.actionPerformed(Conta.java:69)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)

at java.awt.Component.processMouseEvent(Component.java:6175)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:5940)

at java.awt.Container.processEvent(Container.java:2105)

at java.awt.Component.dispatchEventImpl(Component.java:4536)

at java.awt.Container.dispatchEventImpl(Container.java:2163)

at java.awt.Component.dispatchEvent(Component.java:4362)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)

at java.awt.Container.dispatchEventImpl(Container.java:2149)

at java.awt.Window.dispatchEventImpl(Window.java:2478)

at java.awt.Component.dispatchEvent(Component.java:4362)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

A classe onde ocorre é esta:

/*
 * Autor: Amarildo J. C. Lucas (rasinc)
 * CEO: Real Arts Studio.inc
 * WebSite: www.realarts3d.blogspot.com
 * Descrição: Desenvolver uma janela de cadastro de contas
 * Obs: Deverá ser o mais customizável possível
 */

package gui;

//import home.rasinc.ras.0.RealProgrammingDeveloper.inc/PacotesemDesenvolvimento/Classes/icons;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Conta extends JFrame{

 private String[] cidades = {"                                    ", "Ex: Lisboa", "Ex: Luanda", "Ex: Paris"};
 private String[] emails = {"Ex: [email removido]", "Ex: [email removido]", "Ex: [email removido]       "};
 static Conta conta;
 JButton btn;
 //Login login = new Login();
 JTextField txfconta;
 public Conta(){
  setTitle("Conta");
  
  Container c = new Container();
      c = getContentPane();
      c.setLayout(new GridBagLayout());
      
      
      ImageIcon iconanterior = new ImageIcon(getClass().getResource("voltar.png"));
      ImageIcon iconavancar = new ImageIcon(getClass().getResource("avancar.png"));
      
      JPanel geral = new JPanel();
      geral.setLayout(new GridBagLayout());
      JPanel especifico = new JPanel();
      especifico.setLayout(new GridBagLayout());
      JPanel painelB = new JPanel();
      painelB.setLayout(new GridBagLayout());
      
      
      
      geral.setBorder(BorderFactory.createTitledBorder("Banco"));
      especifico.setBorder(BorderFactory.createTitledBorder("Contacto"));
      painelB.setBorder(BorderFactory.createTitledBorder("painelB"));
      
      JLabel lbltitular = new JLabel("Conta");
      JLabel lblusuario = new JLabel("HomePage");
      
      JLabel lbltelefone = new JLabel("Tefefone");
      JLabel lblfax = new JLabel("Fax");
      JLabel lblemail = new JLabel("Email");
      JLabel lblendereco = new JLabel("Agência");
      JLabel lblhomepage = new JLabel("Homepage");
      
      //JLabel lbldireitos = new JLabel("Concordo com todos os termos reservados");
    
      ActionListener actionListener = new ActionListener(){
        public void actionPerformed(ActionEvent actionEvent){
          //AbstractButton abs = (AbstractButton) actionEvent.getSource();
          
          
          if(actionEvent.getSource() == btn){
            //boolean selected = abs.getModel().isSelected();
            //conta.setVisible(false);
            conta.dispose();
            //conta.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            //new Login();
            //System.exit(0);
            //login.setVisible(true);
          }
        }
      };
      
      
      btn = new JButton("Salvar");
      btn.addActionListener(actionListener);
         JCheckBox checkDireitos = new JCheckBox("Concordo");
      checkDireitos.addActionListener(actionListener);
      JComboBox boxendereco = new JComboBox(cidades);
      boxendereco.setEditable(true);
      
      txfconta = new JTextField(22);
      JTextField txfusuario = new JTextField(12);
      
      JTextField txffax = new JTextField(2);
      JTextField txftelefone = new JTextField(10);
      JTextField txfhomepage = new JTextField(17);
      JComboBox boxemail = new JComboBox(emails);
      boxemail.setEditable(true);
      
      JButton btnanterior = new JButton();
      btnanterior.setIcon(iconanterior);
      btnanterior.setBorderPainted(false);
     
      
      JButton btnavancar = new JButton();
      btnavancar.setIcon(iconavancar);
      btnavancar.setBorderPainted(false);
      
      geral.add(lbltitular, new GridBagConstraints(0,0,1,1,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (5,0,0,0),0,0));
      geral.add(txfconta, new GridBagConstraints(1,0,4,1,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5,5,0,15),0,0));
      geral.add(lblendereco, new GridBagConstraints(0,1,1,2,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),0,0));
      geral.add(boxendereco, new GridBagConstraints(1,1,4,2,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10,5,0,15),0,0));
      
     
      geral.add(lblusuario, new GridBagConstraints(0,3,1,4,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10,0,10,10),0,0));
      geral.add(txfusuario, new GridBagConstraints(1,3,4,4,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(10,5,10,15),0,0));
      
      especifico.add(lbltelefone, new GridBagConstraints(0,0,1,1,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,25,0,0),0,0));
      especifico.add(txftelefone, new GridBagConstraints(1,0,2,1,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,25,0,0),0,0));
      
      especifico.add(lblfax, new GridBagConstraints(3,0,4,1,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0,20,0,0),0,0));
      
      especifico.add(txffax, new GridBagConstraints(4,0,5,1,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,50,0,45),0,0));
      especifico.add(lblemail, new GridBagConstraints(0,1,1,2,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(10,25,0,0),0,0));
      especifico.add(boxemail, new GridBagConstraints(1,1,4,2,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(10,25,0,0),0,0));
      
      //especifico.add(lblhomepage, new GridBagConstraints(0,2,1,3,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(20,25,0,0),0,0));
      //especifico.add(txfhomepage, new GridBagConstraints(1,2,4,3,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(20,25,0,0),0,0));
      
      
      especifico.add(btn, new GridBagConstraints(4,2,5,3,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(55,54,0,30),0,0));
      
 //painelB.add(btnanterior, new GridBagConstraints(0,0,1,1,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),0,0));
   //   painelB.add(btnavancar, new GridBagConstraints(0,1,1,2,0,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),0,0));
      
      
      
      c.add(geral, new GridBagConstraints(0,0,1,1,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),0,0));
      c.add(especifico, new GridBagConstraints(0,1,1,2,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(10,0,0,0),0,0));
      c.add(painelB, new GridBagConstraints(0,2,1,3,0,0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(10,0,0,0),0,0));
      
      
      setSize(530, 360);
      setLocationRelativeTo(null);
      
      
 }
 
 /*class Check implements ItemListener{
   public void itemStateChanged(ItemEvent ev){
     if(ev.getSource() == checkDireitos){   
       new Login();
     } 
  }
 }*/
  
 
  public static void main(String[] args){
  
   try {
            // select Look and Feel
            //UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
   //UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
   //UIManager.setLookAndFeel( new com.nilo.plaf.nimrod.NimRODLookAndFeel());
   //UIManager.setLookAndFeel(new rpdinc.realartsinc.realfinance.Login.SubstanceLookAndFeel());
   UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            // start application
   
   
            
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
  
   conta = new Conta();
   conta.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   conta.setVisible(true);
  }
}

Por favor, ajudem, pois ela tá limitando o avanço da minha aplicação…

3 Respostas

F

O problema esta nessa linha:

conta.dispose();

quando voce chama esse metodo a conta nao foi criada ainda.

Nao faça tudo dentro do construtor da classe, crie metodos separados para isso.

D

Eu dei uma olhada RÁPIDA e parece que a “conta.dispose();”, a classe não foi criada. Crie a classe e depois use seus métodos.

PS :: Sugiro que separe o programa em métodos e o deixe com melhor tabulado e comentado, para um programa simples não tem problema, mas em um código gigantesco, será difícil encontrar o erro.

alberto665_1

é fundamento que voce siga as orientações acima para que o seu aplicativo seja desenvolvido de uma forma sustentável.

mais para uma solução rapida

ActionListener actionListener = new ActionListener(){   
        public void actionPerformed(ActionEvent actionEvent){              
            if(actionEvent.getSource() == btn){   
             conta.dispose();   
            }   
        }   
      };

coloque esse trecho abaixo do
conta=new conta();
que esta localizado no metodo main

e digite
btn.addActionListener(actionListener);

não espero que essa dica seja util pois se nao for ficarei frustado.

Criado 1 de dezembro de 2010
Ultima resposta 2 de dez. de 2010
Respostas 3
Participantes 4