This question already has an answer here:
Noob here, app keeps crashing when I start a certain activity, it has a dynamic spinner that can add options with a button and uses an SQLite database. Since I have near no experience most of it is copied from the internet and it doesn't show where the error is, so I have no idea what could be wrong. The part that opens that activity shouldn't be the problem because I have another one that uses basically the same code there and works. Here's the activity that keeps crashing:
public class decks extends AppCompatActivity {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_decks);
loadSpinnerData();
String currentspin = spinner.getSelectedItem().toString();
}
public boolean onCreateOptionsMenu(Menu menu){
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adap = ArrayAdapter.createFromResource(this, R.array.deck_array, android.R.layout.simple_spinner_dropdown_item);
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adap);
return true;
}
//adds a new option to the spinner
public void newDeck(View view) {
Spinner spinner = (Spinner) findViewById(R.id.spinner);
String label = ((EditText) findViewById(R.id.deckName)).getText().toString();
MyDatabaseHelper db = new MyDatabaseHelper(this);
if (label.trim().length() > 0) {
getApplicationContext();
db.insertLabel(label);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add(label);
spinnerAdapter.notifyDataSetChanged();
((EditText) findViewById(R.id.deckName)).setText("");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(findViewById(R.id.deckName).getWindowToken(), 0);
loadSpinnerData();
}
}
//starts another activity
public void startedit(View view){
loadSpinnerData();
Intent intent = new Intent(this, cards.class);
startActivity(intent);
}
private void loadSpinnerData() {
Spinner spinner = (Spinner) findViewById(R.id.spinner);
MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());
List<String> labels = db.getAllLabels();
ArrayAdapter<String> adap = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labels);
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adap);
}
}
There is already an object in the array it's referencing. Edit: Here's the stack trace from when the app crashes.
11-24 19:41:32.527 8127-8127/com.example.yugiohapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.yugiohapp, PID: 8127
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yugiohapp/com.example.yugiohapp.decks}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.yugiohapp.decks.onCreate(decks.java:30)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)