Android Spinner: app crashes onItemSelected
<p>I came up with problem integrating setting title to my custom spinner. After click on any item crashes with this error.</p>

<p>08-04 02:48:39.490 16956-16956/com.example.ilija.festivalapp_makedox E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.ilija.festivalapp_makedox, PID: 16956
                                                                                       java.lang.NullPointerException
                                                                                           at com.example.ilija.festivalapp_makedox.CustomSpinner$1.onItemSelected(CustomSpinner.java:98)
                                                                                           at com.example.ilija.festivalapp_makedox.SpinnerPlus.setSelection(SpinnerPlus.java:21)
                                                                                           at android.widget.Spinner$DropdownPopup$1.onItemClick(Spinner.java:1042)
                                                                                           at android.widget.AdapterView.performItemClick(AdapterView.java:299)
                                                                                           at android.widget.AbsListView.performItemClick(AbsListView.java:1282)
                                                                                           at android.widget.ListView.performItemClick(ListView.java:4450)
                                                                                           at android.widget.AbsListView$PerformClick.run(AbsListView.java:3174)
                                                                                           at android.widget.AbsListView$3.run(AbsListView.java:3925)
                                                                                           at android.os.Handler.handleCallback(Handler.java:733)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                           at android.os.Looper.loop(Looper.java:157)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:5872)
                                                                                           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:858)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
                                                                                           at dalvik.system.NativeStart.main(Native Method)</p>

<p>CustomSpinner.java:</p>

<pre><code>protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_spinner);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Movies");
        setSupportActionBar(toolbar);
        activity = this;
        SpinnerPlus SpinnerExample = (SpinnerPlus) findViewById(R.id.spinner);
        SpinnerPlus SpinnerExample1 = (SpinnerPlus) findViewById(R.id.spinner1);
        SpinnerPlus SpinnerExample2 = (SpinnerPlus) findViewById(R.id.spinner2);
        SpinnerPlus SpinnerExample3 = (SpinnerPlus) findViewById(R.id.spinner3);
         SpinnerPlus SpinnerExampleTest = (SpinnerPlus) findViewById(R.id.spinnerTest);
      /*   Spinner SpinnerExampleTest = (Spinner) findViewById(R.id.spinnerTest);
        Spinner SpinnerExampleTest1 = (Spinner) findViewById(R.id.spinner1Test);
        Spinner SpinnerExampleTest2 = (Spinner) findViewById(R.id.spinner2Test);
        Spinner SpinnerExampleTest3 = (Spinner) findViewById(R.id.spinner3Test);*/
        output = (TextView)findViewById(R.id.output);

        setListData();
        setListData1();
        setListData2();
        setListData3();

        setListDataTest();
       /* setListData1Test();
        setListData2Test();
        setListData3Test();*/

        Resources res = getResources();
        adapter = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr,res);
        adapter1 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1,res);
        adapter2 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2,res);
        adapter3 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3,res);

         adapterTest = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArrTest,res);
       /*adapter1Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1Test,res);
        adapter2Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2Test,res);
        adapter3Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3Test,res);*/

        adapter.setDropDownViewResource(R.layout.spinner_rows);
        SpinnerExample.setPrompt("Select your favorite Planet!");

        SpinnerExample.setAdapter(
                new NothingSelectedSpinnerAdapter(
                        adapter,
                        R.layout.contact_spinner_row_nothing_selected,
                        // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
                        this));

        SpinnerExample.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              //  i++;  //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
                String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
               // if(i&gt;=1){
                    Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
                    if(position==0) {
                        intent.putExtra("Cartoons","10");
                    } else if(position==1) {
                        intent.putExtra("Cartoons","11");
                    } else if(position==2) {
                        intent.putExtra("Cartoons","12");
                    } else if(position==3) {
                        intent.putExtra("Cartoons","13");
                    } else if(position==4) {
                        intent.putExtra("Cartoons","14");
                    } else if(position==5) {
                        intent.putExtra("Cartoons","15");
                    } else if(position==6) {
                        intent.putExtra("Cartoons","16");
                    } else if(position==7) {
                        intent.putExtra("Cartoons","17");
                    } else if(position==8) {
                        intent.putExtra("Cartoons","18");
                    }
                    startActivity(intent);
                //}
            }

            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; parent) {

            }
        });


       SpinnerExample1.setAdapter(adapter1);
        SpinnerExample1.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
               // i++;  //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
                String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
             //   if(i&gt;2){
                    Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
                    if(position==0) {
                        intent.putExtra("Cartoons","20");
                    } else if(position==1) {
                        intent.putExtra("Cartoons","21");
                    } else if(position==2) {
                        intent.putExtra("Cartoons","22");
                    } else if(position==3) {
                        intent.putExtra("Cartoons","23");
                    } else if(position==4) {
                        intent.putExtra("Cartoons","24");
                    } else if(position==5) {
                        intent.putExtra("Cartoons","25");
                    } else if(position==6) {
                        intent.putExtra("Cartoons","26");
                    } else if(position==7) {
                        intent.putExtra("Cartoons","27");
                    } else if(position==8) {
                        intent.putExtra("Cartoons","28");
                    }
                    startActivity(intent);
               // }
            }

            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; parent) {

            }
        });
