Les explico, estoy haciendo un programa de guardar la información de un contacto con Mysql
y estoy poniendo un filtro
de búsqueda en la interfaz de Netbeans
, hace la busqueda pero a la hora de seleccionar a una persona en la tabla no me muestra nada y me da este error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Aquí les dejo el código:
public class datos extends javax.swing.JFrame { <- Error
/**
* Creates new form Datos
*/
String filtro="id";
DefaultTableModel model;
@Override
public Image getIconImage(){
Image retValue = Toolkit.getDefaultToolkit().
getImage(ClassLoader.getSystemResource("imagenes/agregar-usuario.png"));
return retValue;
}
public datos() {
initComponents();
tablaDatos("");
}
void tablaDatos(String Valor){
Connection conectar = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
if(combo.getSelectedItem()== "id"){
filtro= "id";
}
if(combo.getSelectedItem()== "nombre"){
filtro= "Nombre";
}
if(combo.getSelectedItem()== "apellido"){
filtro= "Apellido";
}
if(combo.getSelectedItem()== "compañia"){
filtro= "Compañia";
}
String sql="SELECT * FROM datos WHERE "+filtro+" LIKE '%"+Valor+"%'";
DefaultTableModel modelo = new DefaultTableModel();
modelo.addColumn("ID");
modelo.addColumn("Nombre");
modelo.addColumn("Apellido");
modelo.addColumn("Email");
modelo.addColumn("Telefono");
modelo.addColumn("Compañia");
modelo.addColumn("Posicion");
modelo.addColumn("Nota");
jTable1.setModel(modelo);
String []datos= new String [8];
try{
Statement st=conectar.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
datos[0]=rs.getString(1);
datos[1]=rs.getString(2);
datos[2]=rs.getString(3);
datos[3]=rs.getString(4);
datos[4]=rs.getString(5);
datos[5]=rs.getString(6);
datos[6]=rs.getString(7);
datos[7]=rs.getString(8);
modelo.addRow(datos);
}
jTable1.setModel(modelo);
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
}
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Connection conectar = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
try{
String sql= "UPDATE datos SET id=?," +"Nombre=?,"
+ "Apellido=?,"
+ "Email=?,"
+"Telefono=?,"
+"Compañia=?,"
+"Posicion=?,"
+"Foto=?,"
+"Nota=?,"
+"Ruta=?"
+"WHERE id='"+jTextField1.getText()+"'";
PreparedStatement pst= conectar.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
pst.setString(4, jTextField4.getText());
pst.setString(5, jTextField5.getText());
pst.setString(6, jTextField6.getText());
pst.setString(7, jTextField7.getText());
pst.setString(8, jTextField8.getText());
FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
pst.setBinaryStream(10, Archivofoto);
pst.setString(9, jTextField10.getText());
int n = pst.executeUpdate();
if (n > 0) {
JOptionPane.showMessageDialog(null, "Se modificaron los datos de manera correcta :D");
tablaDatos("");
} else {
JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo modificar los datos :c");
}
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement pst = null;
try {
Connection conectar = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
String insertar = "INSERT INTO datos( id, Nombre, Apellido,Email, Telefono, Compañia, Posicion, Nota, Foto, Ruta)VALUES(?,?,?,?,?,?,?,?,?,?)";
pst = conectar.prepareStatement(insertar);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
pst.setString(4, jTextField4.getText());
pst.setString(5, jTextField5.getText());
pst.setString(6, jTextField6.getText());
pst.setString(7, jTextField7.getText());
pst.setString(8, jTextField8.getText());
FileInputStream ArchivosFoto;
FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
pst.setBinaryStream(9, Archivofoto);
pst.setString(10, jTextField10.getText());
int i = pst.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog(null, "Se ha guardadado correctamente :D");
tablaDatos("");
}
else {
JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo guardar :c");
}
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
pst.close();
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {
JFileChooser archivo = new JFileChooser();
int ventana = archivo.showOpenDialog(null);
if (ventana == JFileChooser.APPROVE_OPTION) {
File file = archivo.getSelectedFile();
jTextField10.setText(String.valueOf(file));
Image foto = getToolkit().getImage(jTextField10.getText());
jLabel10.setIcon(new ImageIcon(foto));
}
}
private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
int fx=JOptionPane.showConfirmDialog(null,"¿Esta realmente seguro de eliminar este registro?"+jTextField1.getText()+"?");
if(fx==JOptionPane.YES_OPTION){
Connection conectar = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
try{
Statement s=conectar.createStatement();
int i=s.executeUpdate("DELETE FROM datos WHERE id='"+jTextField1.getText()+"'");
if(i==1){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
jLabel10.setIcon(null);
jTextField10.setText("");
if (i > 0) {
JOptionPane.showMessageDialog(null, "Se elimino el registro de forma exitosa :D");
tablaDatos("");
} else {
JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo eliminar el registro :(");
}
}
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
jLabel10.setIcon(null);
jTextField10.setText("");
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
int row=jTable1.getSelectedRow();
String id=
Error ->String.valueOf(model.getValueAt(jTable1.getSelectedRow(), 0)); <- ERROR
if(row >= 0){
jTextField1.setText(jTable1.getValueAt(row, 0).toString());
jTextField2.setText(jTable1.getValueAt(row, 1).toString());
jTextField3.setText(jTable1.getValueAt(row, 2).toString());
jTextField6.setText(jTable1.getValueAt(row, 3).toString());
Connection conectar=null;
try{
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
try{
Statement s=conectar.createStatement();
ResultSet rs=s.executeQuery("SELECT Nombre,Apellido,Email,Telefono,Compañia,Posicion,Foto,Nota,Ruta FROM datos WHERE id='"+id+"'");
Image i=null;
Blob blob=rs.getBlob("Foto");
i=javax.imageio.ImageIO.read(blob.getBinaryStream());
ImageIcon image = new ImageIcon(i);
jLabel10.setIcon(image);
jTextField10.setText(rs.getString(3));
} catch (SQLException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
JOptionPane.showMessageDialog(null, "No se selecciono fila");
}
}
public static void main(String args[]) {
//<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(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
id
tiene contenido ? y para optimizar en que linea sale ese error ?