Ao rodar meu projeto, ao tentar acessar minha tela Principal em outros computadores, apresenta o erro java.lang.nullpointerexception: cannot invoke “java.sql.connection.preparestatement(string)” because “this.conector” is null.
public class moduloConexao {
public static Connection conector(){
Connection conector = null;
String URL = "jdbc:mysql://localhost:3306/db_biblioteca";
String USER = "root";
String PASSWORD = "root";
try{
conector = DriverManager.getConnection(URL, USER, PASSWORD);
return conector;
} catch(SQLException e) {
e.printStackTrace();
return null;
}
}
}
Tela Principal
public class TelaPrincipal extends javax.swing.JFrame {
Connection conector = null;
/**
* Creates new form TelaPrincipal
*/
public TelaPrincipal() {
initComponents();
conector = moduloConexao.conector();
}
/**
* 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() {
Desktop = new javax.swing.JDesktopPane();
lblUsuario = new javax.swing.JLabel();
lblData = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
Menu = new javax.swing.JMenuBar();
menCad = new javax.swing.JMenu();
menCadCli = new javax.swing.JMenuItem();
menCadOs = new javax.swing.JMenuItem();
menCadUsu = new javax.swing.JMenuItem();
MenRel = new javax.swing.JMenu();
menRelCli = new javax.swing.JMenuItem();
menRelSer = new javax.swing.JMenuItem();
MenAju = new javax.swing.JMenu();
menAjuSob = new javax.swing.JMenuItem();
MenOpc = new javax.swing.JMenu();
menOpcSai = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("X - Sistema para Controle de OS");
setPreferredSize(new java.awt.Dimension(915, 523));
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
javax.swing.GroupLayout DesktopLayout = new javax.swing.GroupLayout(Desktop);
Desktop.setLayout(DesktopLayout);
DesktopLayout.setHorizontalGroup(
DesktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 641, Short.MAX_VALUE)
);
DesktopLayout.setVerticalGroup(
DesktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 481, Short.MAX_VALUE)
);
lblUsuario.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
lblUsuario.setText("Usuário");
lblData.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
lblData.setText("Data");
menCad.setText("Cadastro");
menCadCli.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_DOWN_MASK));
menCadCli.setText("Cliente");
menCadCli.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menCadCliActionPerformed(evt);
}
});
menCad.add(menCadCli);
menCadOs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_DOWN_MASK));
menCadOs.setText("Os");
menCadOs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menCadOsActionPerformed(evt);
}
});
menCad.add(menCadOs);
menCadUsu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.ALT_DOWN_MASK));
menCadUsu.setText("Usuarios");
menCadUsu.setEnabled(false);
menCadUsu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menCadUsuActionPerformed(evt);
}
});
menCad.add(menCadUsu);
Menu.add(menCad);
MenRel.setText("Relatório");
MenRel.setEnabled(false);
menRelCli.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.ALT_DOWN_MASK));
menRelCli.setText("Clientes");
menRelCli.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menRelCliActionPerformed(evt);
}
});
MenRel.add(menRelCli);
menRelSer.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_DOWN_MASK));
menRelSer.setText("Serviços");
menRelSer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menRelSerActionPerformed(evt);
}
});
MenRel.add(menRelSer);
Menu.add(MenRel);
MenAju.setText("Ajuda");
menAjuSob.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, java.awt.event.InputEvent.ALT_DOWN_MASK));
menAjuSob.setText("Sobre");
menAjuSob.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menAjuSobActionPerformed(evt);
}
});
MenAju.add(menAjuSob);
Menu.add(MenAju);
MenOpc.setText("Opções");
menOpcSai.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_DOWN_MASK));
menOpcSai.setText("Sair");
menOpcSai.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menOpcSaiActionPerformed(evt);
}
});
MenOpc.add(menOpcSai);
Menu.add(MenOpc);
setJMenuBar(Menu);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Desktop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(254, 254, 254)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblData)
.addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(lblUsuario)
.addGap(27, 27, 27)
.addComponent(lblData)
.addGap(355, 355, 355)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Desktop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(30, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(915, 548));
setLocationRelativeTo(null);
}// </editor-fold>
private void menRelSerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void menOpcSaiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int sair = JOptionPane.showConfirmDialog(null, "tem certeze que deseja sair?", "Atenção", JOptionPane.YES_NO_OPTION);
if (sair == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
Date data = new Date();
DateFormat formatador = DateFormat.getDateInstance(DateFormat.SHORT);
lblData.setText(formatador.format(data));
}
private void menAjuSobActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TelaSobre sobre = new TelaSobre();
sobre.setVisible(true);
}
private void menCadUsuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TelaUsuario usuario = new TelaUsuario();
usuario.setVisible(true);
Desktop.add(usuario);
}
private void menCadCliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TelaCliente cliente = new TelaCliente();
cliente.setVisible(true);
Desktop.add(cliente);
}
private void menCadOsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TelaOS os = new TelaOS();
os.setVisible(true);
Desktop.add(os);
}
private void menRelCliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaPrincipal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JDesktopPane Desktop;
private javax.swing.JMenu MenAju;
private javax.swing.JMenu MenOpc;
public static javax.swing.JMenu MenRel;
private javax.swing.JMenuBar Menu;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblData;
public static javax.swing.JLabel lblUsuario;
private javax.swing.JMenuItem menAjuSob;
private javax.swing.JMenu menCad;
private javax.swing.JMenuItem menCadCli;
private javax.swing.JMenuItem menCadOs;
public static javax.swing.JMenuItem menCadUsu;
private javax.swing.JMenuItem menOpcSai;
private javax.swing.JMenuItem menRelCli;
private javax.swing.JMenuItem menRelSer;