0

I have some values on a JTable that increases a value on some columns every second. I created two methods to get and set a cell value as shown below:

private Object[][] text = new Object[6][5];
private JTable tblProcesses = new JTable(text, header);

public Object getCell(int row, int col) {
    return text[row][col];
}

public void setCell(int row, int col, Object object) {
    this.text[row][col] = object;
    tblProcesses.updateUI();
}

The methods work, and I am using them on the controller (I am using MVC). On the controller I have defined a TimerTask that update the values every second:

TimerTask processTimer = new TimerTask() {

    @Override
    public void run() {

        mainTable.setCell(0, 1, (int) mainTable.getCell(0, 1) + 1);
        mainTable.setCell(1, 1, (int) mainTable.getCell(1, 1) + 1);
        mainTable.setCell(2, 1, (int) mainTable.getCell(2, 1) + 1);
        mainTable.setCell(3, 1, (int) mainTable.getCell(3, 1) + 1);
        mainTable.setCell(4, 1, (int) mainTable.getCell(4, 1) + 1);
        mainTable.setCell(5, 1, (int) mainTable.getCell(5, 1) + 1);
    }
};

When I run the program, I get what I expect, the table updates each value each second by 1, however, some times I get a NullPointerException. The JTable keeps updating without any problem and the program keeps running.

I wonder if Java is not able to get each cell value every second, so it throws a NullPointerExceptionevery, however what confuses me is the fact all the cells keep increasing its value by 1.

I have tried using a try catch but it still get the NullPointerException.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2186)
    at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2087)
    at java.desktop/javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1883)
    at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
    at java.desktop/javax.swing.JViewport.paint(JViewport.java:737)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
    at java.desktop/javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:246)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1890)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I would very much appreciate if someone could give me a hand figuring out how can I stop getting the NullPointerException.

  • 1
    1. TimerTask and java.util.Timer are the wrong Timer classes to use. You should be using javax.swing.Timer instead since this is Swing thread-safe. 2. You are asking about a NullPointerException (NPE) but not showing the basic information needed to debug any NPE question, including the stacktrace, an indication of the line throwing the exception or which variable is null. It's as if you asked this question without first investigating NPE's (including the canonical question that is usually the first hit on any search of this term). – Hovercraft Full Of Eels Oct 10 '21 at 18:54
  • Please post your NullPointerException stacktrace and show the line that is throwing it. The duplicate used to close this question will help you with this, but since your NPE is being throw intermittently, I suspect that your problem is a Swing threading issue due to your trying to mutate Swing state off of the event thread. Again, use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Hovercraft Full Of Eels Oct 10 '21 at 18:59
  • I'm sorry I forgot about the stacktrace, I'm editing the question to place it in. I have never heard about javax.swing.Timer, I'm going to take a look to it. – Smooth Researcher Oct 10 '21 at 19:05
  • Thank you for the edit, and it supported my assumption. The NPE is not being thrown directly from your own code but rather from within the Swing library itself and only there. This and that it occurs in an unpredictable and random manner argue *strongly* that this is a threading issue. Fix the issue by using the Swing Timer and let's see what happens. – Hovercraft Full Of Eels Oct 10 '21 at 19:08
  • Thank you @HovercraftFullOfEels!!! I used Swing Timer and now I don't get those NullPointerExceptions. Thank you so much! – Smooth Researcher Oct 10 '21 at 19:24
  • Great, glad that you have fixed it – Hovercraft Full Of Eels Oct 10 '21 at 20:17

0 Answers0