我创建了包含特征几何和属性的对象:
public class Feature {
Feature(String wkt) {
this.wkt = wkt;
}
private HashMap<Column, String> columnMap;
private String wkt;
public String getWKT() {
return wkt;
}
public void addAttribute(Column column, String value) {
columnMap.put(column, value);
}
public String getAttribute(String column) {
return columnMap.get(column) ;
}
public Map<Column, String> getAttributes(){
return columnMap;
}
}
Wkt 是一个几何图形。ColumnMap 是对象,包含一个属性为 HashMap:
public class Column {
private String columnName;
Column(String columnName) {
this.columnName = columnName;
}
public String getName() {
return columnName;
}
}
现在我说:
columnList = new ArrayList<Column>(columns);
......
Feature feature= new Feature(WKT);
for(int p=0;p<columnList.size();p++){
for(int k=0;k<=ViewObject.getMIDInfo(totalObjects).length;k++){
if(p==k){
System.out.println("Column "+columnList.get(p).getName()+" Value "+ ViewObject.getMIDInfo(totalObjects)[k].toString());
//feature.addAttribute(columnList.get(p), ViewObject.getMIDInfo(totalObjects)[k].toString());
}
}
}
并获得输出:
Column id Value 22
Column kadnumm Value "66-41-0707001-19"
所以我怎么理解columnList
和ViewObject.getMIDInfo(totalObjects)
不空。在此之后我改变:
//feature.addAttribute(columnList.get(p), ViewObject.getMIDInfo(totalObjects)[k].toString());
至:
feature.addAttribute(columnList.get(p), ViewObject.getMIDInfo(totalObjects)[k].toString());
并获得例外:
Column id Value 22
java.lang.NullPointerException
at objects.Feature.addAttribute(Feature.java:18)
at objects.MIFParser.findRegion(MIFParser.java:181)
at objects.MIFParser.instanceNextObject(MIFParser.java:66)
at Read.main(Read.java:40)
我如何理解 NullPointerException 意味着我尝试使用空对象?怎么了?
Ps 对不起,我的英语可能很糟糕,尤其是标题。
更新
好吧,我添加了这个:this.columnMap = new HashMap<Column, String>();
在 FEature 类构造函数中。
但现在我尝试做:
System.out.println(feature.getAttribute("id")+" "+feature.getAttribute("kadnumm"));
和输出:
null null
有什么问题?