Swing weird problem

本帖最後由 luckiejacky 於 2013-9-29 18:14 編輯

Hope any ching can help me. Need to demo to client tomorrow
I don't know why this line
throws null exception.
String email = t.getValueAt(i, 5).toString();

  1.     public class EmployeePanel extends GradientPanel {  
  2.       
  3.          
  4.          public EmployeePanel(int empId, Database _db) throws SQLException {  
  5.                
  6.          
  7.          
  8.             super (_db);  
  9.              this.setOpaque(false);  
  10.                initComponents();  
  11.               
  12.       
  13.             jTable1.setRowSelectionAllowed(true);  
  14.             jTable1.setCellSelectionEnabled(true);  
  15.               
  16.             TableModel model = jTable1.getModel();  
  17.                
  18.              // Assign the editor to the second column  
  19.              TableColumnModel dtcm = jTable1.getColumnModel();  
  20.           // todo: do this inside database  
  21.               ResultSet  rs   = db.getAllEmployeeTypes();  
  22.                
  23.               List<String> lemp = new ArrayList<>();  
  24.                
  25.               while (rs.next())  
  26.               {  
  27.                  String type = null;   
  28.                     
  29.                  type = rs.getString(2);  
  30.                     
  31.                   lemp.add(type);  
  32.                     
  33.               }  
  34.                   
  35.                   
  36.                  JComboBox discomboBox = new JComboBox(lemp.toArray());  
  37.                  discomboBox.setEditable(true);  
  38.                  DefaultCellEditor diseditor = new DefaultCellEditor(discomboBox);  
  39.       
  40.                 dtcm.getColumn(2).setCellEditor(diseditor);  
  41.         
  42.                
  43.         }  
  44.            
  45.         // for creating  
  46.         public EmployeePanel(Database _db) throws SQLException {  
  47.             super(_db);  
  48.          }  
  49.          
  50.         public JTable getEmployeeTable()  
  51.         {  
  52.             return jTable1;  
  53.         }  
  54.          private void initComponents() {  
  55.       
  56.             jScrollPane1 = new javax.swing.JScrollPane();  
  57.             jTable1 = new javax.swing.JTable();  
  58.       
  59.             setPreferredSize(new java.awt.Dimension(497, 800));  
  60.             setLayout(new java.awt.BorderLayout());  
  61.       
  62.             jTable1.setModel(new javax.swing.table.DefaultTableModel(  
  63.                 new Object [][] {  
  64.                     {null, null, null, null, null, null},  
  65.                     {null, null, null, null, null, null},  
  66.                     {null, null, null, null, null, null},  
  67.                     {null, null, null, null, null, null}  
  68.                 },  
  69.                 new String [] {  
  70.                     "员工姓氏", "员工姓名", "员工类型", "工作电话", "分机号码", "电邮"  
  71.                 }  
  72.             ));  
  73.             jScrollPane1.setViewportView(jTable1);  
  74.       
  75.             add(jScrollPane1, java.awt.BorderLayout.CENTER);  
  76.         }  
  77.         
  78.         private javax.swing.JScrollPane jScrollPane1;  
  79.         private javax.swing.JTable jTable1;  
  80.       
  81.     }  





  82.       
  83.     public final class CreateEmployee extends GenericForm {  
  84.         public CreateEmployee(String topBarStr, Database _db) throws SQLException  
  85.         {  
  86.             super(topBarStr, _db, 1, false);  
  87.             
  88.               
  89.             empId = db.generateEmployeeID();  
  90.             createEmployeePanel = new EmployeePanel(empId, db);  
  91.             this.add(createEmployeePanel, BorderLayout.CENTER);  
  92.          
  93.               
  94.               
  95.             this.getDone().addActionListener(new java.awt.event.ActionListener() {  
  96.                 public void actionPerformed(java.awt.event.ActionEvent evt) {  
  97.                        
  98.                           
  99.                           
  100.                         Employee employee = new Employee();  
  101.                           
  102.                         JTable t =  createEmployeePanel.getEmployeeTable();  
  103.                           
  104.                         int col = t.getColumnCount();  
  105.                        for (int i = 0; i < t.getRowCount(); i++)   
  106.                        {  
  107.                               
  108.                           
  109.                             employee.setFirstName(t.getValueAt(i, 0).toString());  
  110.               
  111.                             employee.setLastName(t.getValueAt(i, 1).toString());  
  112.                           
  113.                             int empTypeID = db.checkEmpType(t.getValueAt(i, 2).toString());  
  114.                             employee.setEmpType(empTypeID);  
  115.                             employee.setWorkPhone(t.getValueAt(i, 3).toString());  
  116.                               
  117.                               
  118.                               
  119.                             employee.setExtension(t.getValueAt(i, 4).toString());  
  120.               
  121.                             String email = t.getValueAt(i, 5).toString();   
  122.                             employee.setEmailAddress(email);  
  123.                           
  124.                              if (!employee.getFirstName().equals("") && !employee.getLastName().equals("") &&  
  125.                                     employee.getEmpType() != -1 && !employee.getEmailAddress().equals("") && !employee.getExtension().equals("")  
  126.                                             && !employee.getWorkPhone().equals(""))   
  127.                             {  
  128.                                 try {  
  129.                                     db.insertEmployee(employee);  
  130.                                 } catch (SQLException ex) {  
  131.                                     Logger.getLogger(CreateEmployee.class.getName()).log(Level.SEVERE, null, ex);  
  132.                                 }  
  133.                                 JOptionPane.showMessageDialog(null,"Record added");  
  134.                             }  
  135.                     }  
  136.                        
  137.                   
  138.                 }  
  139.                   
  140.       
  141.                   
  142.             });  
  143.               
  144.             
  145.               
  146.         }  
  147.          
  148.          @Override  
  149.         protected void initComponents()  
  150.         {  
  151.               
  152.             NavigationHints.add("员工");  
  153.              NavigationHints.add("加入员工");  
  154.              super.initComponents();  
  155.                
  156.                  
  157.                
  158.               
  159.                
  160.         }  
  161.          
  162.         int empId;  
  163.         EmployeePanel createEmployeePanel;  
  164.      }  
