简体   繁体   中英

Android Null Pointer Exception

I have created a ListViewAdapter in order to produce a customized list view. Below is the class I have written.

public class ListViewAdapter extends BaseAdapter {

Context mContext;
LayoutInflater mInflater;
ArrayList mArray;

public ListViewAdapter(Context context, LayoutInflater inflater) {
    mContext = context;
    mInflater = inflater;
    mArray = new ArrayList();
}

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

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

@Override
public long getItemId(int position) {
    // your particular data set uses String IDs
    // but you have to put something in this method
    return position;
}

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

    // check if the view already exists
    // if so, no need to inflate and findViewById again!
    if (convertView == null) {

        // Inflate the custom row layout from your XML.
        convertView = mInflater.inflate(R.layout.list_item, null);

        // create a new "Holder" with subviews
        holder = new ViewHolder();
        holder.itemNameView = (TextView) convertView.findViewById(R.id.item_name);
        holder.itemExpiryView = (TextView) convertView.findViewById(R.id.item_expiry);

        // hang onto this holder for future recycling
        convertView.setTag(holder);
    } else {

        // skip all the expensive inflation/findViewById
        // and just get the holder you already made
        holder = (ViewHolder) convertView.getTag();
    }
    // More code after this

    // Grab the title and author from the JSON
    String name = "";
    String expiry = "7 days";

    // Write appropriate codes to obtain values for the string variables above
    name = (String) getItem(position);

    // Send these Strings to the TextViews for display
    holder.itemNameView.setText(name);
    holder.itemExpiryView.setText(expiry);

    return convertView;
}

// this is used so you only ever have to do
// inflation and finding by ID once ever per View
private static class ViewHolder {
    public TextView itemNameView;
    public TextView itemExpiryView;
}

public void updateData(ArrayList arrayPassed) {
    // update the adapter's data set
    mArray = arrayPassed;
    notifyDataSetChanged();
}


}

The list_item.xml file looks like this..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/item_expiry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/item_name" />

<Button
    android:id="@+id/button_edit_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/item_expiry"
    android:layout_alignParentLeft="true"
    android:text="Edit"
    android:clickable="true"
    android:onClick="editItem" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/item_expiry"
    android:layout_alignParentRight="true"
    android:text="Delete"
    android:clickable="true"
    android:onClick="deleteItem" />


</RelativeLayout>

I am trying to add to the list using a function inside mainActivity. The relevant part of the code is :

ArrayList currentList = new ArrayList();
ListViewAdapter itemAdder2;
EditText input = new EditText(this);
String inputName = input.getText().toString();
currentList.add(currentList.size(), inputName);
itemAdder2.updateData(currentList);

I am getting the following NULL reference error while accessing the currentList at the very last line.

java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.example.user.reggaeshark.ListViewAdapter.updateData(java.util.ArrayList)' 
on a null object reference
        at com.example.user.reggaeshark.MainActivity$3.onClick(MainActivity.java:170)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Please help. I am pretty new to android programming. Pretty sure that shows. Sorry for the huge amount of code given. :( Couldn't find a better way to ask/convey my question. If someone can rephrase the question in a better way so that it reflects the problem and not just my code instance, please welcome.

You never instantiate the ListViewItemAdapter. You are trying to invoke a method on it but since the object is null, you are getting an NPE.

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