2

I'm having problems with a simple custom list. I have tried a few examples and end up getting the same error. The error I keep getting is

java.lang.NullPointerException: Attempt to read from null array

Adapter Class:

public class CustomList  extends ArrayAdapter<String>   {

        Context context;
        int [] images;
        String [] titleArray;
        String [] descriptionArray;

        CustomList(Context context,String[] titles, int [] images, String [] description) {
            super(context,R.layout.single_row,R.id.textView,titles);
            this.context = context;
            this.images = images;
            this.descriptionArray = description;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row =  inflater.inflate(R.layout.single_row,parent, false);

            ImageView img = (ImageView) row.findViewById(R.id.imageView);
            TextView myTxtTitle = (TextView) row.findViewById(R.id.textView);
            TextView myTxtDescription =  (TextView) row.findViewById(R.id.textView2);

            img.setImageResource(images[position]);
            myTxtTitle.setText(titleArray[position]); // where error is happening?
            myTxtDescription.setText(descriptionArray[position]);

            return row;
        }}

Activity class:

public class MainActivity extends AppCompatActivity {

    ListView list;
    String [] name;
    String [] sound;
    int [] images = {
            R.drawable.bird,
            R.drawable.cat,
            R.drawable.cow,
            R.drawable.dog,
            R.drawable.frog
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res = getResources();
        name = res.getStringArray(R.array.title);
        sound = res.getStringArray(R.array.description);

        list = (ListView) findViewById(R.id.list);

        CustomList customAdapter = new CustomList(this, name, images, sound);
        list.setAdapter(customAdapter);
    }
}

2 Answers 2

6

You just forgot to assign the title array... hence the array is null referenced....

String [] titleArray;
String [] descriptionArray;

CustomList(Context context,String[] titles, int [] images, String [] description)
    {
    super(context,R.layout.single_row,R.id.textView,titles);
    this.context = context;
    this.images = images;
    this.descriptionArray = description;


     //addd this to init the array
     this.titleArray = titles;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

you need null check.

ex) if(titleArray != null) and if(descriptionArray != null)

It happens when you try to use the data that is null.

1 Comment

This check is not necessary, these Object == null check often indicates bad code in my opinion. Sure you need it sometimes, but not in this case, since you only need to initialize the array and it will no be null in any case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.