{"record":{"id":"b767a0a9512d201c","repo":"Baseflow/PhotoView","slug":"matrix-cannot-be-null","errorCode":null,"errorMessage":"Matrix cannot be null","messagePattern":"Matrix cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"photoview/src/main/java/com/github/chrisbanes/photoview/PhotoViewAttacher.java","lineNumber":281,"sourceCode":"    }\n\n    public void setOnSingleFlingListener(OnSingleFlingListener onSingleFlingListener) {\n        this.mSingleFlingListener = onSingleFlingListener;\n    }\n\n    @Deprecated\n    public boolean isZoomEnabled() {\n        return mZoomEnabled;\n    }\n\n    public RectF getDisplayRect() {\n        checkMatrixBounds();\n        return getDisplayRect(getDrawMatrix());\n    }\n\n    public boolean setDisplayMatrix(Matrix finalMatrix) {\n        if (finalMatrix == null) {\n            throw new IllegalArgumentException(\"Matrix cannot be null\");\n        }\n        if (mImageView.getDrawable() == null) {\n            return false;\n        }\n        mSuppMatrix.set(finalMatrix);\n        checkAndDisplayMatrix();\n        return true;\n    }\n\n    public void setBaseRotation(final float degrees) {\n        mBaseRotation = degrees % 360;\n        update();\n        setRotationBy(mBaseRotation);\n        checkAndDisplayMatrix();\n    }\n\n    public void setRotationTo(float degrees) {\n        mSuppMatrix.setRotate(degrees % 360);","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/Baseflow/PhotoView/blob/565505d5cb84f5977771b5d2ccb7726338e77224/photoview/src/main/java/com/github/chrisbanes/photoview/PhotoViewAttacher.java#L263-L299","documentation":"PhotoView's setDisplayMatrix(RectF/Matrix) API rejects a null Matrix argument immediately with an IllegalArgumentException. The attacher needs a real Matrix to copy into its supplementary matrix (mSuppMatrix.set(finalMatrix)) so it can recompute and constrain the image display rect. Passing null would corrupt the zoom/drag state, so the library fails fast at the API boundary.","triggerScenarios":"Calling photoView.setDisplayMatrix(null) or PhotoViewAttacher.setDisplayMatrix(null) directly. Also happens indirectly when code builds a Matrix from getDisplayRect()/getDisplayMatrix() results that were never initialized, or when a matrix variable from a bundle/config is null and is forwarded without a check.","commonSituations":"Restoring a saved zoom state (e.g. from onSaveInstanceState where no matrix was saved yet), refactoring code that used to pass a new Matrix() but was 'simplified', or calling setDisplayMatrix before the view has any state in a ViewPager/RecyclerView holder where the matrix field is transient and null after reattachment.","solutions":["Check for null before calling: if (matrix != null) photoView.setDisplayMatrix(matrix);","If restoring from a saved state, default to a fresh matrix: photoView.setDisplayMatrix(savedMatrix != null ? savedMatrix : new Matrix());","Verify the code path that produces the matrix (getDisplayMatrix(new Matrix()) returns a copy; it never returns null when given an initialized Matrix) and fix the producer so it cannot emit null."],"exampleFix":"// before\nphotoView.setDisplayMatrix(restoredMatrix);\n\n// after\nif (restoredMatrix != null) {\n    photoView.setDisplayMatrix(restoredMatrix);\n}","handlingStrategy":"validation","validationCode":"Matrix m = restoreZoomMatrix(savedInstanceState);\nif (m != null) {\n    photoView.setDisplayMatrix(m);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat Matrix as non-nullable in your API: annotate the parameter @NonNull and let the compiler warn.","Default null matrix sources to new Matrix() at the boundary where they enter (bundle restore, config objects).","Prefer the read-modify-write pattern: photoView.getDisplayMatrix(m); m.postScale(...); photoView.setDisplayMatrix(m); — the producer then can never be null."],"tags":["photoview","android","null-check","matrix","zoom","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"565505d5cb84f5977771b5d2ccb7726338e77224","analyzedAt":"2026-08-14T14:25:26.552Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}