i have two classes one is MyForm and the other one is MySwingWorker, i get a null exception which is related to line 36, i'm not sure what i have to pass to MySwingWorker class. please help!!!!
public class MyForm extends JFrame {
public static void main(String[] args) {
new MyForm();
}
JTextField InputBox;
JButton SendButton;
JTextArea TraceBox;
public MyForm() {
InputBox = new JTextField("Hi, there!");
InputBox.setPreferredSize(new Dimension(200, 20));
SendButton = new JButton("Send");
SendButton.setPreferredSize(new Dimension(100, 20));
SendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
SendButton_Click();
}
});
JPanel strip = new JPanel(new FlowLayout(FlowLayout.CENTER));
strip.add(InputBox);
strip.add(SendButton);
this.getContentPane().add(strip, BorderLayout.NORTH);
TraceBox = new JTextArea();
JScrollPane scroll = new JScrollPane(TraceBox);
this.getContentPane().add(scroll, BorderLayout.CENTER);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 400);
this.setResizable(true);
this.setVisible(true);
//not sure what i have to pass instead of "t". I get a null exception
msw = new MySwingWorker(t);
msw.execute();
java.util.List<Integer> numbers = new java.util.ArrayList<Integer>();
try {
numbers = msw.get();
} catch (InterruptedException ex) {
;
} catch (ExecutionException ex) {
;
}
for (Integer i : numbers) {
TraceBox.append("... get: "+i+"\r\n");
}
}
MyForm t;
MySwingWorker msw;
public void SendButton_Click() {
TraceBox.setText(TraceBox.getText() + InputBox.getText() + "\r\n");
}
}
and the swingWorker codes are
public class MySwingWorker extends SwingWorker<java.util.List<Integer>, Integer> {
MyForm Form;
public MySwingWorker(MyForm Form) {
this.Form = Form;
}
@Override
public java.util.List<Integer> doInBackground() {
java.util.List<Integer> list = new java.util.ArrayList<Integer>();
for (int i=10; i<100; i+=10) {
super.publish(new Integer(i));
list.add(new Integer(i*10));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
;
}
}
return list;
}
@Override
protected void process(java.util.List<Integer> numbers) {
for (Integer i : numbers) {
Form.TraceBox.append("... process: "+i+"\r\n");
}
}
@Override
protected void done() {
Form.TraceBox.append("... done!\r\n");
}
}]