</code></pre>

<p>NothingSelectedSpinnerAdapter.java</p>

<pre><code>package com.example.ilija.festivalapp_makedox;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;

/**
 * Decorator Adapter to allow a Spinner to show a 'Nothing Selected...' initially
 * displayed instead of the first choice in the Adapter.
 */
public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter {

    protected static final int EXTRA = 1;
    protected SpinnerAdapter adapter;
    protected Context context;
    protected int nothingSelectedLayout;
    protected int nothingSelectedDropdownLayout;
    protected LayoutInflater layoutInflater;

    /**
     * Use this constructor to have NO 'Select One...' item, instead use
     * the standard prompt or nothing at all.
     * @param spinnerAdapter wrapped Adapter.
     * @param nothingSelectedLayout layout for nothing selected, perhaps
     * you want text grayed out like a prompt...
     * @param context
     */
    public NothingSelectedSpinnerAdapter(
            SpinnerAdapter spinnerAdapter,
            int nothingSelectedLayout, Context context) {

        this(spinnerAdapter, nothingSelectedLayout, -1, context);
    }

    /**
     * Use this constructor to Define your 'Select One...' layout as the first
     * row in the returned choices.
     * If you do this, you probably don't want a prompt on your spinner or it'll
     * have two 'Select' rows.
     * @param spinnerAdapter wrapped Adapter. Should probably return false for isEnabled(0)
     * @param nothingSelectedLayout layout for nothing selected, perhaps you want
     * text grayed out like a prompt...
     * @param nothingSelectedDropdownLayout layout for your 'Select an Item...' in
     * the dropdown.
     * @param context
     */
    public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
                                         int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
        this.adapter = spinnerAdapter;
        this.context = context;
        this.nothingSelectedLayout = nothingSelectedLayout;
        this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        // This provides the View for the Selected Item in the Spinner, not
        // the dropdown (unless dropdownView is not set).
        if (position == 0) {
            return getNothingSelectedView(parent);
        }
        return adapter.getView(position - EXTRA, null, parent); // Could re-use
        // the convertView if possible.
    }

    /**
     * View to show in Spinner with Nothing Selected
     * Override this to do something dynamic... e.g. "37 Options Found"
     * @param parent
     * @return
     */
    protected View getNothingSelectedView(ViewGroup parent) {
        return layoutInflater.inflate(nothingSelectedLayout, parent, false);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        // Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
        // Spinner does not support multiple view types
        if (position == 0) {
            return nothingSelectedDropdownLayout == -1 ?
                    new View(context) :
                    getNothingSelectedDropdownView(parent);
        }

        // Could re-use the convertView if possible, use setTag...
        return adapter.getDropDownView(position - EXTRA, null, parent);
    }

    /**
     * Override this to do something dynamic... For example, "Pick your favorite
     * of these 37".
     * @param parent
     * @return
     */
    protected View getNothingSelectedDropdownView(ViewGroup parent) {
        return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
    }

    @Override
    public int getCount() {
        int count = adapter.getCount();
        return count == 0 ? 0 : count + EXTRA;
    }

    @Override
    public Object getItem(int position) {
        return position == 0 ? null : adapter.getItem(position - EXTRA);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position &gt;= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
    }

    @Override
    public boolean hasStableIds() {
        return adapter.hasStableIds();
    }

    @Override
    public boolean isEmpty() {
        return adapter.isEmpty();
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        adapter.registerDataSetObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        adapter.unregisterDataSetObserver(observer);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return position != 0; // Don't allow the 'nothing selected'
        // item to be picked.
    }

}
</code></pre>

