Erro

Sou novato em JAVA, e estou tentando conectar no banco de dados, seguinte, ele mostra que conectou com o banco certinho só que quando ele chega a parte de executar o seguinte código:

            Statement comando   = con.createStatement();

dá o seguinte erro:

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at ex14.Janela$ListenerAcaoAbrir.actionPerformed(Janela.java:71)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

alguém poderia me ajudar?

agradecendo desde já

Isso tá parecendo erro na sua interface e não na parte de banco de dados… Cola o código ai :stuck_out_tongue:

Dá a impressão que con == null porque você tentou abrir a conexão, não conseguiu, mas em vez de tratar a exceção corretamente, simplesmente a “abafou” (try/catch vazio) e continuou em frente. Como não tenho seu programa para ver, o que posso dizer é mais ou menos isso (minha bola de cristal eu comprei no Paraguai, sabe como é que é…)

o código está abaixo

/*

  • Janela.java
  • Created on 19 de Julho de 2006, 13:27
  • To change this template, choose Tools | Options and locate the template under
  • the Source Creation and Management node. Right-click the template and choose
  • Open. You can then make changes to the template in the Source Editor.
    */

package ex14;
import java.awt.;
import java.awt.event.
;
import javax.swing.;
import java.sql.
;
/**
*

  • @author Administrador
    */
    public class Janela extends JFrame {

    /** Creates a new instance of Janela */
    JPanel contentPane;
    JMenuBar barraMenu = new JMenuBar();
    JMenu menuArquivo = new JMenu();
    JMenuItem itemMenuSair = new JMenuItem();
    BorderLayout borderLayout1 = new BorderLayout();
    JMenuItem itemMenuAbrir = new JMenuItem();
    JTextArea areaTexto = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(areaTexto);
    JLabel status = new JLabel();
    Connection con;

    //contrutor da janela
    public Janela(){
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400,300));
    this.setTitle(“Teste de Persistência”);
    this.addWindowListener(new ListenerJanela());
    menuArquivo.setText(“Arquivo”);
    itemMenuSair.setText(“Sair”);
    itemMenuSair.addActionListener(new ListenerAcaoSair());
    itemMenuAbrir.setText(“Abrir”);
    itemMenuAbrir.addActionListener(new ListenerAcaoAbrir());
    menuArquivo.add(itemMenuAbrir);
    menuArquivo.addSeparator();
    menuArquivo.add(itemMenuSair);
    barraMenu.add(menuArquivo);
    this.setJMenuBar(barraMenu);
    areaTexto.setText("Escolha Arquivo --> Abrir para ler " + “registros…”);
    areaTexto.setLineWrap(true);
    status.setText("Conexão JDBC: ");
    this.getContentPane().add(scrollPane, BorderLayout.CENTER);
    this.getContentPane().add(areaTexto, BorderLayout.CENTER);
    this.getContentPane().add(status, BorderLayout.SOUTH);
    }

    //opção do menu: arquivo -> Sair selecionada
    class ListenerAcaoSair implements ActionListener{
    public void actionPerformed(ActionEvent e){
    System.exit(0);
    }
    }

    //opção do menu: arquivo --> abrir selecionada
    class ListenerAcaoAbrir implements ActionListener{
    public void actionPerformed(ActionEvent e){
    try{
    String query = “SELECT * FROM funcionario”;
    Statement comando = con.createStatement();
    /* ResultSet registros = comando.executeQuery(query);
    areaTexto.setText("");
    while (registros.next()){
    areaTexto.append("Identificação " + registros.getLong(“id”) + “\n”);
    areaTexto.append("Nome " + registros.getLong(“nome”) + “\n”);
    areaTexto.append(“Cargo " + registros.getLong(“cargo”) + “\n”);
    areaTexto.append(“Idade " + registros.getLong(“idade”) + “\n”);
    areaTexto.append(“salario_hora” + registros.getLong(“salario_hora”) + “\n”);
    areaTexto.append(”-----------------------------------------------------\n”);
    }
    */ }
    catch(SQLException ex){
    }
    }
    }

    //Opção da janela: Fechar ou ALT+F4 selecionada
    class ListenerJanela extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }

     public void windowOpened(WindowEvent e){
         try{
             //DriverManager.getConnection("jdbc:odbc:JdbcTesteAccess","","");
             //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
             //String stringConexao = "jdbc:odbc:funcionarios_link";
             //Connection con = DriverManager.getConnection(stringConexao,"","");
             Connection con = DriverManager.getConnection ("jdbc:odbc:funcionarios_link","",""); 
             //status.setText(status.getText() + stringConexao);
             status.setText(status.getText() + "tiago");
         }
         catch(ClassNotFoundException ex1){
             status.setText(ex1.toString());
             status.setText("Erro1");
         }
         catch(SQLException ex2){
             status.setText(ex2.toString());
             status.setText("Erro2");
         }
     }
    

    }
    }

valeu galera por favor me ajudar