-5
class MinecraftStartInfo
{
    public StringBuilder sb { get; set; }
    public string workingDir { get; set; }

    public void add(String param)
    {
        param = param.Replace("$USERPROFILE$", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
        param = param.Replace("$_WORKINGDIR$", workingDir);
        sb.Append(param);
    }

    public void addWithoutChecks(String param)
    {
        sb.Append(param);
    }

    public void add(int param)
    {
        sb.Append(param + "");
    }

    public string get()
    {
        return sb.ToString().Replace(" , mainclass", ", mainclass").Trim();
    }
}

Ошибка на sb.Append(param) в MinecraftStartInfo.add():

NullPointerException / Ссылка на объект не указывает на экземпляр объекта.

Как исправить ошибку?

Regent
  • 19,134
AGrief
  • 403

2 Answers2

1
sb = new StringBuilder(); // В конструкторе
Qwertiy
  • 123,725
-2
class MinecraftStartInfo
{
    private readonly StringBuilder sb = new StringBuilder();
    private readonly userProfile;

    public MinecraftStartInfo()
    {
        this.userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    }

    public MinecraftStartInfo(string workingDirectory) : this()
    {
        if (string.IsNullOrEmpty(workingDirectory))
            throw new ArgumentException("workingDirectory");

        this.WorkingDir = workingDirectory;
    }

    public string WorkingDir { get; private set; }

    public void AddWithCheck(string param)
    {
        if (string.IsNullOrEmpty(param))
            throw new ArgumentException("param");

        param = param.Replace("$USERPROFILE$", this.userProfile);
        param = param.Replace("$_WORKINGDIR$", this.WorkingDir);

        sb.Append(param);
    }

    public void Add(string param)
    {
        if (string.IsNullOrEmpty(param))
            throw new ArgumentException("param");

        sb.Append(param);
    }

    public void Add(int param)
    {
        sb.Append(param);
    }

    public override string ToString()
    {
        this.sb.Replace(" , mainclass", ", mainclass");
        return this.sb.ToString().Trim();
    }
}
pavelip
  • 5,670