新手初学java,写了一段小程序,把字符串里的值赋给一个二维数组
packagelianxi01;publicclasslianxi03{publicstaticvoidmain(String[]args){double[][]d=nu...
package lianxi01;
public class lianxi03 {
public static void main(String[] args) {
double[][] d = null;
String s="12,32,22,44;45,98,23;3232,44445;43,54";
String[] sFirst=s.split(";");
for(int i=0;i<=sFirst.length-1;i++){
String[] sSecond=sFirst[i].split(",");
for (int j=0;j<=sSecond.length-1;j++){
d[i][j]=Double.parseDouble(sSecond[j]);
System.out.println(d[i][j]);
}
}
}
}运行提示
错误是Exception in thread "main" java.lang.NullPointerException( d[i][j]=Double.parseDouble(sSecond[j]);这行)
请帮忙看看,谢谢 展开
public class lianxi03 {
public static void main(String[] args) {
double[][] d = null;
String s="12,32,22,44;45,98,23;3232,44445;43,54";
String[] sFirst=s.split(";");
for(int i=0;i<=sFirst.length-1;i++){
String[] sSecond=sFirst[i].split(",");
for (int j=0;j<=sSecond.length-1;j++){
d[i][j]=Double.parseDouble(sSecond[j]);
System.out.println(d[i][j]);
}
}
}
}运行提示
错误是Exception in thread "main" java.lang.NullPointerException( d[i][j]=Double.parseDouble(sSecond[j]);这行)
请帮忙看看,谢谢 展开
2个回答
展开全部
修改了一下:
public class lianxi03 {
public static void main(String[] args) {
double[][] d = new double[4][4]; //这里改一下
String s="12,32,22,44;45,98,23;3232,44445;43,54";
String[] sFirst=s.split(";");
for(int i=0; i<=sFirst.length-1; i++) {
String[] sSecond=sFirst[i].split(",");
for (int j=0; j<=sSecond.length-1; j++) {
d[i][j] = Double.parseDouble(sSecond[j]);
System.out.println(d[i][j]);
}
}
}
}
追问
找到错误了,没有指定二维数组的长度
d=new double [sFirst.length][];
d[i]=new double[sSecond.length];
谢谢。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询