0

I have an application which requires to check if a String is present in an array of String type, before adding it so as to avoid duplication. To do this, I wrote the following function:

public boolean arrayHas(String[] arr, String str)
{
    for(int i=0;i<arr.length;i++)
    {
        if(arr[i].equals(str))
            return true;
    }
    return false;
}

To invoke this function, I'm using:

if(!arrayHas(contacts,str))
{
    contacts[i] = str;
    i++;
}

contacts and str are declared as follows

public static  String contacts[] = new String[]{};
String str = "";
Bundle bun = getIntent().getExtras();
str = bun.getString("key");

Elements are added to 'contacts' only through the main code, it is empty at the beginning. I tried adding a toast to display the value of 'str' received through the intent and it works fine. But I'm getting a NullPointerException in the 'if' statement in the arrayHas function. Could someone help me out?

6
  • 3
    Have you initialized your array elements? Most probably this is the reason. Commented Nov 9, 2012 at 6:41
  • 1
    NPE shows presence of a null pointer somewhere, can easily be found using a debugger. (?) Commented Nov 9, 2012 at 6:41
  • I tried initializing the array, didn't work! Commented Nov 9, 2012 at 6:45
  • @AbishekRamasubramaniancan you post the code for initialization bcoz nothing seems wrong here Commented Nov 9, 2012 at 6:49
  • Initializing array in the sense, fill it with strings. And also, you haven't initialized your String str;. Or you have skipped that code? Commented Nov 9, 2012 at 6:49

7 Answers 7

1

Seems that you haven't initialized the array with elements. So all of them are NULL.

In you arrayHas function check if the element you are comparing with is a null or not.

if(arr[i] != null && arr[i].equals(str) )
{
    // do your operation
}

Also before calling arrayHas function in

if(arrayHas(contacts,str)) { }

put a check if contacts is null or not.

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

Comments

1

Two issues:

First: add a null check in if as:

      if(arr[i] != null && arr[i].equals(str))

because that position may not have assigned with a valid string yet e.g. in the very beginning, no assignment is made and arr[0] is null so comparison will result into NullPointerException.

Second: I think you want to check the not ie. ! condition in this check:

   if(!arrayHas(contacts,str))
   {
     contacts[i] = str;
     i++;
   }

1 Comment

and can keep the index number that show at which index last string is added, to avoid unnecessary iteration.
0

If you want to avoid duplication, use a java.util.Set<String>, it will take care of it for you. You can always uso toArray() if you really need an array later on.

If you care about the order of your elements, you can also use a java.util.List, and check the presence of the element with list.contains(str)

Comments

0

Use this instead :

String contacts[] = new String[10];
String str = "somethiung";
if(Arrays.asList(contacts).contains(str))
{
    contacts[i] = str;
    i++;
}

Arrays.asList(.).contains(.) gives you a much better way to test if a string is present in an array.

By the way make sure that contacts and str are properly initialized.

Comments

0

Just a suggestion to code style. Try to defensive programming., ie. you are checking whether the string str is present in arr in that case always do the check in reverse ie., str.equals(arr[i]), so that unnecessary NPEs wont be raised.

In this case an NPE could be raised at 2 points, if arr is null .length and .equals will throw NPE's. From this its evident that, either arr is null , or arr[i] is null.

Find the method where arr is filled with data, there something is going wrong.

thanks

Comments

0

You must not have initialized your contacts[] but it might be the case like str[0]=null but str[1]="something";

in that case change

arr[i].equals(str) to `str.equals(arr[i])` as str is less likely to be null

Comments

0

- I think you are trying to find whether a String is a present in the Array of Not.

- First use Collections cause that will be much more flexible in comparision to Array.

- Use Collections.contains() method to find the String if present of not.

For example if you are using List:

List<String> list = new ArrayList<String>();
list.contains("string_to_be_found");

Comments

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.