所以我是第一次学习Java。以下代码在另一个类中调用NullPointer
时会引发异常square.toString()
(其中 square 是此类中定义的对象的实例),我对为什么这不起作用有点模糊。有人可以向我解释吗?
public class SquareBuilder {
String box;
String[] parts;
final private String beam = "----";
final private String collumn = "|";
private int size;
public SquareBuilder(int firstSize)
{
size = firstSize;
}
public static String repeatString(String s, int n)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++)
{
sb.append(s);
}
return sb.toString();
}
public void changeSize(int newSize)
{
size = newSize;
}
public String toString( )
{
parts[0] = repeatString(beam,size) + "\n";
parts[1] = collumn + repeatString(" ",4*size-2) + collumn + "\n";
box = parts[0] + repeatString(parts[1],size-2) + parts[0];
return box;
}
}