Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

newbie: getKeyStates()

Thread view: 
Jeff - 22 Dec 2005 01:11 GMT
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
zero - 22 Dec 2005 12:51 GMT
> Hey
>
[quoted text clipped - 9 lines]
> 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");
>     }
[quoted text clipped - 3 lines]
> 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) {
[quoted text clipped - 3 lines]
>                 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?

>     }
> }
[quoted text clipped - 3 lines]
> 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
[quoted text clipped - 15 lines]
> 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.

Signature

Beware the False Authority Syndrome

Jeff - 22 Dec 2005 15:06 GMT
Thank you for the tip about Exception:printStackTrace() , it took me 5
minutes to fix this problem

Jeff

>> Hey
>>
[quoted text clipped - 120 lines]
> them post a more complete (but short!) example - if you haven't found the
> solution yourself yet.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.