Hello, all.
I'm writing a Java program that adds entry objects to an ArrayList. The entries each store a value and a key (it's a decoding program).
My add() method accepts a key and a value, and is supposed to add it to the ArrayList in a new entry. If another entry has the key being passed, it is supposed to return the value stored at the key previously.
I had written this method using a for-each loop, but my professor (I'm a first-year Software Engineering student) wants us to use Collections.binarysearch().
Calling collections.sort() before calling binarysearch throws a null pointer exception. Searching the sun forums here, somebody said that binary search and sort will both throw null pointer exceptions if the collection being searched has null objects (can't sort a list with null objects).
I put in a if that checks to see if the objects being passed in are null (see code below) but the .sort() is still throwing a null pointer.
Thanks for help.
public V add(K key, V value) {
if (key == null || value == null)
{
System.out.println("null was added")
System.exit(0);
}
Entry temp = null;
Entry search = new Entry(key, value);
Collections.sort(table);
int test = Collections.binarySearch(table, search);
if (table.get(test)!= null)
{
temp = table.get(test);
}
table.add(test, search);
return temp.getValue();
}