简体   繁体   English

添加到HashMapList时,“主线程中的异常” java.lang.NullPointerException

[英]“Exception in thread ”main" java.lang.NullPointerException when adding to a HashMapList

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 HashMapList将其元素保留在HashMap中),当我调用add方法时,此错误消息将显示在conole“线程“主”中的异常” 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==> 谢谢,我已经解决了第一个问题,但是当我调用add方法时,例如==>

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

and Book class is==> 和Book类是==>

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); 当我想使用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] 此句子将显示在concol中: [org.bihe.com1112.Book @ 1fb8ee3,org.bihe.com1112.Book @ 61de33,org.bihe.com1112.Book @ 14318bb]

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 = ...". 在代码中寻找“ map = ...”的任何地方。

look at your constructor. 看你的构造函数。

new HashMap<Integer, V>();

should be 应该

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

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. 您的Book类没有提供自定义的重写toString()方法。 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. 因此,它使用从Object继承的Object ,该Object仅返回由类的全名和对象的hashCode组成的字符串,这就是您所看到的。 You should override the toString() method if you want to see something different. 如果要查看其他内容,则应重写toString()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM