Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help Passing Value From Spinner to Database

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Through a tutorial I made my first MySQL database which uses EditTexts, then can Add, update, delete and view the records. I've now replaced one of the EditTexts with a Spinner which shows my two values when I test the app. When I go to add the new record my app crashes and then i get this error below in Android Studio. I assume I am passing no value.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.Spinner.getSelectedItem()' on a null object reference
       at com.bassbuddy.bassbuddy.MainActivity$3.onClick(MainActivity.java:96)

Now, I'm not 100% sure about HOW to pass the value so I followed how the EditTexts worked and followed that as in the following:


public void addData(){
       buttonAdd.setOnClickListener(
               new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                    boolean isInserted = myDB.insertData(spinBass.getSelectedItem().toString(),                      
                               tvLure.getText().toString(),
                               tvColor.getText().toString());

                    if(isInserted == true)
                        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this, "Data NOT Inserted", Toast.LENGTH_LONG).show();
                   }
               }
       );
   }



Below is the spinner.

String[] fish = new String[] {"Largemouth", "Smallmouth"};
       Spinner fishSpin = (Spinner) findViewById(R.id.spinBass);
       ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, fish);
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       fishSpin.setAdapter(adapter);



If I need to post more code let me know. Any help would be great.
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message is referring to this line of code:



You'll notice it says

Android wrote:Attempt to invoke virtual method 'java.lang.Object android.widget.Spinner.getSelectedItem()' on a null object reference



And in that code you're calling the method getSelectedItem() on a variable named spinBass. So the error message says that variable is null, which is a problem. Somewhere you've got a Spinner in your GUI but that variable isn't it.
 
Maybe he went home and went to bed. And took this tiny ad with him:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic