0

I am unsure where I am going wrong. I have used an array before to populate a listview and had no issues. Now that I am using a fragment I receive errors that I am unable to fix. I log to find where the error is and the application only crashes when running the last commented out line.

My error message is Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference but how can this be when I am performing the to string on an array of strings (which I believe I have done before without fragments). If it is an issue with the adapter please enlighten me as to what should be there.

ListView Fragment public class ListviewFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener { private LinkedList linkedList; private ListView listView;

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState
)
{


    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.frag_listviewfragment, container, false);
    MainActivity mainActivity = (MainActivity) getActivity();

    LinkedList<ItemClass> items = mainActivity.alist;

    int size = items.size();
    int j = 0;
    String itemArray[ ] = new String[size];

    if(items == null){
        Log.d("UI thread", "SHITS EMPTY FAM " );
    }
    else{
        for(ItemClass i : items){

            Log.d("UI thread", "I am the UI thread " + i.getdescription());
            //Log.d("UI thread", "The size of items is: " + size);
            itemArray[j] = i.getTitle();
            Log.d("UI thread", "following was added to array: " + i.getTitle());
            Log.d("UI thread", "The size of the array is: " + itemArray.length);
        }
    }

    Log.d("UI thread", "The size of the array is STILL: " + itemArray.length);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.activity_listview, itemArray);
    if (adapter == null)
    {
        Log.e("MyTag","Adapter error");
    }
    listView = (ListView) v.findViewById(R.id.list);
    if(listView == null){
        Log.e("MyTag","listview error");
    }

    //listView.setAdapter(adapter);




    return v;
}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{


    super.onViewCreated(view, savedInstanceState);

// ListView listView = (ListView) view.findViewById(R.id.list); // listView.setOnItemClickListener((AdapterView.OnItemClickListener) this);

    //ArrayList<ItemClass> newList = null;


}


@Override
public void onClick(View view) {

}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

}

} // End of listview Fragment

deeko
  • 5
  • 2
  • This suggests that the adapter is trying to call the toString() on a null object, which is likely caused by the itemArray containing null values. Have you checked your data? – Marcin Orlowski Apr 13 '23 at 23:13
  • @MarcinOrlowski I have made a silly mistake. I forgot to include the "j++" line to continue past the first array index. Silly question as I was thinking my adapter was the issue as I have only just started covering these. Simple iteration was the issue ! ;-; – deeko Apr 13 '23 at 23:37

0 Answers0