创建最小 json 对象时的空指针

Nullpointer when creating a minimal json-object

考虑以下代码:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Json;    

public class JSonTest
{
    public final String name = "Test";
    public void save()
    {
        FileHandle file = Gdx.files.local("test.json");
        Json json = new Json();

        json.writeObjectStart();
        json.writeValue("name", name);
        json.writeObjectEnd();

        file.writeString(json.toString(), false);
    }
}

现在,当我调用 save() 方法时,我得到了这个非常奇怪的空指针异常,我根本无法确定:

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.badlogic.gdx.utils.Json.writeObjectStart(Json.java:589)
at com.mySuperSecretProject.JSonTest.save(JSonTest.java:8)

我不明白这怎么会发生。我经常进行 JSON 解析,即使在同一个项目中也是如此,但在这里这行不通。我做错了什么?

我什至看过这里:https://github.com/libgdx/libgdx/wiki/Reading-&-writing-JSON 但文档也是这样做的。我不明白那个 Nullpointer 是从哪里来的。

created your Json instance, but haven't provided a Writer.

The writer field is therefore null and causes the NPE.