Android TextView SetText null object reference error
<p>I am new to android and I am getting a </p>

<pre><code>Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
</code></pre>

<p>error on my app. I have read other questions on here like this one <a href="https://stackoverflow.com/questions/27911066/java-lang-nullpointerexception-settext-on-null-object-reference">- java.lang.NullPointerException - setText on null object reference</a> but I still get the same issue. My format is a little bit different as this is done in a Fragment and what I am doing is having a Custom Adapter from a listview. Here is my code</p>

<p><strong>fragment_song_genre.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools"&gt;

&lt;data class="SongGenreBinding"&gt;

    &lt;variable
        name="handler"
        type="com.mgrmobi.joox.fragments.FilterSongGenreFragment" /&gt;

&lt;/data&gt;

&lt;RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"&gt;

    &lt;RelativeLayout
        android:id="@+id/toprow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"&gt;

        &lt;ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="16dp"
            android:background="@null"
            android:onClick="@{ handler.onCloseClicked }"
            android:src="@drawable/search_close" /&gt;

        &lt;TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="80dp"
            android:text="Genre"
            android:textColor="#fff"
            android:textSize="20sp" /&gt;

        &lt;TextView
            android:onClick="@{ handler.reset }"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:text="@string/reset"
            android:textAllCaps="true"
            android:textColor="#fff"
            android:textSize="12sp" /&gt;

    &lt;/RelativeLayout&gt;

    &lt;ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toprow"
        android:layout_marginTop="24dp"&gt;&lt;/ListView&gt;

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

<p></p>

<p>That is the main page and it has a Listview in the Bottom and here is the fragment that calls that xml layout</p>

<pre><code> public class FilterSongGenreFragment extends BaseFragment {

    private SongGenreBinding binding;
    private ArrayList&lt;String&gt; genres;
    String[] names= {"Johm","Paul","Mike"};

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    }


    private static final String GENRES_KEY = "genres";

    public static FilterSongGenreFragment newInstance(ArrayList&lt;String&gt; genres) {

        Bundle args = new Bundle();
        args.putStringArrayList(GENRES_KEY, genres);
        FilterSongGenreFragment fragment = new FilterSongGenreFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_song_genre, container, false);
        binding.setHandler(this);
        genres = getArguments().getStringArrayList(GENRES_KEY);

        initList();
        return binding.getRoot();
    }

    private void initList() {

    // ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.genre_list_text, android.R.id.text1, genres);
     FilterSongsAdapter filterSongsAdapter= new FilterSongsAdapter(getActivity(),names);


        binding.list.setAdapter(filterSongsAdapter);

        binding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView&lt;?&gt; adapterView, View view,int position, long l) {
               ((SongFilterListener) getActivity()).onGenreSelected(genres.get(position));
/*
                    textView = (ImageView) view.findViewById(R.id.selected_genre);
                    textView.setVisibility(View.VISIBLE);
*/
              //  close();
            }

        });

    }

    private void close() {
        getActivity().getFragmentManager().beginTransaction().remove(this).commit();
    }

    public void onCloseClicked(View view) {
        close();
    }

    public void reset(View view) {
        ((SongFilterListener) getActivity()).onResetFilters();
        close();
    }

}
</code></pre>

<p>Here is the xml for the listview custom style <strong>genre_list_text.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"&gt;

    &lt;TextView
    android:id="@android:id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|left"
    android:text="Genre"
    android:paddingBottom="16dp"
    android:paddingLeft="4dp"
    android:paddingTop="16dp"
    android:textColor="#fff"
    android:textSize="16sp" /&gt;

        &lt;ImageView
            android:id="@+id/selected_genre"
            android:layout_height="24dp"
            android:layout_width="24dp"
            android:visibility="invisible"
            android:src="@drawable/check_circle"
            android:layout_marginStart="140dp"
            android:layout_centerVertical="true" /&gt;

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

<p>and here is my adapter and as stated before the error happens in the SetText area</p>

<pre><code>  public class FilterSongsAdapter extends ArrayAdapter&lt;String&gt; {

    private Context c;
    String[] names= {};
    LayoutInflater inflater;

    public FilterSongsAdapter(Context context,String[] names) {
        super(context,R.layout.genre_list_text,names);
        this.c=context;
        this.names=names;

    }

    public static class ViewHolder {

       TextView text1;


    }



    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        ViewHolder holder;
     if(convertView==null){
         holder  = new ViewHolder();
         inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.from(c).inflate(R.layout.genre_list_text, null);
         // Initialize
         holder.text1=(TextView) convertView.findViewById(R.id.text1);

          convertView.setTag(holder);
    }else {
         holder = (ViewHolder) convertView.getTag();
     }

        holder.text1.setText(names[position]);

        return convertView;
    }




}
</code></pre>

<p>The SetText inside the GetView is throwing the exception every time, that TextView should have 3 objects names from the Names String Array which I have seen that it has. Based on the error I know that i'm setting the SetText before the FindByID method locates the TextView but I have moved things around and nothing works. Any help would be great . I also been following this tutorial for reference <a href="https://www.youtube.com/watch?v=99C_UpLcBRk" rel="nofollow noreferrer">https://www.youtube.com/watch?v=99C_UpLcBRk</a> . I do not know if maybe with a Fragment things are different.</p>