0

I don't understand what is happening

 public class MainActivity extends Activity {

    ArrayAdapter<String> aa;
    ArrayList<ArrayList<String>> l=null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        place = (AutoCompleteTextView)findViewById(R.id.place);
        aa=new ArrayAdapter<String(getBaseContext(),android.R.layout.simple_list_item_1,l.get(0) );
        place.setAdapter(aa);
    }
}

But creating the new ArrayAdapter instance is causing runtime error due to java.lang.NullPointerExeption. I don't understand why and how can I fix this :-/

2
  • Your adapter ArrayList is null before given to adapter please initialize this variable. Commented Aug 5, 2014 at 4:17
  • you defined ArrayList but you didn't initialized it so you are getting NPE error. Commented Aug 5, 2014 at 5:35

2 Answers 2

2

Check this line:

aa=new ArrayAdapter<String(getBaseContext(),android.R.layout.simple_list_item_1,l.get(0) );

Here use of l.get(0) is null as l is never initialized.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, in the line before that I put l.get(0).add(""), I think "I added an empty string as first element in the l.get(0) string array" but again, now this line is causing java.lang null pointer exception.
@JoeCoolman l is null so any operation on l will result in nullpointer exception ArrayList<String> l = new ArrayList<String>(); this kind of initialization needs to be done first on most of the objects including arraylist
0

Here l.get(0) is throwing NPE. You need to initialise ArrayList<ArrayList<String>> l like,

 ArrayList<ArrayList<String>> l = new ArrayList<ArrayList<String>>();

3 Comments

@Illegal Argument and you are both correct, I was reading more and I discovered how initialize one-dimension ArrayList using the double brace initialization method. Now, how it must be done for double-dimension ArrayList?
Did you try my answer?? Try l = new ArrayList<ArrayList<String>>(); and let me know.
@JoeCoolman Thanks for the accept :). You can up vote also. Happy coding :)

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.