Why is SwingWorker not returning an object to the EDT?
<p>I am using the <code>SwingWorker</code> class to perform a background task. Many different components of the <code>GUI</code> get updated after the background thread has done is job (these are in the <code>done()</code> method). The <code>doInBackground()</code> method publishes a <code>HeatMap</code> class object and the <code>process()</code> method adds it to a <code>JPanel</code> component. I have added <code>MouseListener</code>and <code>MouseMotionListener</code> to this <code>Heatmap</code> class object. The <code>mouseMoved()</code> method is present in the main GUI class. When the mouse is moved, the coordinate position of the mouse on the <code>HeatMap</code> should be displayed about it in a <code>JLabel</code>.</p>

<p>When I run the code, the <code>HeatMap</code> object is visible in the <code>JPanel</code>, but I think the <code>EDT</code> does not have access to it. This because, on a quick check I found out that the <code>rawIntensityMap</code> <code>HeatMap</code> object is not <code>null</code> in the <code>process()</code> method of the <code>SwingWorker</code>, but it is still <code>null</code> in the <code>mouseMoved()</code> method, due to which I get a <code>NullPointerException</code>.</p>

<p>The <code>HeatMap</code> object in the <code>GUIMain</code> class and the <code>SwingWorker</code> class have been declared as:</p>

<pre><code>private HeatMap rawIntensityMap = null;
</code></pre>

<p>I do not send the <code>rawIntensityMap</code> object to the <code>SwingWorker</code> class constructor. I had tried that earlier but it did not work. </p>

<p>Here is the <code>process()</code> method from the <code>SwingWorker</code> class:</p>

<pre><code> @Override
protected void process(List&lt;HeatMap&gt; chunks) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            imagePanel.add(rawIntensityMap, BorderLayout.CENTER);
            coordinates.setBounds(31, 31, rawIntensityMap.getWidth() - 31, rawIntensityMap.getHeight() - 31);
        }
    });

    }
</code></pre>

<p>Here is the <code>mouseMoved()</code> method:</p>

<pre><code>@Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("I am in the mouseevent" + coordinates.toString());
        System.out.println("Width of raw Intensity map: " + rawIntensityMap.getWidth());
        if (e.getPoint().x &gt;= 31 &amp;&amp; e.getPoint().y &gt;= 31 &amp;&amp; e.getPoint().x &lt;= rawIntensityMap.getWidth() - 31 &amp;&amp; e.getPoint().y &lt;= rawIntensityMap.getHeight() - 31) {
            rawIntensityMap.removeAll();
            rawIntensityMap.add(coordinates);
            coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
            if (peakPickedImage.isSelected()) {
                preprocessedIntensityMap.add(coordinates);
                coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
                coordinates.revalidate();
                coordinates.repaint();
            }
            coordinates.revalidate();
            coordinates.repaint();
        }
    }
</code></pre>

<p>Here is the basic structure of my <code>SwingWorker</code> Class:</p>

<pre><code>public class FileReadWorker extends SwingWorker&lt;REXP, HeatMap&gt; {

 public FileReadWorker(GUIMain guiClassObject, File fileName, JTree rawSpectraTree, DefaultTreeModel model, DefaultMutableTreeNode root, String currentPath, JTextField minMz, JTextField maxMz, JFreeChart spectrumPlot, ChartPanel chartPanel, JPanel chartContent, float minMzValue, float maxMzValue, Float globalMinMz, Float globalMaxMz, JLabel statusLabel, JPanel imagePanel, JLabel coordinates, JTabbedPane tabbedSpectralFiles, JScrollPane spectralFilesScrollPane, JPanel rawFilesPanel, JRadioButton rawImage, JRadioButton peakPickedImage, JMenuItem loadPeakListMenuItem, JButton loadPeaklistsButton, JMenuItem propertiesMenuItem, JButton propertiesButton) {
        this.guiClassObject = guiClassObject;
        this.fileName = fileName;
        this.rawSpectraTree = rawSpectraTree;
        this.currentPath = currentPath;
        this.minMz = minMz;
        this.maxMz = maxMz;
        this.spectrumPlot = spectrumPlot;
        this.chartPanel = chartPanel;
        this.chartContent = chartContent;
        this.minMzValue = minMzValue;
        this.maxMzValue = maxMzValue;
        this.GlobalMinMz = globalMinMz;
        this.GlobalMaxMz = globalMaxMz;
        this.statusLabel = statusLabel;
        this.imagePanel = imagePanel;
        this.coordinates = coordinates;
        this.tabbedSpectralFiles = tabbedSpectralFiles;
        this.spectralFilesScrollPane = spectralFilesScrollPane;
        this.rawFilesPanel = rawFilesPanel;
        this.rawImage = rawImage;
        this.peakPickedImage = peakPickedImage;
        this.loadPeakListMenuItem = loadPeakListMenuItem;
        this.loadPeaklistsButton = loadPeaklistsButton;
        this.propertiesMenuItem = propertiesMenuItem;
        this.propertiesButton = propertiesButton;
        this.model = model;
        this.root = root;
    }

@Override
    protected REXP doInBackground() throws Exception {

// does some background tasks

// Works on the generating the HeatMap

try {
            rawIntensityMap = gim.generateIntensityMap(rawSpectrumObjects, currentPath, minMzValue, maxMzValue, Gradient.GRADIENT_Rainbow, "RAW");
            publish(rawIntensityMap);
        } catch (RserveException e) {
            e.printStackTrace();
        } catch (REXPMismatchException e) {
            e.printStackTrace();
        }

// returns a REXP object
        return rawSpectrumObjects;
    }

  @Override
    public void done() {

// Updates different components of the GUI
rawIntensityMap.addMouseListener(guiClassObject);
        rawIntensityMap.addMouseMotionListener(guiClassObject);
}


}

}
</code></pre>

