Results 1 to 4 of 4

Thread: TableSorter

  1. #1
    mattj is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default TableSorter

    Im using TableSorter.java available from java.sun.com/docs/books/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java
    to sort my table, which includes a column with dates. Heres what i tried

    Java Code:
    DefaultTableModel model = new DefaultTableModel() {
                Class [] classes = {int.class, String.class, String.class, Date.class, Date.class, double.class};
                    
                public Class getColumnClass(int column) {
                    return classes[column];
                }
            };
    		
    	
            TableSorter sorter = new TableSorter(model);
    	JTable table = new JTable(sorter);
    	sorter.setTableHeader(table.getTableHeader());
    However when i modify my table in any way it comes up with a NullPointerException. If i just use the DefaultTableModel my code works fine except it compares everything as strings.

    How do i compare the table elements using the correct class?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    it comes up with a NullPointerException.
    If you are getting an error, copy and paste the full text of the message here. The error message has the souce code line number in it. Copy and paste the code around that line here also.

  3. #3
    mattj is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    No line number was given, if i replace

    Java Code:
    DefaultTableModel model = new DefaultTableModel() {
                Class [] classes = {int.class, String.class, String.class, Date.class, Date.class, double.class};
                    
                public Class getColumnClass(int column) {
                    return classes[column];
                }
            };
    with

    Java Code:
    DefaultTableModel model =new DefaultTableModel();
    The program works fine. Here is the exception.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JTable.prepareRenderer(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(Unkn own Source)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(Unk nown Source)
    at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
    at javax.swing.plaf.ComponentUI.update(Unknown Source)
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unkno wn Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent._paintImmediately(Unknown Source)
    at javax.swing.JComponent.paintImmediately(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Un known Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I was able to get the same exception by using int.class in a test app.
    Changing it to Integer.class worked okay.
    Apparently, autoboxing doesn't work here.
    Java Code:
    Class[] classes = {Integer.class, String.class, String.class,
                       Date.class, Date.class, Double.class};

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •