views:

154

answers:

2

Hey guys I'm having trouble with a spiner throwing a nullPointerExpression I'm sure I'm missing something stupid?

Thanks for you help

here is my code:

public class main extends Activity { /** Called when the activity is first created. */

public TextView strCurrency;
public TextView strCurrencyOUT;
Button butCalc;
private Spinner spinner; 
private static final String[] array = { "AUD", "CAD", "USD", "GBP",};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    strCurrency = (TextView) this.findViewById(R.id.txtCurrency);
    strCurrencyOUT = (TextView) this.findViewById(R.id.txtOUT);

    butCalc = (Button) this.findViewById(R.id.butCalc);
    butCalc.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});

    Spinner spinner = (Spinner) findViewById(R.id.widget28);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, array);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

public void calculate() {

    String   str = (String) spinner.getSelectedItem();
            strCurrencyOUT.setText(str);

W/KeyCharacterMap( 205): No keyboard for id 0 W/KeyCharacterMap( 205): Using default keymap: /system/usr/keychars/qwerty.kcm.bin D/AndroidRuntime( 205): Shutting down VM W/dalvikvm( 205): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) E/AndroidRuntime( 205): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime( 205): java.lang.NullPointerException E/AndroidRuntime( 205): at biz.thorley.Currency.main.calculate(main.java:58) E/AndroidRuntime( 205): at biz.thorley.Currency.main$1.onClick(main.java:40) E/AndroidRuntime( 205): at android.view.View.performClick(View.java:2344) E/AndroidRuntime( 205): at android.view.View.onTouchEvent(View.java:4133) E/AndroidRuntime( 205): at android.widget.TextView.onTouchEvent(TextView.java:6510) E/AndroidRuntime( 205): at android.view.View.dispatchTouchEvent(View.java:3672) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712) E/AndroidRuntime( 205): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202) E/AndroidRuntime( 205): at android.app.Activity.dispatchTouchEvent(Activity.java:1987) E/AndroidRuntime( 205): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696) E/AndroidRuntime( 205): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658) E/AndroidRuntime( 205): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 205): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 205): at android.app.ActivityThread.main(ActivityThread.java:4203) E/AndroidRuntime( 205): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 205): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime( 205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) E/AndroidRuntime( 205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) E/AndroidRuntime( 205): at dalvik.system.NativeStart.main(Native Method) I/Process ( 53): Sending signal. PID: 205 SIG: 3 I/dalvikvm( 205): threadid=7: reacting to signal 3 E/dalvikvm( 205): Unable to open stack trace file '/data/anr/traces.txt': Permission denied I/Process ( 205): Sending signal. PID: 205 SIG: 9 I/ActivityManager( 53): Process biz.thorley.Currency (pid 205) has died. I/WindowManager( 53): WIN DEATH: Window{438d8e38 biz.thorley.Currency/biz.thorley.Currency.main paused=false} W/UsageStats( 53): Unexpected resume of com.android.launcher while already resumed in biz.thorley.Currency W/InputManagerService( 53): Got RemoteException sending setActive(false) notification to pid 205 uid 10023

A: 

Before looking deeper into the code: it doesn't have anything to do with the spare comma in your "array", does it?

Please also post a StackTrace.

Select0r
No it is not that, I removed a few elements from the array to make it more readable in the post that is how that extra , got in there.
stephen
Let's see your StackTrace then...
Select0r
+2  A: 

Here is the problem in your oncreate() method:

Spinner spinner = (Spinner) findViewById(R.id.widget28);

You assign the result to spinner which is a local variable/reference here. main.spinner class field is still null. This is why you have NullPointerException in calculate() method.

Replace the line above with:

this.spinner = (Spinner) findViewById(R.id.widget28);
radek-k
cool thanks that did the trick
stephen