I'm trying to make a hash table with java. I share a long code only what I need. I use the for loop to fill the table with -1.But I am getting the error.
---Exception in thread "main" java.lang.NullPointerException at datahashtable04.DataHashTable04.main(DataHashTable04.java:68) Java Result: 1---
class Data {
int index, value;
public Data(int index, int value) {
this.index = index;
this.value = value;
}
}
public static void main(String[] args) {
Data a []= new Data[27];
for (int i = 0; i <a.length; i++) {
a[i].index=-1;
a[i].value=-1;
}
}
Data a []= new Data[27];
Everything in this array is null until you initialize each element. You need to call the constructor in your loop:
for (int i = 0; i <a.length; i++) {
a[i] = new Data(-1, -1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.