有谁知道为什么我不能将字符 append 到这个 StringBuffer 数组(在下面的示例中),有人可以告诉我我需要如何完成吗?
public class test {
public static void main(String args[]){
StringBuffer[][] templates = new StringBuffer[3][3];
templates[0][0].append('h');
}
}
我对此代码的输出是:
output: Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:6)
这对我很有帮助,所以如果您知道任何解决方案,请回复此
最佳答案
下面的语句只会声明一个数组,但不会初始化其元素:
StringBuffer[][] templates = new StringBuffer[3][3];
在尝试将内容 append 到数组元素之前,您需要初始化它们。不这样做将导致 NullPointerException
添加此初始化
templates[0][0] = new StringBuffer();
然后追加
templates[0][0].append('h');
关于java - 无法将 char append 到 StringBuffer 二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21582128/