0

Good Evening,
so I am out of clue. I'm getting a
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
Basically I know null object reference means that something is not assigned (Yes, I am new to programming!).

        Spinner spinner = findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.palWerte, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter); //here the error happens
    spinner.setOnItemSelectedListener(this);

[...]

    @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_kaz:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new KalorienzählerFragment()).commit();
            break;
        case R.id.nav_kar:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new KalorienrechnerFragment()).commit();
            break;
        case R.id.nav_erp:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ErnährungsplanFragment()).commit();
            break;
        case R.id.nav_trp:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new TrainingsplanFragment()).commit();
            break;
        case R.id.nav_trh:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new TrainingshilfeFragment()).commit();
            break;
        case R.id.nav_ala:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new AlarmFragment()).commit();
            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
  • I think `findViewById` is returning null instead of an object. Does an object with id equals to **R.id.spinner1** exist? – Alessandro Da Rugna Jan 19 '19 at 21:21
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Alessandro Da Rugna Jan 19 '19 at 21:22
  • Spinner is defined as "spinner1" in fragment_kalorienrechner.xml.. –  Jan 19 '19 at 21:41

3 Answers3

1

Your code:Spinner spinner = findViewById(R.id.spinner1);

This should be Spinner spinner = (Spinner)findViewById(R.id.spinner1);

Or if it is in a fragment

builder.setView(fragmentView);

(Spinner)fragmentView.findViewById(R.id.spinner1);

Hope this help!

Zrufy
  • 303
  • 6
  • 18
mackerel
  • 111
  • 1
  • 7
0

It is looks like findViewById(R.id.spinner1); is returning null. Please, try to check a layout in setContentView function in your activity, your Spinner must defined in the layout with id "@+id/spinner1"

  • Spinner is defined as _spinner1_ with "..=@+id/...". What exactly do you mean with setContentView? I am sorry but I am new and I do not understand it completly.. –  Jan 19 '19 at 19:53
  • In your Activity in onCreate function you must call setContentView(R.layout.***) to bind your Activity with layout (*** - name of your layout where spinner was define) – Andriy Shevchuk Jan 19 '19 at 19:58
  • So I added _setContentView(R.layout.fragment_kalorienrechner);_ but the error is still the same –  Jan 19 '19 at 20:02
  • Your function must looks like this @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_kalorienrechner); } but i saw your layout name contains fragment, if your code in a fragment then we must write in your fragment something like this public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_kalorienrechner, container, false); } – Andriy Shevchuk Jan 19 '19 at 20:06
  • Ok, problem is: I already have a Navigation Drawer, so it looks like this: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.fragment_kalorienrechner); setContentView(R.layout.activity_main); //What should I do? Because without the _setContentView(R.layout.activity_main);_ the app also crashes & if I have both of them written of course this also doesn't work. –  Jan 19 '19 at 20:13
  • You mean my fragment.java, don't you? –  Jan 19 '19 at 20:23
  • Activities and Fragment have different bindings with their layouts. If your Spinner is in Fragment you should check fragment binding with its layout in function onCreateView. Fragment is a part of an Activity but findViewById in Activity does not find Fragment's view. – Andriy Shevchuk Jan 19 '19 at 20:29
  • I understand, but in my MainActivity there are 2 setContentView one for the Navigation Drawer (_ setContentView(R.layout.activity_main);_) and the other one for the Spinner (_setContentView(R.layout.fragment_kalorienrechner);_) but of course two calls won't work with each other. I need the Navigation Drawer one for the drawer to work properly and I need the other for the one for the spinner to work properly, dont' I? So how can I fix that? –  Jan 19 '19 at 20:37
  • Yes, your MainActivity must only one setContentView, but your spinner defined in R.layout.fragment_kalorienrechner and i guess the code to init and attach adapter to the Spinner is in Activity (: I think you should move your spinner java code to a fragment and attach the fragment to the activity or move your spinner layout code from R.layout.fragment_kalorienrechner to R.layout.activity_main – Andriy Shevchuk Jan 19 '19 at 20:52
  • Yes, but if your layout with your spinner called as a fragment.xml in your file system then the layout should called R.layout.fragment instead R.layout.fragment_kalorienrechner – Andriy Shevchuk Jan 19 '19 at 21:10
  • I am very very sorry. But I really do not get how I should do that. –  Jan 19 '19 at 21:39
  • You should set an adapter to the Spinner in the same class where you called setContentView or onCreateView with Spinner's layout. – Andriy Shevchuk Jan 19 '19 at 21:59
  • So set an adapter to the spinner is something like that: spinner.setAdapter(adapter); isn't it? and how am i supposed to write that java code into a xml where my spinners layout is defined? –  Jan 19 '19 at 22:07
  • Yes but if I change this then my navigation drawer won't work –  Jan 19 '19 at 23:31
  • not only my nav drawer but my complete app just won't start it crashes instantly –  Jan 19 '19 at 23:33
  • ok i just did it, not the exact way as you told but you sir helped me! thank you very very much –  Jan 20 '19 at 00:04
0

Probably you used wrong file. Check where U have spinner and where U try set the adapter. For example spinner exists in settings_activity.xml, but you try set adapter in main_activity.java

Ivan Aracki
  • 3,668
  • 8
  • 47
  • 63
matt91t
  • 25
  • 5