我的代码是:
public class javaTest { public static void main(String[] args) {
System.out.print("Starting...");
NewIdentityList[] new1 = new NewIdentityList[1];
new1[0].List[0]= "v1"; //这里这样写好像不行
System.out.print("v1="+new1[0].List[0]);
}
public class NewIdentityList
{
public String [] List=null;
}
}执行此文件,出现错误:
java.lang.NullPointerException
at ds.Public.javaTest.main(javaTest.java:21)
Exception in thread "main" 请问该如何给数组new1[0].List[0]赋值?
public class javaTest { public static void main(String[] args) {
System.out.print("Starting...");
NewIdentityList[] new1 = new NewIdentityList[1];
new1[0].List[0]= "v1"; //这里这样写好像不行
System.out.print("v1="+new1[0].List[0]);
}
public class NewIdentityList
{
public String [] List=null;
}
}执行此文件,出现错误:
java.lang.NullPointerException
at ds.Public.javaTest.main(javaTest.java:21)
Exception in thread "main" 请问该如何给数组new1[0].List[0]赋值?
//你没有对成员变量List分配空间;其值为null;另外成员变量名不要取List,这是类库中的一个类;new1[0].List[0]=new String[5];
new1[0].List[0]= "v1";
public String[] List; public int ghy = 0; NewIdentityList(int i) {
List = new String[i];
}
}public class ghyghost { public static void main(String[] args) {
NewIdentityList new1[] = new NewIdentityList[1];
new1[0]=new NewIdentityList(1);
new1[0].List[0]="my csdn is ghyghost";
System.out.print("v1="+new1[0].List[0]);
}}
class NewIdentityList {
public String[] List; public int ghy = 0; NewIdentityList(int i) {
List = new String[i];
}
}
NewIdentityList new1[] = new NewIdentityList[1];
new1[0]=new NewIdentityList(1);
new1[0].List[0]="my csdn is ghyghost";
System.out.print("v1="+new1[0].List[0]);
}}
编译没有问题,但是执行有问题to huyc_fly() :
这样还是有问题的to ghyghost(爱国人士)
你这样是可以的,不过能否解释一下为什么原来代码不行?即使我按照huyc_fly改了如下的例子还不行?
public class javaTest { public static void main(String[] args) {
System.out.print("Starting...");
NewIdentityList[] new1 = new NewIdentityList[1];
new1[0].List = new String[5];
new1[0].List[0]= "v1"; //这里这样写好像不行
System.out.print("v1="+new1[0].List[0]);
}
public class NewIdentityList
{
public String [] List=null;
}
}
new1[0].List[0]= "v1"; //这里这样写好像不行
System.out.print("v1="+new1[0].List[0]);这样写当然不行因为:NewIdentityList[] new1 = new NewIdentityList[1];这样写的意思是:我要在二个位置上存放类型为NewIdentityList的实例,但是!这样写是相当于盖房子打个地基,但房子并没有盖起来,没有盖房子你就new1[0].List[0]= "v1";这样操作房子,当然不可以。所以必须得先弄地基:NewIdentityList new1[] = new NewIdentityList[1];再盖房子:new1[0]=new NewIdentityList(1);。。最后对房子进行操作:new1[0].List[0]="my csdn is ghyghost";。。不知道我这样解释你明白不。。
改正如下:
new1[0].List=new String[5];
new1[0].List[0]= "v1";
public static void main(String[] args) {
System.out.print("Starting...");
NewIdentityList[] new1 = new NewIdentityList[1];
new1[0] = new Test2().new NewIdentityList();
new1[0].List = new String[5];
new1[0].List[0]= "v1"; //这里这样写好像不行
System.out.print("v1="+new1[0].List[0]);
}
public class NewIdentityList
{
public String [] List=null;
}
}哥们,NewIdentityList是个静态内部类来着。必须用父类的实例来实例化,同时
public class javaTest {public static void main(String[] args) {
System.out.print("Starting...");
javaTest jt=new javaTest(); //由于内部类要与外部类相关联,所以要创建外部类的对象
javaTest.NewIdentityList[] new1 = new javaTest.NewIdentityList[1];//声明一个内部类的数组;这里并没有实际创建内部类的对象;
new1[0]=jt.new NewIdentityList();//让数组的第一个元素只想一个内部类的对象;
new1[0].List = new String[2];//创建内部类的数组成员变量的对象,分配空间
new1[0].List[0]= "v1"; //这里这样写好像不行
new1[0].List[0]= "test";
System.out.print("v1="+new1[0].List[0]);
System.out.print("v1="+new1[0].List[1]);
}public class NewIdentityList
{
public String [] List=null;
}
}
的修改也是对的,不过他是把你的内部类改成了一般类;楼主的主要困惑是因为没有搞清楚,对象数组的原因;为举例方便,假定NewIdentityList是一般的类,NewIdentityList[] myobject=new NewIdentityList[3];这里并没有创建3个NewIdentityList的对象,实际上,这里只是创建了一个对象数组,数组的元素是可以指向NewIdentityList对象的引用;也就是说你现在操作数组的元素是要报空指针异常,因为此时每一个元素的值就是null;所以你还得对没有个数组元素创建所只想的对象;--》new1[0].List = new String[2];//创建内部类的数组成员变量的对象,分配空间
我改了一下,把实例化父类的代码去掉,也可以执行:
public class javaTest2 { public static void main(String[] args) {
System.out.print("Starting...");
NewIdentityList[] new1 = new NewIdentityList[1];//声明一个内部类的数组;这里并没有实际创建内部类的对象;
new1[0]=new NewIdentityList();//让数组的第一个元素只想一个内部类的对象;
new1[0].List = new String[1];//创建内部类的数组成员变量的对象,分配空间
new1[0].List[0]= "v1"; //这里这样写好像不行
System.out.print("v1="+new1[0].List[0]);
} static class NewIdentityList
{
public String [] List=null;
}
}