複製代碼

本帖最後由 luckiejacky 於 2013-9-29 18:28 編輯

This happens to this too. I tried to add another column to both
of these models to no avail, but  email and Price are null when inputted with
1234@yahoo.com.cn and 1000
  1. public class CreateProduct extends GenericForm {
  2.    
  3.    
  4.     public CreateProduct(String _topBarStr, Database _db, int _layout) throws SQLException
  5.     {
  6.         super(_topBarStr, _db, _layout, false);
  7.         initComponents();
  8.         createProductPanel = new ProductPanel2("", db);
  9.         this.setPreferredSize(new Dimension(1000, 600));
  10.         this.add(createProductPanel, BorderLayout.CENTER);
  11.    
  12.         
  13.         
  14.         this.getDone().addActionListener(new java.awt.event.ActionListener() {
  15.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  16.                try {
  17.                     
  18.                     Products product = new Products();
  19.                     JTable t = createProductPanel.getProductTable();
  20.                     
  21.                     for (int i = 0; i < t.getRowCount(); i++)
  22.                     {
  23.                         product.setProductID(UtilityFactory.generateProductID(db));
  24.                         product.setProductName(t.getValueAt(i, 0).toString());

  25.                         int categoryID = db.checkCategoryID(t.getValueAt(i, 1).toString());
  26.                         product.setCategoryID(categoryID);

  27.                        String price =  String.valueOf(t.getValueAt(i, 2));

  28.                         product.setUnitPrice(Integer.parseInt(price));
  29.                     }
  30.                     if (!product.getProductName().equals("") &&
  31.                             product.getCategoryID() != -1 && product.getUnitPrice() != 0)
  32.                     {
  33.                         // todo returns 1
  34.                         if (db.insertProduct(product) != -1)
  35.                                     db.addProductSerial();
  36.                         JOptionPane.showMessageDialog(null,"Record added");
  37.                     }
  38.                 } catch (SQLException ex) {
  39.                     Logger.getLogger(FindProduct.class.getName()).log(Level.SEVERE, null, ex);
  40.                 }
  41.             }
  42.                
  43.             

  44.             
  45.         });
  46.         
  47.       
  48.         
  49.     }
  50.    
  51.        @Override
  52.     protected void initComponents()
  53.     {
  54.         super.initComponents();
  55.         NavigationHints.add("产品");
  56.          NavigationHints.add("加入产品");
  57.          
  58.            
  59.          
  60.         
  61.          
  62.     }
  63.      
  64.     ProductPanel2 createProductPanel;

  65.    
  66. }

  67. public class ProductPanel2 extends GradientPanel {

  68.     public ProductPanel2(String productID, Database _db) throws SQLException
  69.     {
  70.         super( _db);
  71.         initComponents();
  72.         ResultSet rs = _db.getProductDetails2(productID);
  73.         this.setPreferredSize(new Dimension(1000, 600));
  74.         this.setOpaque(false);
  75.         
  76.         jTable1.setRowSelectionAllowed(true);
  77.         jTable1.setCellSelectionEnabled(true);
  78.         
  79.          TableColumnModel dtcm = jTable1.getColumnModel();
  80.       // todo: do this inside database
  81.           ResultSet  rs2   = db.getAllProductCategories();
  82.          
  83.           List<String> lp = new ArrayList<>();
  84.          
  85.           while (rs2.next())
  86.           {
  87.              String type = null;
  88.               
  89.              type = rs2.getString(2);
  90.               
  91.               lp.add(type);
  92.               
  93.           }
  94.             
  95.             
  96.              JComboBox discomboBox = new JComboBox(lp.toArray());
  97.              discomboBox.setEditable(true);
  98.              DefaultCellEditor diseditor = new DefaultCellEditor(discomboBox);

  99.             dtcm.getColumn(1).setCellEditor(diseditor);
  100.   
  101.       
  102.         
  103.         /*
  104.         while (rs.next())
  105.         {
  106.             txtProductName.setText(rs.getString("ProductName"));
  107.             cbProductCategory.setSelectedItem(rs.getString("CategoryName"));
  108.                     
  109.             txtProductPrice.setText(rs.getBigDecimal("UnitPrice").toString());
  110.         }
  111.         */
  112.     }
  113.    
  114.     public JTable getProductTable()
  115.     {
  116.         return jTable1;
  117.     }
  118.      
  119.     public ProductPanel2(Database _db) throws SQLException {
  120.         super(_db);
  121.        initComponents();
  122.          
  123.         this.setPreferredSize(new Dimension(1000, 600));
  124.         this.setOpaque(false);
  125.         
  126.         jTable1.setRowSelectionAllowed(true);
  127.         jTable1.setCellSelectionEnabled(true);
  128.         
  129.          TableColumnModel dtcm = jTable1.getColumnModel();
  130.       // todo: do this inside database
  131.           ResultSet  rs2   = db.getAllProductCategories();
  132.          
  133.           List<String> lp = new ArrayList<>();
  134.          
  135.           while (rs2.next())
  136.           {
  137.              String type = null;
  138.               
  139.              type = rs2.getString(2);
  140.               
  141.               lp.add(type);
  142.               
  143.           }
  144.             
  145.             
  146.              JComboBox discomboBox = new JComboBox(lp.toArray());
  147.              discomboBox.setEditable(true);
  148.              DefaultCellEditor diseditor = new DefaultCellEditor(discomboBox);

  149.             dtcm.getColumn(1).setCellEditor(diseditor);
  150.   
  151.         /*
  152.         List<String> Category = _db.getCategoryNames();
  153.         
  154.         for (String s : Category)
  155.         {
  156.             cbProductCategory.addItem(s);
  157.         }*/        
  158.     }
  159.    
  160.     String getProductCategory() {
  161.         return null;
  162.     }
  163.    
  164.      
  165.   
  166.     private void initComponents() {

  167.         jScrollPane1 = new javax.swing.JScrollPane();
  168.         jTable1 = new javax.swing.JTable();

  169.         setPreferredSize(new java.awt.Dimension(563, 800));
  170.         setLayout(new java.awt.BorderLayout());

  171.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  172.             new Object [][] {
  173.                 {null, null, null},
  174.                 {null, null, null},
  175.                 {null, null, null},
  176.                 {null, null, null}
  177.             },
  178.             new String [] {
  179.                 "产品名称", "产品类型", "产品价格"
  180.             }
  181.         ));
  182.         jScrollPane1.setViewportView(jTable1);

  183.         add(jScrollPane1, java.awt.BorderLayout.CENTER);
  184.     }
  185.     private javax.swing.JScrollPane jScrollPane1;
  186.     private javax.swing.JTable jTable1;
  187.      

  188.    
  189. }
複製代碼

TOP