java.lang.NullPointerException异常处理问题
public static String []split (String s1,String s2){StringBuilder[]sp = new StringBuilder[s1.length ()];
int number = 0;
for(int i=0;i < s1.length ();i++){
for(int j =0;j < s2.length ();j ++){
if (s1. charAt(i) !=s2.charAt(j):
sp[number ].append (s1. charAt(i) );//报错行
else
sp[number++ ].append ( s1. charAt(i));
number++ ;
}
}
number ++;
String []spr = new String [number ];
for(int i=0;i < number; i++){
spr [i] = sp. toString ();
}
return spr;
}
------解决方案--------------------sp没有初始化赋值,所以在调用sp[number].append时会报
空指针,在new StringBuilder[s1.length ()];后面加上
for(int i=0;i<sp.length;i++){
sp[i] = new StringBuilder();
}