nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.72k stars 1.43k forks source link

XSSFSheet.CopySheet - fix null reference error #1332

Closed Bykiev closed 5 months ago

Bykiev commented 5 months ago

Closes #346

tonyqus commented 5 months ago

Do you figure out why clonedSheet.AddRelation(rel.Id, r) throws exception?

Bykiev commented 5 months ago

I believe it's a copy-paste error, but still not 100% sure in my fix, so, it should be reviewed carefully.

https://github.com/nissl-lab/npoi/blob/200c5d35fed9218c100e3d2f96e78aaf6cae5579/ooxml/POIXMLDocumentPart.cs#L419-L420

In line 419 the relation is not exists and then it's not checked for null, which cause NRE. Because relation is not exists, we shouldn't call this method at all

Bykiev commented 5 months ago

It seems I was wrong and AddRelation call shouldn't be removed, just added a null check

cp1319421 commented 5 months ago

So do I have a new version to try?@Bykiev

tonyqus commented 5 months ago

@cp1319421 NPOI doesn't have nightly build. Please pend the next release. It can be a few month later.

tonyqus commented 5 months ago

@Bykiev I debug into the code and found that the exception occurs in cloning drawing relationship.

It tried to look for rId1. And drawing1.xml.rels looks like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.png"/>
    <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="../charts/chart2.xml"/>
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="../charts/chart1.xml"/>
</Relationships>

In other words, rId1 is there. For some reason , it's not found. We may have to dig into this.