<p>SpinnerPlus.java:</p>

<pre><code>import android.content.Context;
import android.util.AttributeSet;
import android.widget.AdapterView;
import android.widget.Spinner;

import java.lang.reflect.Field;

public class SpinnerPlus extends Spinner {
    AdapterView.OnItemSelectedListener listener;

    public SpinnerPlus(Context context, AttributeSet attrs) {
        super(context,attrs);
    }

    @Override
    public void setSelection(int position) {
        super.setSelection(position);
        if (listener != null)
            listener.onItemSelected(this, getSelectedView(), position, 0);
    }

    public void setOnItemSelectedEvenIfUnchangedListener(
            AdapterView.OnItemSelectedListener listener) {
        this.listener = listener;
    }
}
</code></pre>

<p>CustomAdapter.java</p>

<pre><code>package com.example.ilija.festivalapp_makedox;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by ilija on 15-Jul-16.
 */

public class CustomAdapter extends ArrayAdapter&lt;String&gt;{

    private Activity activity;
    private ArrayList data;
    public Resources res;
    SpinnerModel tempValues=null;
    LayoutInflater inflater;

    public CustomAdapter(
            CustomSpinner activitySpinner,
            int textViewResourceId,
            ArrayList objects,
            Resources resLocal
    ) {
        super(activitySpinner, textViewResourceId, objects);
                    activity = activitySpinner;
                    data = objects;
                    res=resLocal;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent){
            return getCustomView(position,convertView,parent);
    }

        public View getView(int position, View convertView, ViewGroup parent){
            return getCustomView(position,convertView,parent);
        }



    public View getCustomView(int position, View convertView, ViewGroup parent){
        View row = inflater.inflate(R.layout.spinner_rows, parent, false);
        tempValues = null;
        tempValues = (SpinnerModel) data.get(position);

        TextView label = (TextView)row.findViewById(R.id.movieName);
        ImageView image = (ImageView)row.findViewById(R.id.imageSpinner);
        RelativeLayout backgorund = (RelativeLayout)row.findViewById(R.id.spinnerBackground);
        label.setText(tempValues.getMovieName());
        image.setImageResource(res.getIdentifier("com.example.ilija.festivalapp_makedox:drawable/"+tempValues.getImage(),null,null));
        //backgorund.setBackgroundColor(tempValues.getBackground());
        return row;
    }
}
</code></pre>

<p>activity_custom_spinner.xml</p>

<pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:agendaCalendar="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainScreen"&gt;


    &lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"&gt;
        &lt;!--tools:context=".MainScreen"--&gt;

        &lt;android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
            android:layout_height="match_parent" android:fitsSystemWindows="true"
            tools:context=".MainScreen"&gt;

            &lt;android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
                android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"&gt;

                &lt;android.support.v7.widget.Toolbar android:id="@+id/toolbar"
                    android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;

            &lt;/android.support.design.widget.AppBarLayout&gt;

            &lt;ImageView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="55dp"
                android:id="@+id/imageView2"
                android:layout_alignParentTop="true"
                android:background="@drawable/makecover"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"/&gt;



        &lt;ScrollView
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="155dp"
            android:layout_marginBottom="10dp"
            android:fillViewport="true"
            &gt;
        &lt;LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:orientation="vertical"
            &gt;
                &lt;TextView
                    android:id="@+id/ico"
                    android:text="SELECTIONS:"
                    android:paddingTop="10dp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="1dp" /&gt;
                &lt;TextView
                    android:id="@+id/ico1"
                    android:text="INTERNATIONAL COMPETITION"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="1dp"
                    android:paddingTop="5dp"/&gt;
                &lt;com.example.ilija.festivalapp_makedox.SpinnerPlus
                    android:id="@+id/spinner"
                    android:background="@color/main_competition"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/&gt;
                &lt;TextView
                    android:id="@+id/ico2"
                    android:text="NEWCOMERS COMPETITION"
                    android:paddingLeft="1dp"
                    android:paddingTop="5dp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" /&gt;
          &lt;!--      &lt;com.example.ilija.festivalapp_makedox.SpinnerPlus
                    android:id="@+id/spinner1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/&gt;--&gt;
            &lt;com.example.ilija.festivalapp_makedox.SpinnerPlus
                android:id="@+id/spinner1"
                android:background="@color/newcomers"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                /&gt;
                &lt;TextView
                    android:id="@+id/ico3"
                    android:text="FOCUS RUSSIA"
                    android:paddingLeft="1dp"
                    android:paddingTop="5dp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" /&gt;
                &lt;com.example.ilija.festivalapp_makedox.SpinnerPlus
                    android:id="@+id/spinner2"
                    android:background="@color/russian"
                    android:spinnerMode="dropdown"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/&gt;

                &lt;TextView
                    android:id="@+id/ico4"
                    android:text="SHORT DOX"
                    android:paddingLeft="1dp"
                    android:paddingTop="5dp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" /&gt;
                &lt;com.example.ilija.festivalapp_makedox.SpinnerPlus
                    android:id="@+id/spinner3"
                    android:background="@color/short1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/&gt;

                 &lt;TextView
                    android:id="@+id/spinnerTesttxt"
                    android:text="STUDENT DOX"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="1dp"
                    android:paddingTop="5dp"/&gt;
                &lt;com.example.ilija.festivalapp_makedox.SpinnerPlus
                    android:id="@+id/spinnerTest"
                    android:background="@color/student"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/&gt;
            &lt;!--&lt;TextView
                 android:id="@+id/ico2spinnerTest"
                 android:text="ActionSpinnerTest:"
                 android:paddingLeft="10dp"
                 android:paddingTop="5dp"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content" /&gt;
             &lt;Spinner
                 android:id="@+id/spinner1Test"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"/&gt;
             &lt;TextView
                 android:id="@+id/ico3spinnerTest"
                 android:text="HorrorspinnerTest:"
                 android:paddingLeft="10dp"
                 android:paddingTop="5dp"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content" /&gt;
             &lt;Spinner
                 android:id="@+id/spinner2Test"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"/&gt;

             &lt;TextView
                 android:id="@+id/ico4spinnerTest"
                 android:text="CartoonspinnerTest:"
                 android:paddingLeft="10dp"
                 android:paddingTop="5dp"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content" /&gt;
             &lt;Spinner
                 android:id="@+id/spinner3Test"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"/&gt;--&gt;
        &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;

    &lt;include layout="@layout/content_custom_spinner" /&gt;

        &lt;/android.support.design.widget.CoordinatorLayout&gt;

        &lt;android.support.design.widget.NavigationView android:id="@+id/nav_view"
            android:layout_width="wrap_content" android:layout_height="match_parent"
            android:layout_gravity="start" android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /&gt;

    &lt;/android.support.v4.widget.DrawerLayout&gt;


&lt;/RelativeLayout&gt;
</code></pre>