By behzad razzaqi

2015-03-16 15:22:09 8 Comments

I define this String Array:

String[] mCountryList = new String[200];

and fill up the variable with JSON data, and when I use that variable with an ArrayAdapter:

ArrayAdapter<String> myAdaptor = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, mCountryList);

I get this error:


I think the String array mCountryList can have null values. For example:


How can I solve this?

The stacktrace I get is:

2459-2459/com.example.behzad.behmytour E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.behzad.behmytour, PID: 2459 java.lang.NullPointerException at android.widget.ArrayAdapter.createViewFromResource( at android.widget.ArrayAdapter.getView( at android.widget.AbsListView.obtainView( at android.widget.ListView.makeAndAddView( at android.widget.ListView.fillDown( at android.widget.ListView.fillFromTop( at android.widget.ListView.layoutChildren( at android.widget.AbsListView.onLayout( at android.view.View.layout( at android.view.ViewGroup.layout(


@The Original Android 2015-03-19 05:58:26

I think your error is in code mCountryList[2]=null; Even though this is allowed, it is not a good idea to initialize a string to null, certainly not common in Java. Only objects can be set to null.

Suggestion: mCountryList[2]= ""; Set to a blank string instead.

@ci_ 2015-03-16 15:36:13

Looks like one of your items is null, like you probably thought, but didn't express easily understandable in your question. The solution is, make sure it's not.

NullPointerException at android widget ArrayAdapter createViewFromResource

You might want to use a List instead of an array, and then only populate as many items as you have instead of having a static array with 200 items, unless of course you will always have 200 items.

So use:

List<String> mCountryList = new ArrayList<String>();

instead of

String[] mCountryList=new String[200];

and then to populate it, something like this:

mCountryList.add("Your String");

@behzad razzaqi 2015-03-16 15:38:54

Thanks,but how can i set my string array to my adaptor?i'm beginner android

@The Original Android 2015-03-19 06:00:42

Using an ArrayList is a good idea, better than using String[], an array of strings.

Related Questions

Sponsored Content

105 Answered Questions

[SOLVED] Close/hide android soft keyboard

52 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

38 Answered Questions

[SOLVED] How to lazy load images in ListView in Android

1 Answered Questions

[SOLVED] Null pointer exception outside my code

1 Answered Questions

[SOLVED] Android ADB Logcat-- showing full logcat

Sponsored Content