我有一个字符数组。我必须在第三个字符之后插入“,”。我已经编写了以下代码。
public class Comma {
char [] str = {'1','2','3','4','5','6','7','8','9'};
char [] buf = null;
int size = str.length;
int c=1;
public void insert()
{
for(int i=0;i<size+10;i++)
{
c++;
if(c==3)
{
buf[i]=',';
i++;
c=1;
}
buf[i]=str[i];
}
for(int i=0;i<buf.length;i++)
System.out.println("Final String is"+buf[i]);
}
public static void main(String args[])
{
Comma c = new Comma();
c.insert();
}
}
当我运行它时,它显示空指针异常。我哪里做错了?