查错:


11


 


1.                               public class WhatHappens {
2.                               public static void main(String[] args) {
3.                                   StringBuffer[] stringBuffers = new StringBuffer[10];
4.                                    for (int i = 0; i < stringBuffers.length; i ++) {
5.                                       stringBuffers[i].append("StringBuffer at index " + i);
6.                                   }
7.                               }
8.                           }


 


Answer: 程序在第定义了数组 StringBuffers[] ,但在使用数组 StringBuffers 对象时没有初始化,要初始化后才能使用。如下第 5 行为对数组对象初始化


1.                           public class ThisHappens {
2.                               public static void main(String[] args) {
3.                                   StringBuffer[] stringBuffers = new StringBuffer[10];
4.                                        for (int i = 0; i < stringBuffers.length; i ++) {
5.                                        stringBuffers[i] = new StringBuffer();
6.                                       stringBuffers[i].append("StringBuffer at index " + i);
7.                                   }
8.                               }
9.                           }