下面这段JAVA代码为什么会报空指针异常?
publicclassDemoTest1{privateString[]s;publicString[]getS(){returns;}publicvoidsetS(St...
public class DemoTest1 {
private String[] s;
public String[] getS() {
return s;
}
public void setS(String[] s) {
this.s = s;
}
public static void main(String[] args) {
DemoTest1 test1=new DemoTest1();
String[] s2 = test1.getS();
for (int i = 0; i < 3; i++) {
s2[i]="sss"+i;
System.out.println(s2[i]);
}
}
} 展开
private String[] s;
public String[] getS() {
return s;
}
public void setS(String[] s) {
this.s = s;
}
public static void main(String[] args) {
DemoTest1 test1=new DemoTest1();
String[] s2 = test1.getS();
for (int i = 0; i < 3; i++) {
s2[i]="sss"+i;
System.out.println(s2[i]);
}
}
} 展开
1个回答
展开全部
数组s的大小没有定,还是空的。可以这样改一下:
public class DemoTest1 {
private String[] s = new String[3]; //这里改一下
public String[] getS() {
return s;
}
public void setS(String[] s) {
this.s = s;
}
public static void main(String[] args) {
DemoTest1 test1 = new DemoTest1();
String[] s2 = test1.getS();
for (int i = 0; i < 3; i++) {
s2[i] = "sss" + i;
System.out.println(s2[i]);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询