<p>Can someone point at the error here?</p>

<p><strong>SOME MORE CODE:</strong></p>

<p>This is my <code>GUIMain</code> class where <code>HeatMap rawIntensityMap</code> is declared. Also, I have pasted parts of the code where this <code>HeatMap</code> object is actually used (have not pasted all the methods).</p>

<pre><code>    public class GUIMain extends JFrame implements ActionListener, ItemListener, MouseListener, MouseMotionListener, ChangeListener {

    volatile HeatMap rawIntensityMap;
private JPanel imagePanel; // container for the HeatMap


 /**
     * Constructor to setup the GUI
     */
    public GUIMain(String title) {

        super(title);

        setLayout(new BorderLayout());
        //getSize();
        setSize(getSize());

 imagePanel = new JPanel(new BorderLayout());
        g.gridx = 0;
        g.gridy = 1;
        g.gridwidth = 2;
        g.weightx = 1.0; // fill the rest of the space
        g.weighty = 1.0;
        g.fill = GridBagConstraints.BOTH;

        imagePanel.setBorder(BorderFactory.createEtchedBorder());
        //imagePanel.addMouseListener(this);

        imageDisplay.add(imagePanel, g);
        //  ImageDisplay.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

        imageDisplay.setBorder(
                BorderFactory.createCompoundBorder(
                        BorderFactory.createTitledBorder("View 2-D ion intensity map"),
                        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

}

public void actionPerformed(ActionEvent e) {

        //Handle open *.img imaging file button and menu item action

        if ((e.getSource() == OpenImagingFileButton) || (e.getSource() == loadRawSpectraMenuItem)) {
            int returnVal = fcImg.showOpenDialog(GUIMain.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fcImg.getSelectedFile();

                root = new DefaultMutableTreeNode(file);
                rawSpectraTree = new JTree(root);
                model = (DefaultTreeModel) rawSpectraTree.getModel();

    //Passing this HeatMap to the SwingWorker class

    FileReadWorker frw = new FileReadWorker(this, file, rawSpectraTree, rawIntensityMap, model, root, currentPath, minMz, maxMz, spectrumPlot, chartPanel, chartContent, minMzValue, maxMzValue, GlobalMinMz, GlobalMaxMz, statusLabel, imagePanel, coordinates, tabbedSpectralFiles, spectralFilesScrollPane, rawFilesPanel, rawImage, peakPickedImage, loadPeakListMenuItem, loadPeaklistsButton, propertiesMenuItem, propertiesButton);
                    frw.execute();

}


    // Method when a different HeatMap color gradient is selected
    @Override
        public void itemStateChanged(ItemEvent e) {
            colorNumber = (Integer) e.getItem();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                rawIntensityMap.updateGradient(gradients[colorNumber]);
                if (peakPickedImage.isEnabled()) {
                    preprocessedIntensityMap.updateGradient(gradients[colorNumber]);
                }
            }

        }

    // Mouse moved event

     @Override
        public void mouseMoved(MouseEvent e) {
            if(rawIntensityMap == null)
                System.out.println("TRUE**");
            else
                System.out.println("FALSE**");
            System.out.println("I am in the mouseevent" + coordinates.toString());
            if (e.getPoint().x &gt;= 31 &amp;&amp; e.getPoint().y &gt;= 31 &amp;&amp; e.getPoint().x &lt;= rawIntensityMap.getWidth() - 31 &amp;&amp; e.getPoint().y &lt;= rawIntensityMap.getHeight() - 31) {
                rawIntensityMap.removeAll();
                rawIntensityMap.add(coordinates);
                coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
                if (peakPickedImage.isSelected()) {
                    preprocessedIntensityMap.add(coordinates);
                    coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
                    coordinates.revalidate();
                    coordinates.repaint();
                }
                coordinates.revalidate();
                coordinates.repaint();
            }
        }

    }
</code></pre>

<p>The code from the <code>SwingWorker</code> class is already pasted above.</p>