eu to fazendo um projeto que tem q inserir, consultar, remover e editar dados de um BD: mas aq ta dando pal para fazer as pesquisas só ta dando java.lang.NullPointerException.
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import src.com.grupo33.modelo.Cliente;
import src.com.grupo33.repositorioCliente.RepositoryClientesMySql;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TelaConsulta extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel telapesquisa = null;
private JPanel painellista = null;
private JScrollPane jScrollPane = null;
private JList lista_clientes = null;
private JButton consultCliente = null;
private JButton consultaProdutos = null;
/**
* This is the default constructor
*/
public TelaConsulta() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(458, 314);
this.setContentPane(getJContentPane());
this.setVisible(true);
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.fill = GridBagConstraints.BOTH;
gridBagConstraints5.gridy = 2;
gridBagConstraints5.weightx = 1.0;
gridBagConstraints5.weighty = 1.0;
gridBagConstraints5.insets = new Insets(0, 20, 5, 20);
gridBagConstraints5.gridx = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 1;
gridBagConstraints2.gridy = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(getTelapesquisa(), gridBagConstraints);
jContentPane.add(getPainellista(), gridBagConstraints2);
jContentPane.add(getLista_clientes(), gridBagConstraints5);
}
return jContentPane;
}
/**
* This method initializes telapesquisa
*
* @return javax.swing.JPanel
*/
private JPanel getTelapesquisa() {
if (telapesquisa == null) {
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 2;
gridBagConstraints4.insets = new Insets(10, 0, 20, 0);
gridBagConstraints4.gridy = 0;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 1;
gridBagConstraints1.insets = new Insets(10, 0, 20, 30);
gridBagConstraints1.gridy = 0;
telapesquisa = new JPanel();
telapesquisa.setLayout(new GridBagLayout());
telapesquisa.add(getConsultCliente(), gridBagConstraints1);
telapesquisa.add(getConsultaProdutos(), gridBagConstraints4);
}
return telapesquisa;
}
/**
* This method initializes painellista
*
* @return javax.swing.JPanel
*/
private JPanel getPainellista() {
if (painellista == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.BOTH;
gridBagConstraints3.weighty = 1.0;
gridBagConstraints3.gridheight = 2;
gridBagConstraints3.weightx = 1.0;
painellista = new JPanel();
painellista.setLayout(new GridBagLayout());
painellista.add(getJScrollPane(), gridBagConstraints3);
}
return painellista;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
}
return jScrollPane;
}
/**
* This method initializes lista_clientes
*
* @return javax.swing.JList
*/
private JList getLista_clientes() {
if (lista_clientes == null) {
lista_clientes = new JList();
}
return lista_clientes;
}
/**
* This method initializes consultCliente
*
* @return javax.swing.JButton
*/
private JButton getConsultCliente() {
if (consultCliente == null) {
consultCliente = new JButton();
consultCliente.setText("Consultar Clientes");
}
return consultCliente;
}
/**
* This method initializes consultaProdutos
*
* @return javax.swing.JButton
*/
private JButton getConsultaProdutos() {
if (consultaProdutos == null) {
consultaProdutos = new JButton();
consultaProdutos.setText("Consultar Produtos");
}
return consultaProdutos;
}
public static void main(String[] args) {
RepositoryClientesMySql dao;
try {
dao = new RepositoryClientesMySql();
List<Cliente> clientes = dao.getLista();
for(Cliente cliente : clientes) {
System.out.println("Código: " + cliente.getCodigo()+ "\n");
System.out.println("Nome: " + cliente.getNome()+ "\n");
System.out.println("Cpf: " + cliente.getCpf() + "\n");
System.out.println("Endereço: " + cliente.getEndereco() + "\n");
System.out.println("Telefone: " + cliente.getTelefone() + "\n");
System.out.println("Data: " + cliente.getData() + "\n");
System.out.println("Login: " + cliente.getLogin() + "\n");
System.out.println("Senha: " + cliente.getSenha() + "\n");
}
} catch (ClassNotFoundException e) {
System.out.println("Class");
} catch (SQLException e) {
System.out.println("Not");
}
}
}
ta dando erro nesse ae e no repositorio:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import src.com.grupo33.connection.ConnectionMySql;
import src.com.grupo33.modelo.Cliente;
public class RepositoryClientesMySql implements RepositorioCliente{
private ConnectionMySql manager;
private static RepositoryClientesMySql instancia;
private Connection connection;
public RepositoryClientesMySql() throws ClassNotFoundException, SQLException {
manager = ConnectionMySql.getInstance();
}
public static RepositoryClientesMySql getInstance()
throws ClassNotFoundException, SQLException {
if (instancia == null) {
instancia = new RepositoryClientesMySql();
}
return instancia;
}
public void updateCliente(Cliente cliente) {
Statement stmt = null;
try {
String sql = "UPDATE cliente SET nome = '" + cliente.getNome() + "'" +
" WHERE codigo_cliente = "+ cliente.getCodigo() ;
stmt = manager.getConnection().createStatement();
stmt.executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public void deleteCliente(Cliente cliente) {
Statement stmt = null;
try {
String sql = "DELETE FROM cliente WHERE codigo_cliente = "
+ cliente.getCodigo();
stmt = manager.getConnection().createStatement();
stmt.executeUpdate(sql);
try {
ConnectionMySql.getInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public void insertCliente(Cliente cliente) {
Statement stmt = null;
try {
String sql = "INSERT INTO cliente " +
" (codigo, nome, cpf, endereco, telefone, data, login, senha) " +
"VALUES " +"((" + cliente.getCodigo() + "), " + "UPPER('" + cliente.getNome() + "'), (" + cliente.getCpf() + ")" +
", UPPER('" + cliente.getEndereco() + "'), ('" + cliente.getTelefone() + "'), ('" + cliente.getData() + "'), ('" + cliente.getLogin() + "')" +
", (" + cliente.getSenha() + "));";
stmt = manager.getConnection().createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public List<Cliente> getLista() {
try {
List<Cliente> clientes = new ArrayList<Cliente>();
PreparedStatement st = this.connection.prepareStatement("SELECT * from cliente");
ResultSet rs = st.executeQuery();
while(rs.next()) {
Cliente cliente = new Cliente();
cliente.setCodigo(rs.getString("codigo"));
cliente.setNome(rs.getString("nome"));
cliente.setCpf(rs.getString("cpf"));
cliente.setEndereco(rs.getString("endereco"));
cliente.setTelefone(rs.getString("telefone"));
cliente.setData(rs.getString("data"));
cliente.setLogin(rs.getString("login"));
cliente.setSenha(rs.getString("senha"));
clientes.add(cliente);
}
rs.close();
st.close();
return clientes;
}catch(SQLException e) {
throw new RuntimeException(e);
}
}
public boolean existeCliente(Cliente cliente) {
boolean resp = false;
Statement stmt = null;
try {
String strSql = "SELECT codigo FROM cliente WHERE codigo = " + cliente.getCodigo();
stmt = manager.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(strSql);
if (rs.next()) {
resp = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return resp;
}
@Override
public ArrayList<Cliente> getCliente(String nome) {
// TODO Auto-generated method stub
return null;
}
}