ArrayList.contains ("texto") arroja NullPointerException?

I'm to trying to make an android app where I accept input from the user using the speech Recognition server. I'm capturing the results in a ArrayList as follows :

matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);

matches is declared globally as :

ArrayList<string> matches;

Now I want to check if the user has spoken a particular word. So I do this :

if(matches.contains("TextToBeDetected")) { }

But the line above throws a NullPointerException.

Por favor, ayuda.

P.S Yes, I'm a Java and Android newbie.

preguntado el 09 de marzo de 12 a las 16:03

ArrayList<string> should be ArrayList<String>. And what is matches being set to in your getStringArrayListExtra method? -

@Selvin: I think you mean OP. And there's no need to be mean to the newbie. -

5 Respuestas

Obviously, your matches is null. As per the Intent documentation, http://developer.android.com/reference/android/content/Intent.html#getStringArrayListExtra%28java.lang.String%29 getStringArrayListExtra returns null if the key is not found in the bundle.

Test for nullity before testing if it contains anything

respondido 09 mar '12, 16:03

Suena como si getStringArrayListExtra está regresando null (los docs say it will if there is such ArrayList value found), and so when you try matches.contains("TextToBeDetected") it fails because you're trying to dereference a null reference. You'll want to check the return value from getStringArrayListExtra to make sure it's not null before you use it. E.g.:

matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
// ...
if (matches != null) {
    if (matches.contains("TextToBeDetected")) {
        // do something
    }
}

respondido 09 mar '12, 16:03

If you see the docs Android.Content.Intent.GetStringArrayListExtra Dice

the value of an item that previously added with putExtra() or null if no ArrayList value was found.

You need to check for the list being null before setting it to matches.

respondido 09 mar '12, 16:03

Estás seguro que matches is allocated?
Try this, and look at your logcat output:

if (matches == null) {
    Log.d("YourAppName", "matches is null!");
} else if (matches.contains("TextToBeDetected")) {
    Log.d("YourAppName", "matches does contain TextToBeDetected");
} else {
    Log.d("YourAppName", "matches does not contain TextToBeDetected");
}

respondido 09 mar '12, 16:03

comments like this (which do not resolve PO's problem) should be added as comments, not answers. - GETah

I figured the user might want to have some test code, as s/he mentioned s/he is a newbie. - benoit duffez

That gives you a good reason to answer in a comment, not an answer :) - GETah

That may give me a good reason, but that doesn't give me formatting options :-) is it so bad that I answered? I think it's the point of this whole website. - benoit duffez

data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)

will return the value of an item that previously added with putExtra() or null if no ArrayList value was found. In your case, maybe it returned null

respondido 09 mar '12, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.