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");
	    }

}]

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");
	    }

}]

Well, t is null when you pass it, so the MySwingWorker constructor is receiving null as an argument:

MyForm Form;
		
	public MySwingWorker(MyForm Form) {
			this.Form = Form;
	}

so you are doing this:

MyForm Form;
		
	public MySwingWorker(MyForm Form) {
			this.Form = null;
	}

If you don't want to do that, you have to initialize your t object before line 36 when you pass it to the above constructor. So do something like this:

t = new MyForm ();

before line 36.

Well, t is null when you pass it, so the MySwingWorker constructor is receiving null as an argument:

............

If you don't want to do that, you have to initialize your t object before line 36 when you pass it to the above constructor. So do something like this:

t = new MyForm ();

before line 36.

but if i do that it starts to go into infinit loop and open frame and gets out of memory error ?!!

but if i do that it starts to go into infinit loop and open frame and gets out of memory error ?!!

t = new MyForm ();

Oh, I see, that line is inside your MyForm () constructor, so it's an infinite recursive call to your constructor. Every new object makes a new object so there is no "last object". You can just forget about t and use this instead. Replace:

msw = new MySwingWorker(t);

with

msw = new MySwingWorker(this);

and see if that works better.

commented: Nicely done =) +4

Thank you so MUCH !!!!!!

I really appreciate your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.