*RESOLVIDO* Erro Estranho com statement? null Pointer exception

3 respostas
SirDominque
run: Exception in thread "main" java.lang.NullPointerException at upeoairport.TerminalsPanel.ConnectTomysql(TerminalsPanel.java:150) at upeoairport.UPEOAirPort.main(UPEOAirPort.java:20) CONSTRUÍDO COM SUCESSO (tempo total: 4 segundos)

Alguem por gentileza, poderia me ajudar ?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upeoairport;

import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author André
 */
public class TerminalsPanel extends javax.swing.JFrame {

    public static Connection conn = null;
    
    /**
     * Creates new form TerminalsPanel
     */
    public TerminalsPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jDesktopPane2 = new javax.swing.JDesktopPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Time", "Destiny", "Flight", "Title 4", "Counter", "Boarding", "Gate", "?"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jScrollPane1.setBounds(0, 0, 790, 210);
        jDesktopPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        
        
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TerminalsPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TerminalsPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TerminalsPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TerminalsPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TerminalsPanel().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JDesktopPane jDesktopPane2;
    private javax.swing.JScrollPane jScrollPane1;
    public static javax.swing.JTable jTable1;
    // End of variables declaration

 
  
  public static void ConnectTomysql()
  {
      
    String driverName = "com.mysql.jdbc.Driver";
    String serverName = "localhost";    //caminho do servidor do BD
    String mydatabase ="airlines";     
    String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
    String username = "root";          
    String password = "4001";     
        
    try {
          conn = DriverManager.getConnection(url,username,password);
          Statement stm = null;
          stm.executeQuery("");
          
        } catch (SQLException ex) {
           System.out.println("ex.getSQLState() : " + ex.getSQLState());
           System.out.println("ex.getMessage() : " + ex.getMessage());
        }

    


  
      
  }

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upeoairport;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author André
 */
public class UPEOAirPort {

   
    public static void main(String[] args) {
            
        TerminalsPanel.main(args);
        TerminalsPanel.ConnectTomysql();  
            
     
        
    }
}

3 Respostas

gmmascarin

Por que você criou o Statement com null? Assim você não conseguirá.

Use

Statement stm = conn.createStatement();
gmmascarin

Artigo legal sobre jdbc: http://www.guj.com.br/articles/7

SirDominque

Muito Obrigado senhor. Consegui aqui!!!

Criado 23 de junho de 2012
Ultima resposta 23 de jun. de 2012
Respostas 3
Participantes 2