简体   繁体   中英

Android - Adapter NullPointerException

I apologize in advance if this is a duplicate. I am still new to android development and tried looking for a resolution however could not find one that works.

I am creating a to-do app and getting this error in my adapter.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String assignment.Model.getAssignment()' on a null object reference
at assignment.Adapter.getView(Adapter.java:39)    

and the line of code that it is referencing to is

assignment.setText(modelItems[position].getAssignment());

I believe that the position that I am setting it as is what is causing the error but I'm not sure how to fix it.

Here's part of the rest of my code for reference:

MainActivity.Java - onActivityResult

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    String assignmentSentBack = data.getStringExtra("editAssignment");
    Integer monthSentBack = data.getIntExtra("month", 0);
    Integer daySentBack = data.getIntExtra("day", 0);

    modelItems = new Model[100];
    ArrayList<Model> modelArrayList = new ArrayList<>(Arrays.asList(modelItems));
    modelArrayList.add(new Model(assignmentSentBack, (monthSentBack + 1) + "/" + daySentBack, 0));
    lv = (ListView) findViewById(R.id.listAssignment);
    ListAdapter adapter = new Adapter(this, modelItems);
    lv.setAdapter(adapter);
}

Second Activity - onSendActivity (Button)

public void onSendAssignment (View view) {

    EditText editAssignmentET = (EditText)
            findViewById(R.id.editAssignment);

    String editAssignment = String.valueOf(editAssignmentET.getText());

    DatePicker datePickerDP = (DatePicker)
            findViewById (R.id.datePicker);

    Integer month = Integer.valueOf(datePickerDP.getMonth());

    Integer day = Integer.valueOf(datePickerDP.getDayOfMonth());

    Intent goingBack = new Intent();

    goingBack.putExtra("editAssignment", editAssignment);
    goingBack.putExtra ("month", month);
    goingBack.putExtra("day", day);

    setResult(RESULT_OK, goingBack);

    finish();
}

Adapter

public class Adapter extends ArrayAdapter {

Model[] modelItems = null;
Context context;

public Adapter(Context context, Model[] resource) {
    super(context, R.layout.row, resource);
    this.context = context;
    this.modelItems = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.row, parent, false);
        TextView assignment = (TextView) convertView.findViewById(R.id.assignment);
        TextView dueDate = (TextView) convertView.findViewById(R.id.dueDate);
        CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);
        assignment.setText(modelItems[position].getAssignment());
        dueDate.setText(modelItems[position].getDueDate());
        if (modelItems[position].getValue() == 1)
            cb.setChecked(true);
        else
            cb.setChecked(false);

        return convertView;
    }
}

Model

public class Model {

String assignment;
String dueDate;
int value;

Model (String assignment, String dueDate, int value){
    this.assignment = assignment;
    this.dueDate = dueDate;
    this.value = value;
}

public String getAssignment(){
    return this.assignment;
}

public String getDueDate(){
    return this.dueDate;
}

public int getValue(){
    return this.value;
    }
}    

Any help is appreciated. Thank you.

you should try to wrap it in an inner Holder class and define the parameters in that class

public class Adapter extends ArrayAdapter {

Model[] modelItems = null;
Context context;

public Adapter(Context context, Model[] resource) {
    super(context, R.layout.row, resource);
    this.context = context;
    this.modelItems = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = vi.inflate(R.layout.list_layout, null);

            // Find the child views.
            holder.assignment= (TextView) convertView.findViewById(R.id.txt_name);
            holder.dueDate= (Button) convertView.findViewById(R.id.btn_invite);
            holder.cb= (Button) convertView.findViewById(R.id.btn_track);
            convertView.setTag(holder);

//....

        }
        // Reuse existing row view
        else {
            holder = (ViewHolder)convertView.getTag();

        }

        return convertView;
    }
 class ViewHolder {

        TextView assignment;
TextView dueDate;
CheckBox cb;

    }
}

change

public class Adapter extends ArrayAdapter {

to

public class Adapter extends ArrayAdapter<Model> {

You have created model array of size 100 here:

modelItems = new Model[100];

So, 100 models are being created but all 100 indexes have null value.

Then you have created ArrayList using that array:

ArrayList<Model> modelArrayList = new ArrayList<>(Arrays.asList(modelItems));

So again your modelArrayList has 100 null objects. Which BTW you are using no where.

You are passing modelItems into constructor of Adapter . So now since all you items are null , you are getting this exception.

Try to do something like this:

ArrayList<Model> modelArrayList = new ArrayList<>();
modelArrayList.add(new Model(assignmentSentBack, (monthSentBack + 1) + "/" + daySentBack, 0));

Similarly add more model objects like that. Pass this modelArrayList in your adapter's constructor and use this (instead of array) to display the list.

lv = (ListView) findViewById(R.id.listAssignment);
ListAdapter adapter = new Adapter(this, modelArrayList)

And thus your adapter will be like this:

ArrayList<Model> modelArrayList;
Context context;

public Adapter(Context context, ArrayList<Model> resource) {
    super(context, R.layout.row, resource);
    this.context = context;
    this.modelArrayList = resource;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM