I'm writing an indexOf method for a class called BasicArrayList.

i have two private instance variables an Object[] called list, and an int called size.

public int indexOf(Object element)
 67    {
 68       for(int i = 0; i < size; i++)
 69       {
 70          if(list[i].equals(element))
 71          {
 72             return i; 
 73          }
 74       }      
 75  
 76       return -1; 
 77    }

it keeps telling me i have a null pointer exception.. but i dont know how else to change up the code. i need to use the equals method as well.

in this case i thought the method should do this: Using the provided element's equals method, this method returns the index of the first element in the list that is equal to the provided element, if any.

Thanks again for your help. Its much appreciated.

Recommended Answers

All 9 Replies

Your code is OK but the problem is how you define your "list" variable. The problem shows that at some points, inside your "list" variable is not initialized properly.

There are missing links in this code. We need to take more look of your other piece of code.

public class BasicArrayList implements BasicList<Object>
 11 {
 12    //private instance variables
 13   
 14    public static final int DEFAULT_CAPACITY = 10;
 15    private Object[] list;
 16    private int size;
 17 
 18    //constructors
 19   
 20    public BasicArrayList()
 21    {
 22       size = 0;
 23       list = new Object[DEFAULT_CAPACITY];
 24    }
 25   
 26    public BasicArrayList(int capacity)
 27    {
 28       if(capacity < DEFAULT_CAPACITY)
 29       {
 30          size = 0;
 31          list = new Object[DEFAULT_CAPACITY];
 32       }
 33      
 34       else
 35       {
 36          size = 0;
 37          list = new Object[capacity];
 38       }
 39    }
 40   
 41    //methods
 42   
 43    public Object get(int index)
 44    {
 45       return list[index];
 46    }
 47 
 48    public int size()
 49    {
 50       return size;
 }
 52 
 53    public Object set(int index, Object o)
 54    {
 55       return new Object();
 56    }
 57 
 58    public Object remove(int index)
 59    {
 60       Object del = new Object();
 61       list[index] = del;
 62 
 63       size--;
 64 
 65       return list[index];
 66    }
 67 
 68    public void trimToSize()
 69    {
 70    }
 71 
 72    public int indexOf(Object element)
 73    {
 74       for(int i = 0; i < size; i++)
 75       {
 76          if(list[i].equals(element))
 77          {
 78             return i;
 79          }
 80       }
 81 
 82       return -1;
 83    }
 84 
 85    public int capacity()
 86    {
 87       return list.length;
 88    }
 89 
 90    public boolean contains(Object o)
 91    {
 92       return list.equals(o);
 93    }
 94 
 95    public void clear()
 96    {
 97       new BasicArrayList();
 98    }
public void add(int index, Object o)
101    {
102       list[index] = o;
103       size++;
104    }
105 
106    public void add(Object o)
107    {
108       list[size] = o;
109       size++;
110    }
111 
112    public Object unusualmethodForTestDriverOnly()
113    {
114       return list;
115    }
116 
117 }

this is what im working on.

Edit:
Err I'm wrong again. James is correct. >_< I'm not thinking this morning.

Your set() method doesn't really set the object to the list but try to return an empty Object???

When you instantiate an array of Object references in Java, the array is initially full of nulls. (If you don't believe me try printing element[0] of a newly initialised object array)

Edit:
Err I'm wrong again. James is correct. >_< I'm not thinking this morning.

Your set() method doesn't really set the object to the list but try to return an empty Object???

no it doesnt, i havent written that yet, its just a place holder until i write it. But yes i understand that initially its an array with nulls. My question would be how to get around that?

You can easily bypass the problem by enhancing your if test:

if(list[i] != null && list[i].equals(element))

but it would be better to find out why you have nulls where you expect Objects anyway.

ps @Taywin: don't worry, we've all had mornings like that :-)

The answer to that would be i havent filled the array with anything yet? Since im just writing the class to be run by a TestDriver later.

I assumed that size contains the current number of Objects stored the array, so

for(int i = 0; i < size; i++)

will prevent you from trying to access elements that have not yet been populated.
If that's not how your size variable works, maybe that's a problem.

Ah yes, something i did not see. I think maybe using something else along the lines of a list.length would be more appropriate there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.