-1

I've created my own custom adapter for a ListView that is in a fragment. I believe I have everything correct, but it still throws an error when launching my app and it crashes. I tried to debug it, but ended up not being able to locate the issue.

Here's my code for the fragment:

public class TabFragment0 extends Fragment {

Button addPR;
ArrayList<PRObject> PRList;
PRObjectAdapter PRListAdapter;
private ListView listViewer;
private final String PRKey = "PRArgs";

public TabFragment0() {
    // Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.tab_personalrecords, container, 
        false);
    if(loadList(PRKey) != null) {
        PRList = loadList(PRKey);
    }
    PRListAdapter = new PRObjectAdapter(getActivity(), PRList);
    listViewer = (ListView) view.findViewById(R.id.prListView);
    listViewer.setAdapter(PRListAdapter);
...................................................

The line listViewer.setAdapter(PRListAdapter); is what throws the error. I've also tried PRListAdapter = new PRObjectAdapter(this.getActivity(), PRList); as wel as PRListAdapter = new PRObjectAdapter(getContext(), PRList); to no avail.

Here's my code for the adapter class:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;

public class PRObjectAdapter extends BaseAdapter {

private LayoutInflater inflater;
private ArrayList<PRObject> objects;

private class ViewHolder {
    TextView textview1;
    TextView textview2;
}

public PRObjectAdapter(Context context, ArrayList<PRObject> objects) {
    inflater = LayoutInflater.from(context);
    this.objects = objects;
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    PRObjectAdapter.ViewHolder holder = null;
    if(convertView == null) {
        holder = new PRObjectAdapter.ViewHolder();
        convertView = inflater.inflate(R.layout.pr_item_layout, null);
        holder.textview1 = (TextView) 
                   convertView.findViewById(R.id.prTitleTV);
        holder.textview2 = (TextView) 
                   convertView.findViewById(R.id.prNumTV);
        convertView.setTag(holder);
    } else {
        holder = (PRObjectAdapter.ViewHolder) convertView.getTag();
    }
    holder.textview1.setText(objects.get(position).getTitle());
    holder.textview2.setText(objects.get(position).getNum());
    return convertView;
}
}

I want to say that the issue is in my View getView method, but I can't pinpoint the issue. Any help would be greatly appreciated.

EDIT:

This question is not a duplicate of this NullPointerException question

My question specifically pertains to the fragment/customAdapter combination in Android Studio, whereas the above link is just a guide on what a NPE is and how to fix it.

ESM
  • 175
  • 10
  • 1
    share your log report. objects.get(position).getNum() what is the return type of "getNum() " ? – shb Nov 11 '18 at 02:23
  • I'm a complete fricken moron. Look at the line `ArrayList PRList;`, and then compare it to `PRListAdapter = new PRObjectAdapter(getActivity(), PRList);`. Notice anything strange? `ArrayList PRList;` needs to be `ArrayList PRList = new ArrayList();`.... I'm such an idiot, lol – ESM Nov 11 '18 at 02:47
  • 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) – Hovercraft Full Of Eels Nov 11 '18 at 02:57
  • @HovercraftFullOfEels it is not a duplicate. – ESM Nov 11 '18 at 03:24

1 Answers1

0

Okay so it turns out nothing was wrong with the adapter class, it works perfectly as intended. My problem was that:

ArrayList<PRObject> PRList;

was set without being initialized in my fragment class.

The fix was to change it to:

ArrayList<PRObject> PRList = new ArrayList<PRObject>();

The view now loads as intended. Hopefully someone can make use out of my adapter class then since it works, hahah

Cheers everyone!

ESM
  • 175
  • 10