Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toCharArray()" because "this.name" is null at p.<init>(Demo.java:33) at Demo.main(Demo.java:3)
public class Demo {
public static void main(String [] args){
p l =new p(" Tupi ");
l.i();
l.x();
l.s1();
//System.out.print(l.c+" "+l.b);
int e = 0;
int g = 5;
char [] a = {' ','a','b','c','n',' '};
System.out.print(String.copyValueOf(a,e,g+1));
}
}
class p{
private String name;
p(String name){
this.name = name;
}
public int getC() {
return c;
}
public int getB() {
return b;
}
private int c;
private int b;
char [] a = name.toCharArray();
public void i() {
for (int i = 0;i<a.length;i++){
if (a[i]!=' '){
c=i;
break;
}
}
}
public void x() {
for (int i = a.length-1;i>0;i--){
if (a[i]!=' '){
b=i;
break;
}
}
}
public void s1(){
System.out.println(String.copyValueOf(a,this.getC(),this.getB()));
}
}
我人都傻掉了 ,设计思路是这样的 我想模拟trim 去空格,我想先传一个字符串进去,然后再用 字符串转字符数组,然后再用字符数组循环去除空格,再用字符数组转换字符串!
现在问题是 我穿进去一个字符串,为什么说我是null值 搞不懂 我是小白!大神救救孩子吧!!