Skip to content

getLastChildElement() always returns first element or null #146

@RHarryH

Description

@RHarryH

Hello,

It looks like there is a bug in OdfElement.getLastChildElement() method:

public OdfElement getLastChildElement() {
    OdfElement lastElementChild = null;
    NodeList nodeList = this.getChildNodes();
    Node node = nodeList.item(0);
    for (int i = nodeList.getLength(); i >= 0; i--) {
      if (node instanceof OdfElement) {
        lastElementChild = (OdfElement) node;
        break;
      }
    }
    return lastElementChild;
  }

The loop there does noting. Only if first child node will be and instance of OdfElement, the node will be returned. Otherwise, the null value will be returned. I think the correct code should be as below:

public OdfElement getLastChildElement() {
    OdfElement lastElementChild = null;
    NodeList nodeList = this.getChildNodes();
    for (int i = nodeList.getLength(); i >= 0; i--) {
      Node node = nodeList.item(i);
      if (node instanceof OdfElement) {
        lastElementChild = (OdfElement) node;
        break;
      }
    }
    return lastElementChild;
  }

Please correct me if my understanding of this method is wrong but I think its name is self explanatory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions