so I'm learning Java for the first time. The following code throws a NullPointer exception when square.toString() is called in another class (where square is an instance of the object defined in this class), and I'm a little foggy about why this doesn't work. Can someone explain that to me?
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;
}
}