Search code examples
javaswingswingworker

SwingWorker gives "Cannot find symbol" error when referring to class members


Link to a paste of the compilation errors together with the SSCE: http://pastebin.com/upYzbHN1

Filename is 'foo.java'. Compiled with 'javac foo.java'.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.List;

public class foo extends JFrame {
        HashMap<Integer,Thing> Things = new HashMap<Integer,Thing>();
        JTextPane jtp = new JTextPane();

        public void findThings() {
                SwingWorker<HashMap<Integer,Thing>,Thing> sw1 = new SwingWorker<HashMap<Integer,Thing>,Thing>() {
                        protected HashMap<Integer,Thing> doInBackground() {
                                HashMap<Integer,Thing> things = new HashMap<Integer,Thing>();
                                Thread.sleep(1000);
                                return things;
                        }

                        protected void process(List<Thing> chunks) {
                                for(Thing thing : chunks) {
                                        this.things.put(thing.id, thing);
                                        this.jtp.setText(String.valueOf(this.things.size()));
                                }
                        }
                };

                sw1.execute();
        }

        public foo() {
                super();
                setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JButton jbtn = new JButton("findThings()");
                jbtn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                findThings();
                        }
                });
                add(jbtn);
                this.jtp.setPreferredSize(new Dimension(300,300));
                add(this.jtp); 

                setLocationRelativeTo(null);
                pack();
                setVisible(true);
        }

        public static void foo(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                new foo();
                        }
                });
        }

        private class Thing {
                public Thing() {
                        id = 100;
                        name = "Thing's name";
                }

                Integer id = null;
                String name = null;
        }
}

Gives these compilation errors:

foo.java:21: error: cannot find symbol
                                        this.things.put(thing.id, thing);
                                            ^
  symbol: variable things
foo.java:22: error: cannot find symbol
                                        this.jtp.setText(String.valueOf(this.thi
ngs.size()));
                                                                            ^
  symbol: variable things
foo.java:22: error: cannot find symbol
                                        this.jtp.setText(String.valueOf(this.thi
ngs.size()));
                                            ^
  symbol: variable jtp
3 errors

Solution

  • Your instance Map is called Things (capital letter - wrong convention for variable naming by the way).

    You are referencing it by calling things (lowercase letter - variables should be named camelBack, so that's the right name to use).

    Edit

    Also as mentioned in other answers, this will refer to the worker thread, not to the Foo class instance.

    • Use Foo.this.things.
    • Change your declaration to:

      HashMap<Integer,Thing> things = new HashMap<Integer,Thing>(); // lowercased variable name

    • Change your class name to Foo

    • Check the Java coding conventions here.