uncaught NullPointerException with keyPressed

I am using a 2.0.3 Mac version, and I get an uncaught exception raised when I try to listen for keyPressed. I haven't seen this before in the previous versions, and I wondered what does this mean? Could someone explain this?

void keyPressed(){
  if(key == 'a'){
    println("a!");
  }else if(key == 'z'){
   println("z");
  }
}

When I press a or z, I get the following errors:

013-11-05 10:44:46.049 java[2213:2437] An uncaught exception was raised
2013-11-05 10:44:46.049 java[2213:2437] java.lang.NullPointerException
2013-11-05 10:44:46.053 java[2213:2437] (
    0   CoreFoundation                      0x00007fff8456c41c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8b326e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8456bfc9 -[NSException raise] + 9
    3   JavaNativeFoundation                0x0000000102cd4457 JNFCallObjectMethod + 212
    4   libawt.jnilib                       0x000000010c542511 -[NSViewAWT attributedSubstringForProposedRange:actualRange:] + 78
    5   AppKit                              0x00007fff8b8a981d -[NSTextInputContext handleTSMEvent:] + 3476
    6   AppKit                              0x00007fff8b8a8a65 _NSTSMEventHandler + 205
    7   HIToolbox                           0x00007fff8502b6d4 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 892
    8   HIToolbox                           0x00007fff8502ac87 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 385
    9   HIToolbox                           0x00007fff8503ed90 SendEventToEventTarget + 40
    10  HIToolbox                           0x00007fff850800ed SendTSMEvent + 33
    11  HIToolbox                           0x00007fff8522d71f SendTextInputEvent + 1028
    12  HIToolbox                           0x00007fff852788ec -[IMKInputSession _copyUniCharsForRange:intoBuffer:ofLength:] + 268
    13  HIToolbox                           0x00007fff852789bb -[IMKInputSession _coreAttributesFromRange:whichAttributes:actualRange:] + 137
    14  HIToolbox                           0x00007fff85279aac -[IMKInputSession attributedSubstringFromRange:] + 89
    15  CoreFoundation                      0x00007fff84457dec __invoking___ + 140
    16  CoreFoundation                      0x00007fff84457c54 -[NSInvocation invoke] + 308
    17  CoreFoundation                      0x00007fff844fa7a6 -[NSInvocation invokeWithTarget:] + 54
    18  CoreFoundation                      0x00007fff844ca384 ___forwarding___ + 452
    19  CoreFoundation                      0x00007fff844ca138 _CF_forwarding_prep_0 + 120
    20  CoreFoundation                      0x00007fff84457dec __invoking___ + 140
    21  CoreFoundation                      0x00007fff84457c54 -[NSInvocation invoke] + 308
    22  Foundation                          0x00007fff8c883c67 -[NSConnection dispatchInvocation:] + 135
    23  Foundation                          0x00007fff8c88392d -[NSConnection handleRequest:sequence:] + 1434
    24  Foundation                          0x00007fff8c82acf0 -[NSConnection handlePortCoder:] + 652
    25  Foundation                          0x00007fff8c82a628 -[NSConnection dispatchWithComponents:] + 50
    26  Foundation                          0x00007fff8c80eed8 __NSFireMachPort + 257
    27  CoreFoundation                      0x00007fff8449dd04 __CFMachPortPerform + 388
    28  CoreFoundation                      0x00007fff8449db69 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    29  CoreFoundation                      0x00007fff8449dade __CFRunLoopDoSource1 + 478
    30  CoreFoundation                      0x00007fff8448ebd6 __CFRunLoopRun + 1830
    31  CoreFoundation                      0x00007fff8448e275 CFRunLoopRunSpecific + 309
    32  Foundation                          0x00007fff8c82700e -[NSConnection sendInvocation:internal:] + 1965
    33  CoreFoundation                      0x00007fff844ca384 ___forwarding___ + 452
    34  CoreFoundation                      0x00007fff844ca138 _CF_forwarding_prep_0 + 120
    35  HIToolbox                           0x00007fff85274b8d -[IMKInputSession handleEvent:] + 946
    36  HIToolbox                           0x00007fff852382e8 IMKInputSessionProcessEventRef + 45
    37  HIToolbox                           0x00007fff852378aa InputMethodInstanceProcessEventRef + 100
    38  HIToolbox                           0x00007fff8522ce6f TSMEventToInputMethod + 131
    39  HIToolbox                           0x00007fff8507f87c TSMKeyEvent + 283
    40  HIToolbox                           0x00007fff8506c678 TSMProcessRawKeyEvent + 3584
    41  AppKit                              0x00007fff8b8a82b0 -[NSTextInputContext handleEvent:] + 731
    42  AppKit                              0x00007fff8b887b5d -[NSView interpretKeyEvents:] + 180
    43  libawt.jnilib                       0x000000010c52aecc -[NSViewAWT keyDown:] + 102
    44  AppKit                              0x00007fff8b85481b -[NSWindow sendEvent:] + 1843
    45  libawt.jnilib                       0x000000010c51425c -[CocoaAppWindow sendEvent:] + 185
    46  AppKit                              0x00007fff8b7f5ca2 -[NSApplication sendEvent:] + 3395
    47  AppKit                              0x00007fff8b645a29 -[NSApplication run] + 646
    48  libawt.jnilib                       0x000000010c4eef30 +[AWTStarter startAWT:] + 1495
    49  libawt.jnilib                       0x000000010c4ee8aa -[CPerformer perform] + 93
    50  Foundation                          0x00007fff8c8080de __NSThreadPerformPerform + 229
    51  CoreFoundation                      0x00007fff8449d8f1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    52  CoreFoundation                      0x00007fff8448f062 __CFRunLoopDoSources0 + 242
    53  CoreFoundation                      0x00007fff8448e7ef __CFRunLoopRun + 831
    54  CoreFoundation                      0x00007fff8448e275 CFRunLoopRunSpecific + 309
    55  java                                0x0000000102403843 java + 18499
    56  java                                0x000000010240329a java + 17050
    57  java                                0x0000000102400a98 java + 6808
    58  ???                                 0x000000000000000f 0x0 + 15
)

Answers

  • I got same exception raised when I try to listen for keyPressed, too. I am using a 2.1 on OSX 10.9.

  • edited December 2013

    Try changing the code a bit.

    Example:


    void setup(){
    size(200,  200);
    background(0);
    }
    void draw(){
    background(0);
    }
    void keyPressed(){
    println(key);
    }
    

    Do let me know if the above example produces the same kind of error.

    Jack

  • edited December 2013

    I do still get the same error. the code inside the keyPressed() seems to work but I am still getting the same error message in the console. How can I get more information about this error?

    I am also using a 2.1 on OSX 10.9.

  • I found one work around. I have gotten this error no longer with Processing 2.1 which I build from source code in GitHub with JDK 1.7.0_45.

Sign In or Register to comment.