newbie: getKeyStates()

J

Jeff

Hey

midp 2.0
J2ME Wireless Toolkit 2.2
JDK 5


I get an NullPointerException when running the code (copy of code added
below, data output also added under the code), and I don't know what is
causing this exception... If you see what I'm doing wrong here, then please
tell me

public void checkKeys() {
int keyState = getKeyStates();
System.out.println(keyState);
if ((keyState & LEFT_PRESSED) != 0) {
System.out.println("LEFT_PRESSED");
}
}
***************************
Here is the thread code, you see that checkKeys are called in the loop
public void run() {
testCanvas.flushKeys();
testPause = false;
testStop = false;
while (true) {
if (testStop) {
break;
}
while (testPause) {
synchronized(this) {
try {
wait();
}
catch (Exception e) {
}
}
synchronized(this) {
try {
wait(1);
}
catch(Exception e) {}
}
}
testCanvas.checkKeys();
testCanvas.advance();
}
}

The output from this code is (as you can see from the output that this
output contain data from more than one iteraton):
I did click only 1 time on LEFT_PRESSED (on emulator) and it looks like the
loop are processing my click several times (getKeyStates= 4, 4, 0, 0):
4
LEFT_PRESSED
java.lang.NullPointerException
4
LEFT_PRESSED
java.lang.NullPointerException
0
0
0
-1
LEFT_PRESSED
java.lang.NullPointerException
-1
LEFT_PRESSED
java.lang.NullPointerException
-1
LEFT_PRESSED
java.lang.NullPointerException

I'm very thankful for help on this issue!

Jeff
 
Z

zero

Hey

midp 2.0
J2ME Wireless Toolkit 2.2
JDK 5


I get an NullPointerException when running the code (copy of code
added below, data output also added under the code), and I don't know
what is causing this exception... If you see what I'm doing wrong
here, then please tell me

public void checkKeys() {
int keyState = getKeyStates();

what does getKeyStates do?
System.out.println(keyState);
if ((keyState & LEFT_PRESSED) != 0) {

how is LEFT_PRESSED defined?
System.out.println("LEFT_PRESSED");
}
}
***************************
Here is the thread code, you see that checkKeys are called in the loop
public void run() {
testCanvas.flushKeys();

what is testCanvas? What does flushKeys do?
testPause = false;
testStop = false;
while (true) {
if (testStop) {
break;
}

unless you have a very good reason, while(true) and break; are usually
not a good idea.

while(!testStop)
{
// ...
}
while (testPause) {
synchronized(this) {
try {
wait();
}
catch (Exception e) {
}

please don't drop exceptions like this. Especially not *all* exceptions,
as you are doing here.

catch(InterruptedException e)
{
// ...
}
}
synchronized(this) {
try {
wait(1);
}
catch(Exception e) {}

see above

a better, consistent indentation style will make your code much easier to
read and maintain.
testCanvas.checkKeys();
testCanvas.advance();

what does advance do?
}
}

The output from this code is (as you can see from the output that this
output contain data from more than one iteraton):
I did click only 1 time on LEFT_PRESSED (on emulator) and it looks
like the loop are processing my click several times

why is there a loop anyway? How does testStop ever get set to true? In
this code snippet it doesn't.
(getKeyStates= 4,
4, 0, 0): 4
LEFT_PRESSED
java.lang.NullPointerException
4
LEFT_PRESSED
java.lang.NullPointerException
0
0
0
-1
LEFT_PRESSED
java.lang.NullPointerException
-1
LEFT_PRESSED
java.lang.NullPointerException
-1
LEFT_PRESSED
java.lang.NullPointerException

It looks like you're catching this exception somewhere, and then only
printing its name. If you use Exception:printStackTrace() you'll see the
exact line number that causes the exception. For a NullPointerException
it's very easy once you know what line throws the exception: it means one
of the objects on that line is null - most likely because it was never
instatiated.

From this code snippet there is no way to tell what is going wrong
exactly. I suggest you look at the notes above, and after correcting
them post a more complete (but short!) example - if you haven't found the
solution yourself yet.
 
J

Jeff

Thank you for the tip about Exception:printStackTrace() , it took me 5
minutes to fix this problem

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top