aarelovich
aarelovich

Reputation: 5566

Null Pointer Exception on an Interface call on Android

I've got a class that extends from dialog like so:

interface DiagInterface{
void selectionDone(boolean accepted);
}

public class CardSelectionDialog extends Dialog {

private android.view.View.OnClickListener positive;
private android.view.View.OnClickListener negative;
private boolean AcceptState;
private DiagInterface ExtComm;

public CardSelectionDialog(Context context){
    super(context);

    AcceptState = false;

    positive = new android.view.View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            AcceptState = true;
            OnSelectionDone();
            dismiss();
        }
    };

    negative= new android.view.View.OnClickListener() {         
        @Override
        public void onClick(View v) {
            AcceptState = false;
            OnSelectionDone();
            dismiss();
        }
    };      

}

private void OnSelectionDone(){
    System.err.println("Before selection");
    ExtComm.selectionDone(AcceptState);
}

Then on my main class I have the following:

Here is where I call the dialog (MainActivity):

public void SelectCardDialog(){
    CardSelectionDialog diag = new CardSelectionDialog(this);
    CardDeck deck = new CardDeck();
    deck.addNOfCard(CardType.WOUNDCARD, 30, this);
    diag.setSelection(deck);
    diag.show();
}

And this is the implementation of the the interfase:

@Override
public void selectionDone(boolean accepted){
    if (accepted){
        System.err.println("ACCEPTED");
    }
    else{
        System.err.println("REJECTED");
    }       
}

However whenever I pressed any of the buttons I get the following Error (the Before selection message appears)

01-01 16:59:42.180: E/AndroidRuntime(29810): java.lang.NullPointerException
01-01 16:59:42.180: E/AndroidRuntime(29810):    at legen.dary.CardSelectionDialog.OnSelectionDone(CardSelectionDialog.java:106)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at legen.dary.CardSelectionDialog.access$1(CardSelectionDialog.java:103)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at legen.dary.CardSelectionDialog$1.onClick(CardSelectionDialog.java:35)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at android.view.View.performClick(View.java:4084)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at android.view.View$PerformClick.run(View.java:16966)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at android.os.Handler.handleCallback(Handler.java:615)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at android.os.Looper.loop(Looper.java:137)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at java.lang.reflect.Method.invokeNative(Native Method)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at java.lang.reflect.Method.invoke(Method.java:511)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-01 16:59:42.180: E/AndroidRuntime(29810):    at dalvik.system.NativeStart.main(Native Method)

Any ideas of what I'm doing wrong?

Thank you very much.....

Upvotes: 0

Views: 1890

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

You have not initialized

 private DiagInterface ExtComm;

You probably want

public CardSelectionDialog(Context context){
super(context); 
ExtComm= (DiagInterface)context;
AcceptState = false;
}

Also you may want to move the below outside the constructor

 positive = new android.view.View.OnClickListener() {            
    @Override
    public void onClick(View v) {
        AcceptState = true;
        OnSelectionDone();
        dismiss();
    }
 };

negative= new android.view.View.OnClickListener() {         
    @Override
    public void onClick(View v) {
        AcceptState = false;
        OnSelectionDone();
        dismiss();
    }
};      

Upvotes: 1

marcinj
marcinj

Reputation: 49986

I suppose this NPE exception is comming from this line:

ExtComm.selectionDone(AcceptState);

probably ExtComm is null

Upvotes: 0

Related Questions