0

I want to know if an Object in an ArrayList is null. If it's null, then it shouldn't do anything. Example:

if(!(theList.get(theIndexofObject) == null)){
   do something...
}
else{
   do nothing...
}

This doesn't work, because it throws an exception cause of the '.get()'-method. Any ideas to solve this problem?

9
  • 1
    What exception? If it's a NullPointerException, the list is null, not the object. Also, ArrayList#get() asks for an int (the object index), not an object. Commented Jul 4, 2011 at 7:49
  • 1
    are you sure your ArrayList itself isn't null? and you've got your program logic backwards according to what you stated. Commented Jul 4, 2011 at 7:50
  • ArrayList has a get method that takes an object? Commented Jul 4, 2011 at 7:51
  • I'm not sure I understand what you are trying to do. the only get method ArrayList exposes accepts an integer argument. Commented Jul 4, 2011 at 7:53
  • my ArrayList isn't null I know that. and the get-method returns an element at the specified index - sorry if it's confusing cause i wrote object... Commented Jul 4, 2011 at 7:54

7 Answers 7

4

Use the contains() Method of your list:

boolean contains(Object o)
Sign up to request clarification or add additional context in comments.

Comments

3

You are probably confused about how to use the API. Here is a simple example of how it works:

import java.util.ArrayList;
import java.util.List;

public class NullItems {
    public static void main(String[] args) {

        List<Object> items = new ArrayList<Object>();
        items.add("foo");
        items.add(null);
        items.add(25);

        for (int i = 0; i < items.size(); i++) {
            Object item = items.get(i);
            if (item != null) {
                System.out.println(item);
            }
        }

        // or shorter:
        for (Object item : items) {
            if (item != null) {
                System.out.println(item);
            }
        }
    }
}

Comments

1

You are using the get method wrong. You need to pass the index an item is at to the get method. You could use the contains method to see if the object is in the ArrayList.

Example:

if(theList.contains(theObject))
   //do something

Otherwise you could use a try and catch which seems confusing and hard to read so I would strongly not recommend doing the following but have included it to show you:

for(int i=0; i<theList.size(); i++)
{
    try
    {
       if(!(theList.get(i) == null))
       {
           //do something
       }
       else
       {
           //do nothing
       }
    }
    catch(NullPointerException npe)
    {
        //do something else
    }
}

Alternatively use a for-each loop.

Comments

0

In javaScript itemArray.length, for java u have to use ARRAY.size() insted of length function

 var itemArray=//Assign some list of value;
    for (var i = 0; i < itemArray.length; i++){

          if(itemArray[i].value == null){
             Do nothing
              }else{
                 Do something
              }
    }

Comments

0

i think that your arraylist is null chang first condition to:

if(theList!=null && !(theList.get(theIndexofObject) == null)){
    // do something...
}
else{
    // do nothing...
}

Comments

0

The method arrayList.size() returns the number of items in the list - so if the index is greater than or equal to the size(), it doesn't exist.

Comments

-2
if(!(theList.get(theIndexofObject) == null)){
   do something...
}
else{
   do nothing...
}

instead of writing this code.Try in the below format,I think you will get answer

if(theList.get(theIndexofObject)!= null)){
   do something...
}
else{
   do nothing...
}

2 Comments

This has nothing to do with the problem OP is encountering.
You did same thing in reverse order.

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.