SQLException

Toda vez que eu executo o código abaixo aparece o seguinte erro…

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at inserirCadastro_prof.actionPerformed(inserirCadastro_prof.java:17)
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.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)

   1. import java.awt.event.ActionEvent;  
   2. import java.awt.event.ActionListener;  
   3. import javax.swing.*;  
   4. import java.sql.*;  
   5.   
   6. public class inserirCadastro_prof implements ActionListener{  
   7.     public interface_3_prof campos;  
   8.     public Connection conecta;  
   9.       
  10.     public inserirCadastro_prof(Connection d, interface_3_prof a){  
  11.         campos=a;  
  12.         conecta=d;  
  13.     }  
  14.     public void actionPerformed(ActionEvent e){  
  15.         try{  
  16.             Statement executaSQL=conecta.createStatement();  
  17.             if(!campos.nome_prof.getText().equals("")){  
  18.                   
  19.                 String query="INSERT INTO professor(nm_prof,tel_prof,end_prof) VALUES( '"+campos.nome_prof.getText()+  
  20.                 "','"+campos.tel_prof.getText()+  
  21.                 "','"+campos.end_prof.getText()+  
  22.                 "')";  
  23.                   
  24.                 int result = executaSQL.executeUpdate(query);  
  25.                   
  26.                 if (result==1){  
  27.                     System.out.println("Inserido \n");  
  28.                     campos.nome_prof.setText("");  
  29.                     campos.tel_prof.setText("");  
  30.                     campos.end_prof.setText("");  
  31.                   
  32.                       
  33.                 }  
  34.                 else{  
  35.                     System.out.println("Erro \n");  
  36.                     campos.nome_prof.setText("");  
  37.                     campos.tel_prof.setText("");  
  38.                     campos.end_prof.setText("");  
  39.                 }  
  40.             }  
  41.             else  
  42.                 JOptionPane.showMessageDialog(null, "PREENCHA OS CAMPOS","Atenção",JOptionPane.ERROR_MESSAGE);  
  43.             executaSQL.close();  
  44.         }  
  45.         catch(SQLException sqlex){  
  46.             sqlex.printStackTrace();  
  47.             System.out.println(sqlex.toString());  
  48.         }  
  49.     }  
  50. }  [/code]


o código é este

[code]/**Versão 1.0
 * 
 */
import java.awt.*;
import javax.swing.*;
import java.sql.*;
/**
 * @author Rodrigo Ribeiro Pereira
 *Interface de cadastro de professor
 */
public class interface_3_prof extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lnome_prof,lend_prof,ltel_prof;
	public JTextField nome_prof,tel_prof,end_prof;
	private JButton salvar,cancelar;
	public Connection d;
	public interface_3_prof a;
	
	
	public interface_3_prof(){
		setTitle("Cadastro de professor");
		Container c=getContentPane();
		c.setLayout(null);
		
		lnome_prof=new JLabel("Nome : ");
		lnome_prof.setBounds(15,10,70,20);
		c.add(lnome_prof);
		nome_prof= new JTextField(50);
		nome_prof.setBounds(95, 10, 280, 20);
		c.add(nome_prof);
		
		ltel_prof=new JLabel("Telefone : ");
		ltel_prof.setBounds(15,40,70,20);
		c.add(ltel_prof);
		tel_prof= new JTextField(10);
		tel_prof.setBounds(95, 40, 170, 20);
		c.add(tel_prof);
		
		lend_prof=new JLabel("Endereço : ");
		lend_prof.setBounds(15,70,70,20);
		c.add(lend_prof);
		end_prof= new JTextField(10);
		end_prof.setBounds(95, 70, 250, 20);
		c.add(end_prof);
		
		salvar= new JButton("Salvar");
		salvar.setBounds(15,310,100,20);
		salvar.addActionListener(new inserirCadastro_prof(d,a));
		c.add(salvar);
		
		cancelar= new JButton("Cancelar");
		cancelar.setBounds(125,310,100,20);
		c.add(cancelar);
		
		
		
		setLocation(100,100);
		setSize(800,450);
}}

Baste ler sua exceção…e achar o erro…


at inserirCadastro_prof.actionPerformed( inserirCadastro_prof.java:17 )

rrp1989 , não precisa postar a mesma dúvida em dois forums diferentes.

Imaginei que a outra pudesse estar difícil de compreender

Sorry 8)

Primeiro vamos esclarecer
isso:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

não é SQLException e sim NullPointerException

portanto o título já está errado,

Segundo
at inserirCadastro_prof.actionPerformed(inserirCadastro_prof.java:17)

Linha 17 existe algo nulo.

Abraço

if(!campos.nome_prof.getText().equals(""))

Um desses caras é nulo, escrevendo o código assim, com essas chamadas desse jeito, é grande chance de dar null pointer.

Como eu posso fazer para saber onde está o NullPointer???

Vlw por ajudar