2
Soon after I did some icon overwrites on the main screen and on the About screen, error occurred NullPointerException
, I have no idea how to continue and continue the course I am doing. I use Mysql, and there everything is right, it was after changing the same icons. Follow the code below,
Error Print:
package br.com.infox.telas;
import java.sql.*;
import br.com.infox.dal.ModuloConexao;
import java.awt.Color;
import javax.swing.JOptionPane;
public class TelaLogin extends javax.swing.JFrame {
Connection conexao = null;
PreparedStatement pst = null;
ResultSet rs = null;
public void logar() {
String sql = "select * from tbusuarios where login=? and senha=?";
try {
//as linhas abaixo preparam a consulta ao banco em função do
//que foi digitado nas caixas de texto. O ? é substituido pel
//conteúdo das variáveis
pst = conexao.prepareStatement(sql);
pst.setString(1, txtUsuario.getText());
pst.setString(2, txtSenha.getText());
rs = pst.executeQuery();
if (rs.next()) {
String perfil = rs.getString(6);
if (perfil.equals("admin")) {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
TelaPrincipal.menRel.setEnabled(true);
TelaPrincipal.menCadUsu.setEnabled(true);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
TelaPrincipal.lblUsuario.setForeground(Color.red);
this.dispose();
} else {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
this.dispose();
}
conexao.close();
} else {
JOptionPane.showMessageDialog(null, "Usuário e/ou senha inválido(s)");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public TelaLogin() {
initComponents();
conexao = ModuloConexao.conector();
if (conexao != null) {
lblStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/infox/icones/dbok.png")));
} else {
lblStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/infox/icones/dberro.png")));
}
}
/**
* 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")
private void initComponents() {
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtUsuario = new javax.swing.JTextField();
btnLogin = new javax.swing.JButton();
txtSenha = new javax.swing.JPasswordField();
lblStatus = new javax.swing.JLabel();
jButton2.setText("jButton2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("X System - Login");
setResizable(false);
jLabel1.setText(" Usuário");
jLabel2.setText(" Senha");
btnLogin.setText("Login");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
lblStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/infox/icones/dbok.png"))); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txtSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
.addComponent(txtUsuario)))
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnLogin))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(96, 96, 96)
.addComponent(lblStatus)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnLogin))
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(lblStatus)))
.addGap(42, 42, 42))
);
setSize(new java.awt.Dimension(599, 262));
setLocationRelativeTo(null);
}// </editor-fold>
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
//chamando o método logar
logar();
}
/**
* @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(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaLogin.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 TelaLogin().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnLogin;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel lblStatus;
private javax.swing.JPasswordField txtSenha;
private javax.swing.JTextField txtUsuario;
// End of variables declaration
}
package br.com.infox.telas;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class TelaPrincipal extends javax.swing.JFrame {
public TelaPrincipal() {
initComponents(); }
private void initComponents() {
desktop = new javax.swing.JDesktopPane();
lblUsuario = new javax.swing.JLabel();
lblData = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
lblLogo = 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();
menRelServ = new javax.swing.JMenuItem();
MenAju = new javax.swing.JMenu();
menAjuSob = new javax.swing.JMenuItem();
menOpc = new javax.swing.JMenu();
menOpcSair = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("X - Sistema de Controle de Ordem de Serviço");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
desktop.setPreferredSize(new java.awt.Dimension(630, 470));
javax.swing.GroupLayout desktopLayout = new javax.swing.GroupLayout(desktop);
desktop.setLayout(desktopLayout);
desktopLayout.setHorizontalGroup(
desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 630, Short.MAX_VALUE)
);
desktopLayout.setVerticalGroup(
desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 470, 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");
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/infox/icones/systemLogo (2).png"))); // NOI18N
lblLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/infox/icones/systemLogo.png"))); // NOI18N
menCad.setText("Cadastro");
menCadCli.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK));
menCadCli.setText("Cliente");
menCad.add(menCadCli);
menCadOs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK));
menCadOs.setText("Ordem de Serviço");
menCad.add(menCadOs);
menCadUsu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK));
menCadUsu.setText("Usuário");
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);
menRelServ.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
menRelServ.setText("Serviços");
menRel.add(menRelServ);
Menu.add(menRel);
MenAju.setText("Ajuda");
menAjuSob.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, java.awt.event.InputEvent.ALT_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");
menOpcSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
menOpcSair.setText("Sair");
menOpcSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menOpcSairActionPerformed(evt);
}
});
menOpc.add(menOpcSair);
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()
.addComponent(desktop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(62, 62, 62)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblUsuario)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(lblData)))
.addComponent(lblLogo))
.addContainerGap(61, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(83, 83, 83)
.addComponent(lblUsuario)
.addGap(55, 55, 55)
.addComponent(lblData)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(55, 55, 55)
.addComponent(lblLogo)
.addGap(87, 87, 87))
.addGroup(layout.createSequentialGroup()
.addComponent(desktop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(869, 523));
setLocationRelativeTo(null);
}// </editor-fold>
private void menCadUsuActionPerformed(java.awt.event.ActionEvent evt) {
// as linhas abaixo abrir o form TelaUsuario dentro do desktop pane
TelaUsuario usuario = new TelaUsuario();
usuario.setVisible(true);
desktop.add(usuario);
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// as linhas abaixo subsituem a label Data lblData, pela data atual do sistema ao inicializar o form.
Date data = new Date();
DateFormat formatador = DateFormat.getDateInstance(DateFormat.SHORT);
lblData.setText(formatador.format(data));
}
private void menOpcSairActionPerformed(java.awt.event.ActionEvent evt) {
// exibe uma caixa de diálogo antes de sair do sistema
int sair = JOptionPane.showConfirmDialog(null,"Tem certeza que deseja sair?","Atenção",JOptionPane.YES_NO_OPTION);
if (sair == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
private void menAjuSobActionPerformed(java.awt.event.ActionEvent evt) {
//chamando a tela sobre
TelaSobre sobre = new TelaSobre();
sobre.setVisible(true);
}
public static void main(String args[]) {
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>
/* 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.JMenu MenAju;
private javax.swing.JMenuBar Menu;
private javax.swing.JDesktopPane desktop;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblData;
private javax.swing.JLabel lblLogo;
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.JMenu menOpc;
private javax.swing.JMenuItem menOpcSair;
public static javax.swing.JMenu menRel;
private javax.swing.JMenuItem menRelServ;
// End of variables declaration
}
package br.com.infox.telas;
public class TelaSobre extends javax.swing.JFrame {
public TelaSobre() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Sistema para Controle de Ordem de Serviços");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel2.setText("Desenvolvido por: Carlos Eduardo ");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel3.setText("Sob a licença GPL");
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/infox/icones/about.png"))); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(136, 136, 136)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(jLabel4)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jLabel1)
.addGap(13, 13, 13)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jLabel4)
.addContainerGap(53, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(392, 294));
setLocationRelativeTo(null);
}// </editor-fold>
private void formWindowActivated(java.awt.event.WindowEvent 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(TelaSobre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaSobre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaSobre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaSobre.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 TelaSobre().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
// End of variables declaration
}
package br.com.infox.dal;
import java.sql.*;
public class ModuloConexao {
//método responsável por estabelecer a conexão com o banco
public static Connection conector() {
java.sql.Connection conexao = null;
// a linha abaixo chama o driver que importei para a biblioteca
String driver = "com.mysql.jdbc.Driver";
// armazenando informações referente ao banco
String url="jdbc:mysql://localhost:3306/dbinfox";
String user="root";
String password ="";
//estabelecendo a conexão com o banco
try {
Class.forName(driver);
conexao = DriverManager.getConnection(url, user, password);
return conexao;
} catch (Exception e) {
//a linha abaixo serve de apoio para esclarecer o erro
//System.out.println(e);
return null;
}
}
}
Hello Eduardo, welcome to stackoverflow. From the experience I have with Java and IT in general, the way you are describing the problem, it would be very difficult to find it, you have already learned to use the mode debug of your IDE?
– RXSD