fiz algumas modificações os erros de escrita desapareceram
eu dei um jeito de tirar a classe inserirCadastro e ir direto pra inserirCadastro_prof
funcionou porém,
quando eu boto pra rodar aparece logo atrás da tela com os botões a tela de cadastro de professor e eu queria que ela so aparecesse qdo clicasse no botão
inserir cadastro professor
além desse problema qdo clico realmente no botão ele dispara um erro
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inserirCadastro_prof.actionPerformed(inserirCadastro_prof.java:18)
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)
Seguem os códigos refeitos
Biblioteca classe main
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
public class Biblioteca extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
//Declaração da referencia ao objeto que efetua a conexão com o banco de dados;
public Connection conecta;
public Biblioteca() {
//============Banco de dados=======;
try{
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
String serverName = "localhost";
String mydatabase = "biblioteca";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "root";
String password = "root";
conecta = DriverManager.getConnection(url, username, password);
System.out.println("Conexão efetuada\n");
}
catch(ClassNotFoundException e)
{
System.out.println("Conexão não efetuada, classe");
}
catch(SQLException d)
{
System.out.println("erro na conexão");
}
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String args[]){
new interface_2().setVisible(true);
}}
import java.awt.*;
import java.sql.Connection;
import javax.swing.*;
public class interface_2 extends JFrame{
/**Autor: Rodrigo Ribeiro
* Interface inicial para o usuário
*/
private static final long serialVersionUID = 1L;
JButton inserir_Cad,altera_Cad,exclui_Cad,inserir_Pub,altera_Pub,exclui_Pub,gerar_Rel,loc_Pub;
private Connection conecta;
private interface_3_prof telaprofessor;
public interface_2(){
setTitle("Escolha ação a ser realizada");
Container c=getContentPane();
c.setLayout(null);
inserir_Cad= new JButton("Inserir Cadastro Professor");
inserir_Cad.setBounds(15,10,300,30);/**(coluna,linha,largura,altura)*/
inserir_Cad.addActionListener(new inserirCadastro_prof(conecta,telaprofessor));
c.add(inserir_Cad);
altera_Cad= new JButton("Alterar Cadastro");
altera_Cad.setBounds(15,50,200,30);
//altera_Cad.addActionListener(new alteraCadastro());
c.add(altera_Cad);
exclui_Cad= new JButton("Excluir Cadastro");
exclui_Cad.setBounds(15,90,200,30);
//exclui_Cad.addActionListener(new excluiCadastro());
c.add(exclui_Cad);
inserir_Pub= new JButton("Inserir Publicação");
inserir_Pub.setBounds(15,130,200,30);
//inserir_Pub.addActionListener(new inserirPublicacao());
c.add(inserir_Pub);
altera_Pub= new JButton("Alterar Publicação");
altera_Pub.setBounds(15,170,200,30);
//altera_Pub.addActionListener(new alteraPublicacao());
c.add(altera_Pub);
exclui_Pub= new JButton("Excluir Publicação");
exclui_Pub.setBounds(15,210,200,30);
//exclui_Pub.addActionListener(new excluiPublicacao());
c.add(exclui_Pub);
loc_Pub= new JButton("Locar Publicação");
loc_Pub.setBounds(15,250,200,30);
//loc_Pub.addActionListener(new locaPublicacao());
c.add(loc_Pub);
gerar_Rel= new JButton("Gerar Relatórios");
gerar_Rel.setBounds(15,290,200,30);
//gerar_Rel.addActionListener(new gerarRelatorio());
c.add(gerar_Rel);
setLocation(100,100);/**Posição na tela*/
setSize(500,450);/**Tamanho da tela*/
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.*;
public class inserirCadastro_prof implements ActionListener{
public interface_3_prof campos;
public Connection conecta;
public inserirCadastro_prof(Connection d, interface_3_prof a){
new interface_3_prof().setVisible(true);
campos=a;
conecta=d;
}
public void actionPerformed(ActionEvent e){
try{
Statement executaSQL=conecta.createStatement();
if(!campos.nome_prof.getText().equals("")){
String query="INSERT INTO professor(nm_prof,tel_prof,end_prof) VALUES( '"+campos.nome_prof.getText()+
"','"+campos.tel_prof.getText()+
"','"+campos.end_prof.getText()+
"')";
int result = executaSQL.executeUpdate(query);
if (result==1){
System.out.println("Inserido \n");
campos.nome_prof.setText("");
campos.tel_prof.setText("");
campos.end_prof.setText("");
}
else{
System.out.println("Erro \n");
campos.nome_prof.setText("");
campos.tel_prof.setText("");
campos.end_prof.setText("");
}
}
else
JOptionPane.showMessageDialog(null, "PREENCHA OS CAMPOS","Atenção",JOptionPane.ERROR_MESSAGE);
executaSQL.close();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
System.out.println(sqlex.toString());
}
}
}
/**Versão 1.0
*
*/
import java.awt.*;
import javax.swing.*;
/**
* @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 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);
c.add(salvar);
cancelar= new JButton("Cancelar");
cancelar.setBounds(125,310,100,20);
c.add(cancelar);
setLocation(100,100);
setSize(800,450);
}}