java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rishabh.casino/com.example.rishabh.casino.Game}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
Main problem is You are initializing your spinner above your view.
You should put setContentView(R.layout.activity_game);
on top and after that you should initialize other components. Correct your onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_game); //Initialize layout first
spin=(Spinner)findViewById(R.id.spinner);
rembal=(TextView)findViewById(R.id.textView8);
bet_amount=(TextView)findViewById(R.id.textView9);
beton=(TextView)findViewById(R.id.textView10);
result=(TextView)findViewById(R.id.textView11);
bet=(EditText)findViewById(R.id.editText2);
okgen=(Button)findViewById(R.id.button4);
ArrayAdapter<String> adapter_option=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,options);
adapter_option.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_option);
spin.setOnItemSelectedListener(this);
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
This is the reason behind null pointer exception. View is not initialize yet and you are trying to initialize spinner inside view.
From the error log looks like your spinner object is null...
this can be because you haven't initialized it (with findViewById("...")) or you did that but in one of latter calls...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With