Skip to content

Fix NPE in Matrix constructors - #743

Merged
MaximPlusov merged 4 commits into
integrationfrom
matrix
Aug 11, 2026
Merged

Fix NPE in Matrix constructors#743
MaximPlusov merged 4 commits into
integrationfrom
matrix

Conversation

@LonelyMidoriya

@LonelyMidoriya LonelyMidoriya commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of invalid or incomplete matrix data.
    • Documents with null or non-numeric matrix values now safely fall back to the identity matrix.
    • Malformed matrix values generate a warning rather than causing unreliable processing.
    • Valid matrix values continue to be processed normally.
    • Improved reliability when processing documents containing incomplete or malformed matrix values.

@LonelyMidoriya LonelyMidoriya self-assigned this Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4ef15f73-0854-4f32-8615-15c0c3ed1350

📥 Commits

Reviewing files that changed from the base of the PR and between 8d3692d and 184e892.

📒 Files selected for processing (1)
  • wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java

📝 Walkthrough

Walkthrough

Matrix constructors validate all six inputs. Invalid or non-real values trigger a warning and reset the matrix to the identity matrix.

Changes

Matrix validation

Layer / File(s) Summary
Constructor validation
wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java
The constructors iterate over all six inputs, convert valid values, and reset invalid inputs to the identity matrix while logging a warning.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: maximplusov

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the fix to null-pointer exceptions in the Matrix constructors.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch matrix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java (1)

52-80: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider extracting the duplicated validation loop.

The Matrix(COSArray array) and Matrix(List<COSBase> arguments) constructors share nearly identical validation logic, differing only in the element accessor (array.at(i) vs arguments.get(i)) and the log message's source description. Extract a shared private helper that takes an IntFunction<COSObject> (or similar) to reduce duplication.

♻️ Proposed refactor
 public Matrix(COSArray array) {
     matrixArray = new double[SIZE];
-    for (int i = 0; i < SIZE; i++) {
-        COSObject arg = array.at(i);
-        Double d = (arg != null) ? arg.getReal() : null;
-        if (d == null) {
-            matrixArray = new double[] {1, 0, 0, 1, 0, 0};
-            LOGGER.log(Level.WARNING,"Null real value for matrix argument at index {0} in COSArray. " +
-                    "Defaulting to matrix [1,0,0,1,0,0].", i);
-            return;
-        }
-        matrixArray[i] = d;
-    }
+    populateFromValues(i -> array.at(i), "COSArray");
 }

 public Matrix(List<COSBase> arguments) {
     matrixArray = new double[SIZE];
-    for (int i = 0; i < SIZE; i++) {
-        COSBase arg = arguments.get(i);
-        Double d = (arg != null) ? arg.getReal() : null;
-        if (d == null) {
-            matrixArray = new double[] {1, 0, 0, 1, 0, 0};
-            LOGGER.log(Level.WARNING,"Null real value for matrix argument at index {0} in List of arguments. " +
-                            "Defaulting to matrix [1,0,0,1,0,0].", i);
-            return;
-        }
-        matrixArray[i] = d;
-    }
+    populateFromValues(i -> arguments.get(i) != null ? arguments.get(i).getReal() : null, "List of arguments");
+}
+
+private void populateFromValues(java.util.function.IntFunction<Double> valueAt, String source) {
+    for (int i = 0; i < SIZE; i++) {
+        Double d = valueAt.apply(i);
+        if (d == null) {
+            matrixArray = new double[] {1, 0, 0, 1, 0, 0};
+            LOGGER.log(Level.WARNING, "Null real value for matrix argument at index {0} in " + source + ". " +
+                    "Defaulting to matrix [1,0,0,1,0,0].", i);
+            return;
+        }
+        matrixArray[i] = d;
+    }
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java`
around lines 52 - 80, Extract the duplicated matrix-element validation from the
Matrix(COSArray array) and Matrix(List<COSBase> arguments) constructors into a
private helper that accepts an element accessor and source description. Have
each constructor delegate to this helper, preserving the existing default
identity matrix, warning behavior, and COSArray/List access semantics.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java`:
- Around line 59-60: Update the warning log calls in Matrix to use
java.util.logging MessageFormat placeholders, replacing each `{}` in the
messages at the null real-value handling sites with `{0}` while continuing to
pass the argument index `i` as the formatting parameter.

---

Nitpick comments:
In
`@wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java`:
- Around line 52-80: Extract the duplicated matrix-element validation from the
Matrix(COSArray array) and Matrix(List<COSBase> arguments) constructors into a
private helper that accepts an element accessor and source description. Have
each constructor delegate to this helper, preserving the existing default
identity matrix, warning behavior, and COSArray/List access semantics.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7286c19c-fafc-4112-89a3-e72b366fb240

📥 Commits

Reviewing files that changed from the base of the PR and between 4dea032 and 66b57ef.

📒 Files selected for processing (1)
  • wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java

Comment thread wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/Matrix.java Outdated
@MaximPlusov
MaximPlusov merged commit b1b438e into integration Aug 11, 2026
8 checks passed
@MaximPlusov
MaximPlusov deleted the matrix branch August 11, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants