程序一直报错,请指出我哪里写错了String[] headName = null;
for (int i=0;i<data.size ();i++){
a=((String)data.get (i)).split (" ");
int count=a.length;
if(i==0){
for(int m=0;m<count;m++){
headName[m]=a[m];
System.out.println(headName);
}所报错误是headName[m]=a[m]这一行是空指针,我测试了a数组,里面是有内容的,不知道我在哪里写错了,请各位指点一下
a=((String)data.get (i)).split (" ");
int count=a.length;
headName=new String[count];
if(i==0){
for(int m=0;m<count;m++){
headName[m]=a[m];
System.out.println(headName);
}最近这种问题很多啊
就发生过,不知道是为什么,按理说headName应该是由几个字符串组成的数组。请问这是为什么?
所以你System.out.println(headName);时实际上输出的是地址值应该是System.out.println(headName[m]);
public void setHeadName (String[] a) {
int m;
int count=a.length;
headName=new String[count];
for( m=0;m<count;m++)
headName[m]=a[m];
}
public String[] getHeadName(){
return headName;
}
我想错误是发生在最后一个语句上的,但是我将怎么在调用get函数时取得每一个headName的值呢?
String[] headName;
public void setHeadName (String[] a) {
int m;
int count=a.length;
headName=new String[count];
for( m=0;m<count;m++)
headName[m]=a[m];
}
public String[] getHeadName(){
return headName;
}
public static void main(String[] args){
TestStr ts=new TestStr();
String[] s={"hello","world","str1","str2","str3",};
ts.setHeadName(s);
String[] d=ts.getHeadName();
for(int i=0;i<d.length;i++)
System.out.println(d[i]);
}
}
String[] d=ts.getHeadName();先要得到这个类的一个对象实例
文本文件如下:
WellName X-Coord Y-Coord KB TopDepth BottomDepth Symbol
34/10-A-10 60491.7 35683.0 56.6 0 2534 Oil
34/10-A-15 61757.5 30147.1 23.6 0 3133 Gas要写成如下形式的xml:<wellhead>
<well name="34/10-A-10" X-Coord="60491.7" Y-Coord="35683.0" KB="56.6" TopDepth="0" BottomDepth= "2534" Symbol="Oil"/>
<well name="34/10-A-15" X-Coord=" 61757.5" Y-Coord=" 30147.1 " KB="23.6" TopDepth="0" BottomDepth= " 3133 " Symbol="Gas"/>
<wellhead/>