Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. public Class getColumnClass(int c) {
  2. return getValueAt(0, c).getClass();
  3. }
  4.  
  5. public Class getColumnClass(int columnIndex) {
  6. Object obj = getValueAt(0, columnIndex);
  7. if (obj == null) return Object.class;
  8. return obj.getClass();
  9. }
  10.  
  11. Successfully connected to the database: jdbc:postgresql://localhost:5433/sample_db
  12. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  13. at com.jdbc.ui.EmployeeTableModel.getValueAt(EmployeeTableModel.java:49)
  14. at com.jdbc.ui.EmployeeTableModel.getColumnClass(EmployeeTableModel.java:71)
  15. at javax.swing.JTable.getColumnClass(JTable.java:2698)
  16. at javax.swing.JTable.getCellRenderer(JTable.java:5674)
  17. at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2113)
  18. at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
  19. at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
  20. at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
  21. at javax.swing.JComponent.paintComponent(JComponent.java:780)
  22. at javax.swing.JComponent.paint(JComponent.java:1056)
  23. at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
  24.  
  25. public class EmployeeTableModel extends AbstractTableModel {
  26.  
  27. private static final int EMPNO_COL = 0;
  28. private static final int ENAME_COL = 1;
  29. private static final int JOB_COL = 2;
  30. private static final int MGR_COL = 3;
  31. private static final int HIREDATE_COL = 4;
  32. private static final int SAL_COL = 5;
  33. private static final int COMM_COL = 6;
  34. private static final int DEPTNO_COL = 7;
  35.  
  36. private String[] columnNames = { "empno", "ename", "job", "mgr", "hiredate", "sal", "comm", "deptno"};
  37. private List<Employee> employees;
  38.  
  39. public EmployeeTableModel(List<Employee> theEmployees) {
  40. employees = theEmployees;
  41. }
  42.  
  43. @Override
  44. public int getColumnCount() {
  45. return columnNames.length;
  46. }
  47.  
  48. @Override
  49. public int getRowCount() {
  50. return employees.size();
  51. }
  52.  
  53. @Override
  54. public String getColumnName(int colNumber) {
  55. return columnNames[colNumber];
  56. }
  57.  
  58. @Override
  59. public Object getValueAt(int row, int col) {
  60.  
  61. Employee tempEmployee = employees.get(row);
  62.  
  63. switch (col) {
  64. case EMPNO_COL:
  65. return tempEmployee.getEmpno();
  66. case ENAME_COL:
  67. return tempEmployee.getEname();
  68. case JOB_COL:
  69. return tempEmployee.getJob();
  70. case MGR_COL:
  71. return tempEmployee.getMgr();
  72. case HIREDATE_COL:
  73. return tempEmployee.getHiredate();
  74. case SAL_COL:
  75. return tempEmployee.getSal();
  76. case COMM_COL:
  77. return tempEmployee.getComm();
  78. case DEPTNO_COL:
  79. return tempEmployee.getDeptno();
  80. default:
  81. return Object.class;
  82. }
  83. }
  84.  
  85. @Override
  86. public Class getColumnClass(int columnIndex) {
  87. Object obj = getValueAt(0, columnIndex);
  88. if (obj == null) return Object.class;
  89. return obj.getClass();
  90. }
  91. }
  92.  
  93. public class EmployeeApp extends JFrame {
  94. private JPanel panelMain;
  95. private JButton searchButton;
  96. private JScrollPane scrollPane;
  97. private JTextField idTextField;
  98. private JLabel lastNameLabel;
  99. private JTable table;
  100.  
  101. private EmployeesDao employeesDao;
  102.  
  103.  
  104. public EmployeeApp() {
  105.  
  106. // create the DAO
  107. try {
  108. employeesDao = new EmployeesDao();
  109. } catch (Exception exc) {
  110. JOptionPane.showMessageDialog(this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
  111. }
  112.  
  113. searchButton.addActionListener(new ActionListener() {
  114. public void actionPerformed(ActionEvent e) {
  115. String searchValue = idTextField.getText();
  116. Employee singleEmployee;
  117. List<Employee> employees = null;
  118.  
  119. try {
  120.  
  121. int numValue = Integer.parseInt(searchValue);
  122. singleEmployee = employeesDao.getEmployeeById(numValue);
  123. employees = Collections.singletonList(singleEmployee);
  124.  
  125. // create the model and update the table
  126. EmployeeTableModel empModel1 = new EmployeeTableModel(employees);
  127. table.setModel(empModel1);
  128.  
  129.  
  130. } catch (NumberFormatException | SQLException | NullPointerException exc) {
  131. //JOptionPane.showMessageDialog(EmployeeApp.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
  132.  
  133. if (searchValue != null && searchValue.trim().length() > 0) {
  134. try {
  135. employees = employeesDao.searchEmployees(searchValue);
  136. } catch (Exception e1) {
  137. e1.printStackTrace();
  138. }
  139. // create the model and update the table
  140. EmployeeTableModel empModel2 = new EmployeeTableModel(employees);
  141. table.setModel(empModel2);
  142. } else {
  143. try {
  144. employees = employeesDao.getAllEmployees();
  145. } catch (SQLException e1) {
  146. e1.printStackTrace();
  147. }
  148. // create the model and update the table
  149. EmployeeTableModel empModel3 = new EmployeeTableModel(employees);
  150. table.setModel(empModel3);
  151. }
  152.  
  153. }
  154.  
  155. }
  156.  
  157. });
  158.  
  159. table = new JTable();
  160. scrollPane.setViewportView(table);
  161. }
  162.  
  163.  
  164. public static void main(String[] args) {
  165. JFrame frame = new JFrame("EmployeeApp");
  166. frame.setContentPane(new EmployeeApp().panelMain);
  167. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  168. frame.pack();
  169. frame.setVisible(true);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement