Meu select
public Cliente getClienteByPhone(String phone_number) throws SQLException {
String sql = "Select * from customers where phone_number = ?";
Connection conn = null;
PreparedStatement pstm = null;
// RECUPERA DADOS DO BANCO
ResultSet rs = null;
Cliente cli = null;
try {
conn = ConexaoMSQL.conectar();
pstm = conn.prepareStatement(sql);
pstm.setString(1, phone_number);
rs = pstm.executeQuery();
int cont = 0;
if (rs.next()) {
cont++;
cli = new Cliente();
// RECUPERAR ID
cli.setCustomer_id(rs.getInt("customer_id"));
cli.setName(rs.getString("name"));
cli.setLast_name(rs.getString("last_name"));
cli.setPhone_number(rs.getString("phone_number"));
cli.setIs_whatsapp(rs.getBoolean("is_whatsapp"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return cli;
}
Metodo que pegar o Phone do usuario.
protected void pesquisaByPhone() throws SQLException {
boolean isWhatsap = false;
cliente = dados.getClienteByPhone(txtPhone.getText());
txtPhone.setText(cliente.getPhone_number());
txtID.setText(String.valueOf(cliente.getCustomer_id()));
txt_nome.setText(cliente.getName());
txt_sobrenome.setText(cliente.getLast_name());
isWhatsap = cliente.isIs_whatsapp();
if (isWhatsap) {
radio_yes.setSelected(true);
radio_no.setSelected(false);
} else {
radio_no.setSelected(true);
radio_yes.setSelected(false);
}
System.out.println("Cliente existe");
}
ao inserir o valor aqui txtPhone.setText(cliente.getPhone_number()); qd o numero não existe no banco ele gera esse erro
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "application.model.Cliente.getPhone_number()" because "this.cliente" is null
at application.DAO.CadastroCliente.pesquisaByPhone(CadastroCliente.java:284)
at application.DAO.CadastroCliente$6.actionPerformed(CadastroCliente.java:145)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Como resolvo isso.
Wlw galera