• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Help Passing Value From Spinner to Database  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!