Guest User

Untitled

a guest
Aug 11th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.11 KB | None | 0 0
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2.  
  3. public class datos extends javax.swing.JFrame { <- Error
  4.  
  5. /**
  6. * Creates new form Datos
  7. */
  8. String filtro="id";
  9. DefaultTableModel model;
  10.  
  11. @Override
  12. public Image getIconImage(){
  13. Image retValue = Toolkit.getDefaultToolkit().
  14. getImage(ClassLoader.getSystemResource("imagenes/agregar-usuario.png"));
  15. return retValue;
  16.  
  17. }
  18.  
  19. public datos() {
  20. initComponents();
  21. tablaDatos("");
  22. }
  23. void tablaDatos(String Valor){
  24. Connection conectar = null;
  25.  
  26. try{
  27.  
  28. Class.forName("com.mysql.jdbc.Driver");
  29. conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
  30.  
  31.  
  32. } catch (ClassNotFoundException ex) {
  33. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  34. } catch (SQLException ex) {
  35. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  36. }
  37.  
  38. if(combo.getSelectedItem()== "id"){
  39. filtro= "id";
  40.  
  41. }
  42.  
  43.  
  44. if(combo.getSelectedItem()== "nombre"){
  45. filtro= "Nombre";
  46.  
  47. }
  48. if(combo.getSelectedItem()== "apellido"){
  49. filtro= "Apellido";
  50.  
  51. }
  52.  
  53.  
  54. if(combo.getSelectedItem()== "compañia"){
  55. filtro= "Compañia";
  56.  
  57. }
  58. String sql="SELECT * FROM datos WHERE "+filtro+" LIKE '%"+Valor+"%'";
  59.  
  60. DefaultTableModel modelo = new DefaultTableModel();
  61. modelo.addColumn("ID");
  62. modelo.addColumn("Nombre");
  63. modelo.addColumn("Apellido");
  64. modelo.addColumn("Email");
  65. modelo.addColumn("Telefono");
  66. modelo.addColumn("Compañia");
  67. modelo.addColumn("Posicion");
  68. modelo.addColumn("Nota");
  69.  
  70. jTable1.setModel(modelo);
  71. String []datos= new String [8];
  72. try{
  73. Statement st=conectar.createStatement();
  74. ResultSet rs=st.executeQuery(sql);
  75. while(rs.next()){
  76. datos[0]=rs.getString(1);
  77. datos[1]=rs.getString(2);
  78. datos[2]=rs.getString(3);
  79. datos[3]=rs.getString(4);
  80. datos[4]=rs.getString(5);
  81. datos[5]=rs.getString(6);
  82. datos[6]=rs.getString(7);
  83. datos[7]=rs.getString(8);
  84.  
  85. modelo.addRow(datos);
  86. }
  87. jTable1.setModel(modelo);
  88. } catch (SQLException ex) {
  89. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  90. }
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97. pack();
  98. }// </editor-fold>
  99.  
  100. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  101. Connection conectar = null;
  102.  
  103. try{
  104.  
  105. Class.forName("com.mysql.jdbc.Driver");
  106. conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
  107.  
  108.  
  109. } catch (ClassNotFoundException ex) {
  110. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  111. } catch (SQLException ex) {
  112. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  113. }
  114. try{
  115. String sql= "UPDATE datos SET id=?," +"Nombre=?,"
  116. + "Apellido=?,"
  117. + "Email=?,"
  118. +"Telefono=?,"
  119. +"Compañia=?,"
  120. +"Posicion=?,"
  121. +"Foto=?,"
  122. +"Nota=?,"
  123. +"Ruta=?"
  124. +"WHERE id='"+jTextField1.getText()+"'";
  125.  
  126. PreparedStatement pst= conectar.prepareStatement(sql);
  127.  
  128. pst.setString(1, jTextField1.getText());
  129. pst.setString(2, jTextField2.getText());
  130. pst.setString(3, jTextField3.getText());
  131. pst.setString(4, jTextField4.getText());
  132. pst.setString(5, jTextField5.getText());
  133. pst.setString(6, jTextField6.getText());
  134. pst.setString(7, jTextField7.getText());
  135. pst.setString(8, jTextField8.getText());
  136. FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
  137. pst.setBinaryStream(10, Archivofoto);
  138. pst.setString(9, jTextField10.getText());
  139. int n = pst.executeUpdate();
  140. if (n > 0) {
  141. JOptionPane.showMessageDialog(null, "Se modificaron los datos de manera correcta :D");
  142. tablaDatos("");
  143.  
  144. } else {
  145.  
  146. JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo modificar los datos :c");
  147.  
  148. }
  149. } catch (SQLException ex) {
  150. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  151. } catch (FileNotFoundException ex) {
  152. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  153. }
  154. }
  155.  
  156. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  157. PreparedStatement pst = null;
  158. try {
  159. Connection conectar = null;
  160. try {
  161. Class.forName("com.mysql.jdbc.Driver");
  162. conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
  163.  
  164. } catch (SQLException | ClassNotFoundException ex) {
  165. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  166. }
  167. String insertar = "INSERT INTO datos( id, Nombre, Apellido,Email, Telefono, Compañia, Posicion, Nota, Foto, Ruta)VALUES(?,?,?,?,?,?,?,?,?,?)";
  168. pst = conectar.prepareStatement(insertar);
  169. pst.setString(1, jTextField1.getText());
  170. pst.setString(2, jTextField2.getText());
  171. pst.setString(3, jTextField3.getText());
  172. pst.setString(4, jTextField4.getText());
  173. pst.setString(5, jTextField5.getText());
  174. pst.setString(6, jTextField6.getText());
  175. pst.setString(7, jTextField7.getText());
  176. pst.setString(8, jTextField8.getText());
  177. FileInputStream ArchivosFoto;
  178. FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
  179. pst.setBinaryStream(9, Archivofoto);
  180. pst.setString(10, jTextField10.getText());
  181. int i = pst.executeUpdate();
  182. if (i > 0) {
  183. JOptionPane.showMessageDialog(null, "Se ha guardadado correctamente :D");
  184. tablaDatos("");
  185. }
  186. else {
  187.  
  188. JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo guardar :c");
  189.  
  190. }
  191.  
  192. } catch (SQLException ex) {
  193. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  194. } catch (FileNotFoundException ex) {
  195. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  196. } finally {
  197. try {
  198. pst.close();
  199. } catch (SQLException ex) {
  200. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  201. }
  202. }
  203.  
  204. }
  205.  
  206. private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {
  207. JFileChooser archivo = new JFileChooser();
  208. int ventana = archivo.showOpenDialog(null);
  209. if (ventana == JFileChooser.APPROVE_OPTION) {
  210. File file = archivo.getSelectedFile();
  211. jTextField10.setText(String.valueOf(file));
  212. Image foto = getToolkit().getImage(jTextField10.getText());
  213. jLabel10.setIcon(new ImageIcon(foto));
  214.  
  215. }
  216. }
  217.  
  218. private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
  219. // TODO add your handling code here:
  220. }
  221.  
  222. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  223. int fx=JOptionPane.showConfirmDialog(null,"¿Esta realmente seguro de eliminar este registro?"+jTextField1.getText()+"?");
  224. if(fx==JOptionPane.YES_OPTION){
  225. Connection conectar = null;
  226.  
  227. try{
  228.  
  229. Class.forName("com.mysql.jdbc.Driver");
  230. conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
  231.  
  232.  
  233. } catch (ClassNotFoundException ex) {
  234. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  235. } catch (SQLException ex) {
  236. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  237. }
  238.  
  239. try{
  240. Statement s=conectar.createStatement();
  241. int i=s.executeUpdate("DELETE FROM datos WHERE id='"+jTextField1.getText()+"'");
  242. if(i==1){
  243. jTextField1.setText("");
  244. jTextField2.setText("");
  245. jTextField3.setText("");
  246. jTextField4.setText("");
  247. jTextField5.setText("");
  248. jTextField6.setText("");
  249. jTextField7.setText("");
  250. jTextField8.setText("");
  251. jLabel10.setIcon(null);
  252. jTextField10.setText("");
  253. if (i > 0) {
  254. JOptionPane.showMessageDialog(null, "Se elimino el registro de forma exitosa :D");
  255. tablaDatos("");
  256. } else {
  257.  
  258. JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo eliminar el registro :(");
  259.  
  260. }
  261. }
  262. } catch (SQLException ex) {
  263. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  264. }
  265. }
  266. }
  267.  
  268. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  269. jTextField1.setText("");
  270. jTextField2.setText("");
  271. jTextField3.setText("");
  272. jTextField4.setText("");
  273. jTextField5.setText("");
  274. jTextField6.setText("");
  275. jTextField7.setText("");
  276. jTextField8.setText("");
  277. jLabel10.setIcon(null);
  278. jTextField10.setText("");
  279.  
  280. }
  281.  
  282.  
  283.  
  284. private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
  285. int row=jTable1.getSelectedRow();
  286. String id=
  287. Error ->String.valueOf(model.getValueAt(jTable1.getSelectedRow(), 0)); <- ERROR
  288. if(row >= 0){
  289. jTextField1.setText(jTable1.getValueAt(row, 0).toString());
  290. jTextField2.setText(jTable1.getValueAt(row, 1).toString());
  291. jTextField3.setText(jTable1.getValueAt(row, 2).toString());
  292. jTextField6.setText(jTable1.getValueAt(row, 3).toString());
  293.  
  294. Connection conectar=null;
  295. try{
  296. Class.forName("com.mysql.jdbc.Driver");
  297. DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");
  298.  
  299. } catch (ClassNotFoundException ex) {
  300. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  301. } catch (SQLException ex) {
  302. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  303. }
  304. try{
  305. Statement s=conectar.createStatement();
  306. ResultSet rs=s.executeQuery("SELECT Nombre,Apellido,Email,Telefono,Compañia,Posicion,Foto,Nota,Ruta FROM datos WHERE id='"+id+"'");
  307. Image i=null;
  308. Blob blob=rs.getBlob("Foto");
  309. i=javax.imageio.ImageIO.read(blob.getBinaryStream());
  310. ImageIcon image = new ImageIcon(i);
  311. jLabel10.setIcon(image);
  312. jTextField10.setText(rs.getString(3));
  313.  
  314.  
  315. } catch (SQLException ex) {
  316. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  317. } catch (IOException ex) {
  318. Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
  319. }
  320.  
  321.  
  322.  
  323. }else{
  324. JOptionPane.showMessageDialog(null, "No se selecciono fila");
  325. }
  326. }
  327.  
  328.  
  329. public static void main(String args[]) {
  330.  
  331. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  332. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  333. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  334. */
  335. try {
  336. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  337. if ("Nimbus".equals(info.getName())) {
  338. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  339. break;
  340. }
  341. }
  342. } catch (ClassNotFoundException ex) {
  343. java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  344. } catch (InstantiationException ex) {
  345. java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  346. } catch (IllegalAccessException ex) {
  347. java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  348. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  349. java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  350. }
  351.  
  352. String id= String.valueOf(model.getValueAt(jTable1.getSelectedRow(), 0).toString())
Add Comment
Please, Sign In to add comment