0

HashMapList keeps its elements inside a HashMap) and when I call add method this error message will be shown in the concole "Exception in thread "main" java.lang.NullPointerException

public class HashMapList<K, V extends Product> extends AbstractList<Product> {
public V element;

public int index;

Map<Integer, V> map;

public HashMapList() {
    super();
    new HashMap<Integer, V>();
 }

// Override
public void add(int index, V element) {
    map.put(new Integer(index), element);

 }
}  

thanks,I have solved the first problem but when I call add method like==>

HashMapList<Integer, Book> list = new HashMapList<Integer, Book>();
list.add(0, new Book("physics"));

and Book class is==>

public class Book extends Product {
public String name = null;
public Book(String name) {
    super(name);

   }
 }

and Product class is==>

public class Product implements Comparable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private String name = null;

public Product(String name) {
    if (name == null)
        throw new NullPointerException();
    this.name = name;
  }

public String getName() {
    return name;
  }

// Override

public int compareTo(Object o) {
    Product product = (Product) o;
    int compare = getName().compareTo(product.name);
    return compare;
  }
 }

And when I want to print this list basically with System.out.println(list); this sentence will be shown in the concole:[org.bihe.com1112.Book@1fb8ee3, org.bihe.com1112.Book@61de33, org.bihe.com1112.Book@14318bb]

3
  • just a few pointers on the code above... when throwing a new exception like when product name is null, always put a string in the constructor that explains why this null should not happen. It may sound trivial sometimes but when your will be reading your logs later you will thank yourself from explaining to yourself our you did not use your class and methods correctly. this will save you oogles of time going from "shy is this exception thrown here... " to "Silly me... forgot to assign the variable before calling constructor" Commented Aug 3, 2009 at 5:32
  • Another pointer : instead of re declaring the name variable at each descendent, either make it protected or create accessors at the product level. The way you are doing it will work most of the time but you will get strange side-effects when code is accessing the child's name variable when it is the parent's variable that contains the data. Right there is a good source of weird null pointer exceptions... Anyways... my two cent :-) Commented Aug 3, 2009 at 5:35
  • What is still wrong after correcting the map = new Hashmap(... issue ? Commented Aug 3, 2009 at 5:52

3 Answers 3

3

You are not assigning anything to map

public HashMapList() {
    super();
    map = new HashMap<Integer, V>();
}

whenever you get a null pointer exception look for where you assign a value to the variable to you are using. Here look for anywhere in your code where you say "map = ...".

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

Comments

2

For your second question, you should really start another thread. It is correctly printing the string representation of your object. Your Book class does not provide a custom overridden toString() method. So it uses the one inherited from Object, which just returns a string made of the full name of the class and the hashCode of the object, which is what you are seeing. You should override the toString() method if you want to see something different.

1 Comment

You mean toString() method and not .toString(), as methods can't start with a period.
2

look at your constructor.

new HashMap<Integer, V>();

should be

map = new HashMap<Integer, V>();

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.