Hello! I have a problem with filtering JTable (AbstractTableModel)... So, this is my code:
//Create table and filter
public TableRowSorter<MyTableModel> sorter;
public static MyTableModel tableModel;
...
//Define table and fill it with List "userDataList"
List<UserDataRow> userDataList = readuserdatafromfile();
filetree.Form.tableDetails = new JTable();
tableModel = new MyTableModel( userDataList );
filetree.Form.tableDetails.setModel(tableModel);
filetree.Form.tableDetails.setFillsViewportHeight(true);
// Create textfield and add DocumentListener
JTextField txtFirstName = new JTextField();
txtFirstName.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
newFilter();
}
public void insertUpdate(DocumentEvent e) {
newFilter();
}
public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
// Define filter
private void newFilter() {
RowFilter<MyTableModel, Object> rf = null;
try {
rf = RowFilter.regexFilter("(?i)" + txtFirstName.getText());
} catch (java.util.regex.PatternSyntaxException e) {
System.err.println(e.getMessage());
}
sorter.setRowFilter(rf);
}
When I enter a char to textfield, I get the following error message:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at filetree.FilterClass.newFilter(FilterClass.java:238)
at filetree.FilterClass.access$000(FilterClass.java:34)
at filetree.FilterClass$1.insertUpdate(FilterClass.java:126)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:185)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:734)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:693)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:114)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:655)
at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1351)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:859)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1633)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2886)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2814)
at java.awt.Component.processEvent(Component.java:6044)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Pliz, help me understand and fix this error.