 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Petterson Mikael Guest
|
Posted: Mon Jan 23, 2006 1:04 pm Post subject: Threads and invokeLater issue |
|
|
Hi,
I have written a class that uses invokeLater() to update the gui.
However I get the following error when running it ( see below).
What is giving me a NullPointer?
Cheers,
//mikael
my numberOfThreads is in the same class:
private int numberOfThreads = 0;
This is the part that gives me the error:
| Quote: | /**
* Thread that checks number of threads in model.
*/
public void run() {
while (true) {
try {
int nrOfThreads = model.getNumberOfThreads();
Thread.sleep(50);
//Check if number of threads has changed
if (nrOfThreads != numberOfThreads) {
numberOfThreads = nrOfThreads;
final String number = new Integer(nrOfThreads).toString();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
view.getThreadsLabel().setText(
"Number of Threads is " + number);
}
});
}else{
//No need to update gui
}
} catch (InterruptedException ie) {
System.err.println("Thread has been interrupted");
}
}
}
|
This is the error trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ThreadCounter$1.run(ThreadCounter.java:3
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
|
|
| Back to top |
|
 |
Knute Johnson Guest
|
Posted: Mon Jan 23, 2006 4:47 pm Post subject: Re: Threads and invokeLater issue |
|
|
Petterson Mikael wrote:
| Quote: | Hi,
I have written a class that uses invokeLater() to update the gui.
However I get the following error when running it ( see below).
What is giving me a NullPointer?
Cheers,
//mikael
my numberOfThreads is in the same class:
private int numberOfThreads = 0;
This is the part that gives me the error:
/**
* Thread that checks number of threads in model.
*/
public void run() {
while (true) {
try {
int nrOfThreads = model.getNumberOfThreads();
Thread.sleep(50);
//Check if number of threads has changed
if (nrOfThreads != numberOfThreads) {
numberOfThreads = nrOfThreads;
final String number = new
Integer(nrOfThreads).toString();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
view.getThreadsLabel().setText(
"Number of Threads is " + number);
}
});
}else{
//No need to update gui
}
} catch (InterruptedException ie) {
System.err.println("Thread has been interrupted");
}
}
}
This is the error trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ThreadCounter$1.run(ThreadCounter.java:3
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
|
The 38 is a clue. But since I have no idea if line 38 is even in the
code you posted I'll have to guess. I would say that either view or the
method getThreadsLabel() is returning a null but it could also be model
and getNumberOfThreads().
And if you want to turn in int into a String use Integer.toString(int).
--
Knute Johnson
email s/nospam/knute/
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Mon Jan 23, 2006 7:44 pm Post subject: Re: Threads and invokeLater issue |
|
|
On Mon, 23 Jan 2006 14:04:47 +0100, Petterson Mikael
<mikael.petterson (AT) era (DOT) ericsson.se> wrote, quoted or indirectly quoted
someone who said :
| Quote: | final String number = new Integer(nrOfThreads).toString();
|
there are easier ways of doing that.
See http://mindprod.com/applets/converter.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Mon Jan 23, 2006 9:10 pm Post subject: Re: Threads and invokeLater issue |
|
|
"Thomas Hawtin" <usenet (AT) tackline (DOT) plus.com> wrote
| Quote: | Petterson Mikael wrote:
/**
* Thread that checks number of threads in model.
*/
public void run() {
while (true) {
try {
int nrOfThreads = model.getNumberOfThreads();
Thread.sleep(50);
//Check if number of threads has changed
if (nrOfThreads != numberOfThreads) {
numberOfThreads = nrOfThreads;
final String number = new
Integer(nrOfThreads).toString();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
view.getThreadsLabel().setText(
"Number of Threads is " + number);
}
});
}else{
//No need to update gui
}
} catch (InterruptedException ie) {
System.err.println("Thread has been interrupted");
}
If the thread has been interrupted, presumably that means it should stop.
So, move the try/catch outside of the loop.
}
}
|
Couldn't the InterruptedException have come from so called "spurrious
wakeups" that occasionally happen in Java? It looks like this code is
basically intended to eternally poll for the number of threads, and update
the GUI when that number has changed. If such a spurrious wakeup does
happen, it seems like the correct behaviour is indeed to go back to the top
of the while loop, and go back to sleep.
- Oliver
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Mon Jan 23, 2006 9:53 pm Post subject: Re: Threads and invokeLater issue |
|
|
"Thomas Hawtin" <usenet (AT) tackline (DOT) plus.com> wrote
| Quote: | Oliver Wong wrote:
Couldn't the InterruptedException have come from so called "spurrious
wakeups" that occasionally happen in Java? It looks like this code is
No.
The length of time may be inaccurate. "...subject to the precision and
accuracy of system timers and schedulers." But that will just mean
Thread.sleep returns early. Threads are not aloud to go around claiming to
be interrupted when they haven't been.
|
Sorry, I was thinking of the Object.wait() method. You are correct.
- Oliver
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|