stanfordnlp/CoreNLP · error · NullPointerException

Image does not have width

Error message

Image does not have width

What it means

SceneGraphImage.readFromJSON requires a "width" integer field; when it is absent the NullPointerException "Image does not have width" is thrown at src/edu/stanford/nlp/scenegraph/image/SceneGraphImage.java:92. Like the height check, it enforces that the JSON image record contains complete dimension metadata.

Solutions

  1. Add the "width" integer field to the JSON record.
  2. Re-download complete metadata from the dataset source.
  3. Pre-validate containsKey("width")/isNull before parsing and repair or skip bad records.
  4. Fill width from an external image-metadata lookup when only dimensions are missing.

Example fix

// before
SceneGraphImage img = SceneGraphImage.readFromJSON(obj); // NPE, no width
// after
if (!obj.containsKey("width") || obj.isNull("width")) {
  obj.put("width", imageWidthLookup(obj.getInt("id")));
}
SceneGraphImage img = SceneGraphImage.readFromJSON(obj);
Defensive patterns

Strategy: validation

Validate before calling

if (!obj.containsKey("width") || obj.isNull("width")) {
  throw new IllegalArgumentException("Image record missing width: " + obj.getInt("id"));
}

Type guard

boolean hasWidth(JsonObject o) { return o.containsKey("width") && !o.isNull("width"); }

Try / catch

try {
  return SceneGraphImage.readFromJSON(obj);
} catch (RuntimeException e) {
  log.warn("Skipping image without width: " + obj.getInt("id"));
  return null;
}

Prevention

When it happens

Trigger: Calling readFromJSON on a JSON object missing "width" or with "width": null — typically the same malformed/partial records that lack "height".

Common situations: Custom scene-graph JSON exports omitting width; records where dimensions were stripped to save space; dataset preprocessing bugs.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/8d19daefeb55eeba. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/scenegraph/image/SceneGraphImage.java:92

      JsonArray attributes = obj.getJsonArray("attributes");
      for (JsonObject object: attributes.getValuesAs(JsonObject.class)) {
        img.addAttribute(SceneGraphImageAttribute.fromJSONObject(img, object));
      }

      JsonArray relationships = obj.getJsonArray("relationships");
      for (JsonObject relation: relationships.getValuesAs(JsonObject.class)) {
        img.addRelationship(SceneGraphImageRelationship.fromJSONObject(img, relation));
      }

      img.id = obj.getInt("id");
      Number height = obj.getInt("height");
      Number width = obj.getInt("width");
      if (height == null) {
        throw new NullPointerException("Image does not have height");
      }
      if (width == null) {
        throw new NullPointerException("Image does not have width");
      }
      img.height = height.intValue();
      img.width = width.intValue();

      img.url = obj.getString("url");

      return img;
    } catch (RuntimeException e) {
      throw new RuntimeException("Couldn't parse \n" + json, e);
    }
  }

  @SuppressWarnings("unchecked")
  public String toJSON() {
    JsonObjectBuilder json = Json.createObjectBuilder();
    json.add("id", this.id);
    json.add("height", this.height);
    json.add("width", this.width);

View on GitHub (pinned to 1b7edd19c4)