Riki Rikmen Riki Rikmen - 6 months ago 123
Android Question

Array Adapter with RealmList : Attempt to invoke virtual method on a null object reference

I just trying to create adapter to my list with checkbox, i'm using Realm for my database.

i found null object reference.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference.

public class DialogActivity extends AppCompatActivity {
Realm realm;
private RealmList<DetailPerson> person;
private ListView listView;
private MenuAdapter adapter;
private String bill;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_add_menu);
bill = getIntent().getStringExtra("bill_ID");
realm = Realm.getDefaultInstance();
listView = (ListView) findViewById(R.id.listViewMenu);
person = realm.where(Bill.class).equalTo("Bill_ID", bill).findFirst().getDetailperson();
adapter = new MenuAdapter(this,R.layout.row_menu,person);
listView.setAdapter(adapter);



}
}


Bill.java

public class Bill extends RealmObject {


@PrimaryKey private String Bill_ID;
public RealmList<DetailMenu> detailmenu;
public RealmList<DetailPerson> detailperson;
private String name;
private int price;

public RealmList<DetailPerson> getDetailperson() {
return detailperson;
}

public void setDetailperson(RealmList<DetailPerson> detailperson) {
this.detailperson = detailperson;
}

public RealmList<DetailMenu> getDetailmenu() {
return detailmenu;
}

public void setDetailmenu(RealmList<DetailMenu> detailmenu) {
this.detailmenu = detailmenu;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPrice() {
return price;
}

public void setPrice(int price) { this.price = price; }

public String getBill_ID() {
return Bill_ID;
}

public void setBill_ID(String bill_ID) {
Bill_ID = bill_ID;
}

}


MenuAdapter.java

public class MenuAdapter extends ArrayAdapter<DetailPerson>{
private RealmList<DetailPerson> persons;
private Context context;
Realm realm;
private LayoutInflater inflater;

public MenuAdapter(Context context, int resource, RealmList<DetailPerson> objects) {
super(context, resource, objects);
this.context = context;
this.persons = objects;
}


@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}

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

private class ViewHolder {
TextView menuItem;
CheckBox checkBoxMenu;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

if (convertView == null) {
convertView = inflater.inflate(R.layout.row_menu, null); //there its the error come from.

holder = new ViewHolder();
holder.menuItem = (TextView) convertView.findViewById(R.id.textViewMenuItem);
holder.checkBoxMenu = (CheckBox) convertView.findViewById(R.id.checkBoxMenu);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}

holder.menuItem.setText(" (" + persons.get(position).getPersonID() + ")");
holder.checkBoxMenu.setText(persons.get(position).getPersonName());
holder.checkBoxMenu.setChecked(false);
holder.checkBoxMenu.setTag(persons);

return convertView;
}
}

Answer

You are missing to initialize your inflater object, you can and should do that in your MenuAdapter constructor, for e.g here

public MenuAdapter(Context context, int resource, RealmList<DetailPerson> objects) {
    super(context, resource, objects);
    this.context = context;
    this.persons = objects;
    inflater = LayoutInflater.from(context);
}
Comments