• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Basic Question: Spinner not found

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I started reading book head first Android. In the beginnings, I'm learning to get an item selected in list from Spinner in display in text view after clicking on a button. Below is my code. I'm getting error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.widget.Spinner.getSelectedItemId()' on a null object reference

findViewById is not listing my spinner in R.id.

Please suggest where am going wrong.

My Main Activity



 
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should initialize spinner and textView below setContentView.

 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:You should initialize spinner below setContentView.



Hello Thank you for your replay. This is still not working. In the android studio, if you put R.id and dot, it should give list of ids. In the attached image you can see, it's not happening.
Screen-Shot-2021-03-07-at-7.53.29-PM.png
[Thumbnail for Screen-Shot-2021-03-07-at-7.53.29-PM.png]
Screen-Shot-2021-03-07-at-7.53.49-PM.png
[Thumbnail for Screen-Shot-2021-03-07-at-7.53.49-PM.png]
 
Randy Tong
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declare textView and spinner as global value.

 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I just tried lie you mentioned. I'm still getting the error.  And same null pointer exception
Screen-Shot-2021-03-07-at-8.27.35-PM.png
[Thumbnail for Screen-Shot-2021-03-07-at-8.27.35-PM.png]
 
Randy Tong
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please show all your import class too.
 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:please show all your import class too.




Here they are

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
 
Randy Tong
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is error persist after you write R.id.action_bar_spinner?
 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:Is error persist after you write R.id.action_bar_spinner?



Yes.. Same null poinnter exception.
 
Randy Tong
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any red line error in line R.id.action_bar_spinner ?
 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:any red line error in line R.id.action_bar_spinner ?



There is no red line. I just did "invalidate caches and restart" and then it worked. I did this before. But it didn't work. It's working now. Weird!

How does declaring the button or spinner is different from directly declaring them in the onClickFindBeer(View view) method?
 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dreke Droga wrote:

Randy Tong wrote:any red line error in line R.id.action_bar_spinner ?



There is no red line. I just did "invalidate caches and restart" and then it worked. I did this before. But it didn't work. It's working now. Weird!

How does declaring the button or spinner is different from directly declaring them in the onClickFindBeer(View view) method?



It's now working but for the spinner values it's giving value 0, 1 etc based on selection instead of


In the xml I gave


 
Dreke Droga
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. I have to put  getSelectedItem() instead of getSelectedItemId()


Thank you very much for replying
 
Randy Tong
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dreke Droga wrote:How does declaring the button or spinner is different from directly declaring them in the onClickFindBeer(View view) method?


Because the view in the button is the View that was clicked, not the view of the layout.

https://developer.android.com/guide/topics/ui/controls/button


 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic