stanfordnlp/CoreNLP · error · NullPointerException

Error: a relation was marked between two words where one of…

Error message

Error: a relation was marked between two words where one of the words was not a named entity.  Line causing this error: '${currentLine}'

What it means

When readSentence parses a relation line (column type 3), it looks up the two argument word indices in the sentence's entity mentions. If either side is not a recognized named entity, it throws this NullPointerException, since relations must connect two named entities in the CoNLL04 format.

Solutions

  1. Fix the data file so every relation argument index refers to a word tagged as an entity (Peop/Loc/Org).
  2. Regenerate relations from the corrected NER annotations.
  3. Pre-validate the file: check each relation line's two indices exist in the sentence's entity set before reading.
  4. Wrap readSentence in error handling to skip the offending sentence and continue.

Example fix

// before
Relation r = reader.readSentence(...);
// after
try {
  Relation r = reader.readSentence(...);
} catch (NullPointerException e) {
  logger.warning("skipping sentence: " + e.getMessage());
}
Defensive patterns

Strategy: validation

Validate before calling

// per relation line 'i1 i2 Type': ensure both indices were seen as entities earlier in the sentence
if (!entityIndices.contains(i1) || !entityIndices.contains(i2)) {
  logger.warning("relation over non-entity word, skipping line: " + line);
}

Try / catch

try { readSentence(...); } catch (NullPointerException e) { logger.warning("skipping sentence: " + e.getMessage()); }

Prevention

When it happens

Trigger: A relation line (e.g. '3 5 Work_For') whose indices (3, 5) do not appear as entity words earlier in the same sentence — because the word was tagged 'Other'/not annotated as an entity, or entity indices shifted by parsing errors.

Common situations: Corrupted or hand-edited CoNLL04 files, relation annotations disagreeing with the NER column, feeding partially stripped files where entity lines were removed but relation lines kept.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ie/machinereading/domains/roth/RothCONLL04Reader.java:122

    while (lineIterator.hasNext() && numBlankLinesSeen < 2) {
      String currentLine = lineIterator.next();
      currentLine = currentLine.replace("COMMA", ",");

      List<String> pieces = StringUtils.split(currentLine);
      String identifier;

      int size = pieces.size();
      switch (size) {
      case 1: // blank line between sentences or relations
        numBlankLinesSeen++;
        break;
      case 3: // relation
        String type = pieces.get(2);
        List<ExtractionObject> args = new ArrayList<>();
        EntityMention entity1 = indexToEntityMention.get(pieces.get(0));
        EntityMention entity2 = indexToEntityMention.get(pieces.get(1));
        if (entity1 == null || entity2 == null) {
          throw new NullPointerException("Error: a relation was marked between two words where one of the words was not a named entity.  Line causing this error: '" + currentLine + "'");
        }
        args.add(entity1);
        args.add(entity2);
        Span span = new Span(entity1.getExtentTokenStart(), entity2.getExtentTokenEnd());
        // identifier = "relation" + sentenceID + "-" + sentence.getAllRelations().size();
        identifier = RelationMention.makeUniqueId();
        RelationMention relationMention = new RelationMention(identifier,
            sentence, span, type, null, args);
        AnnotationUtils.addRelationMention(sentence, relationMention);
        break;
      case 9: // token
        /*
         * Roth token lines look like this:
         *
         * 19 Peop 9 O NNP/NNP Jamal/Ghosheh O O O
         */

        // Entities may be multiple words joined by '/'; we split these up

View on GitHub (pinned to 1b7edd19c4)