user1432223
user1432223

Reputation: 1

Call a JList method from a thread

The following code is in progress:

private final Controller controller = new Controller();
private final XSimpleList lists[] = new XSimpleList[2]; //extends JList (my framework)

I have a public class X which extends JFrame and implements Runnable. This public class X have a defined constructor. The last line in the constructor is:

(new Thread(new X())).start();

As required I do have a public void run() {}. In this run() method I do have a

lists[0].FillUp(controller.getSomeData(), 2);

The thread is programmed to execute the above line when it received a messages from a udp server. So... the problem is when the given line is executed (runtime). It results in a java.lang.NullPointerException.

XSimpleList code segments:

final JList hW;
final DefaultListModel dlm;
final JScrollPane lsp;

public final void FillUp(MyTypeRepository volume, int filter) {
    dlm.clear();
    //iterations
    dlm.addElement(some_data);
    //end iterations
}

My question is... How I can solve this issue in order the lists[0] not to return that NullPointer when called from the started Thread?

When executed (only) from the class X the issue is not present. I think is a problem with sharing data between 2 "pipes", meaning the parent instance (class x) and the child instance (thread y).

Thank You!

Upvotes: 0

Views: 114

Answers (1)

Himanshu Mohta
Himanshu Mohta

Reputation: 1043

lists[0] has NULL because
private final XSimpleList lists[] = new XSimpleList[2] creates an array with NULL values Like:
  lists[0] = NULL;
  lists[1] = NULL;

You should write code as:
private final XSimpleList lists[] = new XSimpleList[2];
lists[0] = new XSimpleList();
lists[1] = new XSimpleList();

Upvotes: 1

Related Questions