Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Class getColumnClass(int c) {
- return getValueAt(0, c).getClass();
- }
- public Class getColumnClass(int columnIndex) {
- Object obj = getValueAt(0, columnIndex);
- if (obj == null) return Object.class;
- return obj.getClass();
- }
- Successfully connected to the database: jdbc:postgresql://localhost:5433/sample_db
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- at com.jdbc.ui.EmployeeTableModel.getValueAt(EmployeeTableModel.java:49)
- at com.jdbc.ui.EmployeeTableModel.getColumnClass(EmployeeTableModel.java:71)
- at javax.swing.JTable.getColumnClass(JTable.java:2698)
- at javax.swing.JTable.getCellRenderer(JTable.java:5674)
- at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2113)
- at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
- at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
- at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
- at javax.swing.JComponent.paintComponent(JComponent.java:780)
- at javax.swing.JComponent.paint(JComponent.java:1056)
- at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
- public class EmployeeTableModel extends AbstractTableModel {
- private static final int EMPNO_COL = 0;
- private static final int ENAME_COL = 1;
- private static final int JOB_COL = 2;
- private static final int MGR_COL = 3;
- private static final int HIREDATE_COL = 4;
- private static final int SAL_COL = 5;
- private static final int COMM_COL = 6;
- private static final int DEPTNO_COL = 7;
- private String[] columnNames = { "empno", "ename", "job", "mgr", "hiredate", "sal", "comm", "deptno"};
- private List<Employee> employees;
- public EmployeeTableModel(List<Employee> theEmployees) {
- employees = theEmployees;
- }
- @Override
- public int getColumnCount() {
- return columnNames.length;
- }
- @Override
- public int getRowCount() {
- return employees.size();
- }
- @Override
- public String getColumnName(int colNumber) {
- return columnNames[colNumber];
- }
- @Override
- public Object getValueAt(int row, int col) {
- Employee tempEmployee = employees.get(row);
- switch (col) {
- case EMPNO_COL:
- return tempEmployee.getEmpno();
- case ENAME_COL:
- return tempEmployee.getEname();
- case JOB_COL:
- return tempEmployee.getJob();
- case MGR_COL:
- return tempEmployee.getMgr();
- case HIREDATE_COL:
- return tempEmployee.getHiredate();
- case SAL_COL:
- return tempEmployee.getSal();
- case COMM_COL:
- return tempEmployee.getComm();
- case DEPTNO_COL:
- return tempEmployee.getDeptno();
- default:
- return Object.class;
- }
- }
- @Override
- public Class getColumnClass(int columnIndex) {
- Object obj = getValueAt(0, columnIndex);
- if (obj == null) return Object.class;
- return obj.getClass();
- }
- }
- public class EmployeeApp extends JFrame {
- private JPanel panelMain;
- private JButton searchButton;
- private JScrollPane scrollPane;
- private JTextField idTextField;
- private JLabel lastNameLabel;
- private JTable table;
- private EmployeesDao employeesDao;
- public EmployeeApp() {
- // create the DAO
- try {
- employeesDao = new EmployeesDao();
- } catch (Exception exc) {
- JOptionPane.showMessageDialog(this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
- }
- searchButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String searchValue = idTextField.getText();
- Employee singleEmployee;
- List<Employee> employees = null;
- try {
- int numValue = Integer.parseInt(searchValue);
- singleEmployee = employeesDao.getEmployeeById(numValue);
- employees = Collections.singletonList(singleEmployee);
- // create the model and update the table
- EmployeeTableModel empModel1 = new EmployeeTableModel(employees);
- table.setModel(empModel1);
- } catch (NumberFormatException | SQLException | NullPointerException exc) {
- //JOptionPane.showMessageDialog(EmployeeApp.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
- if (searchValue != null && searchValue.trim().length() > 0) {
- try {
- employees = employeesDao.searchEmployees(searchValue);
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- // create the model and update the table
- EmployeeTableModel empModel2 = new EmployeeTableModel(employees);
- table.setModel(empModel2);
- } else {
- try {
- employees = employeesDao.getAllEmployees();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- // create the model and update the table
- EmployeeTableModel empModel3 = new EmployeeTableModel(employees);
- table.setModel(empModel3);
- }
- }
- }
- });
- table = new JTable();
- scrollPane.setViewportView(table);
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame("EmployeeApp");
- frame.setContentPane(new EmployeeApp().panelMain);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement