]> source.dussan.org Git - poi.git/commitdiff
Ensure that XWPFParagraph.getDocument() is not null
authorMaxim Valyanskiy <maxcom@apache.org>
Thu, 28 Oct 2010 13:02:43 +0000 (13:02 +0000)
committerMaxim Valyanskiy <maxcom@apache.org>
Thu, 28 Oct 2010 13:02:43 +0000 (13:02 +0000)
    (Null getDocument() causes NPE when looking for comments in XWPFWordExtractorDecorator)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1028283 13f79535-47bb-0310-9956-ffa450edef68

16 files changed:
src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/java/org/apache/poi/POIXMLFactory.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFactory.java
src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/IBody.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFComment.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFactory.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java

index 6c360990afe590195387ae8305da58b701ee4a8c..993413c1b0d74dcc612d39ebb5c9152792cefbd7 100644 (file)
@@ -37,7 +37,7 @@ import org.apache.poi.openxml4j.opc.*;
  * @author Yegor Kozlov
  */
 public class POIXMLDocumentPart {
-    private static POILogger logger = POILogFactory.getLogger(POIXMLDocumentPart.class);
+    private static final POILogger logger = POILogFactory.getLogger(POIXMLDocumentPart.class);
 
     public static final XmlOptions DEFAULT_XML_OPTIONS;
     static {
@@ -86,7 +86,23 @@ public class POIXMLDocumentPart {
         this.packagePart = part;
         this.packageRel = rel;
     }
-    
+
+    /**
+     * Creates an POIXMLDocumentPart representing the given package part, relationship and parent
+     * Called by {@link #read(POIXMLFactory, java.util.Map)} when reading in an exisiting file.
+     *
+     * @param parent - Parent part
+     * @param part - The package part that holds xml data represenring this sheet.
+     * @param rel - the relationship of the given package part
+     * @see #read(POIXMLFactory, java.util.Map)
+     */
+    public POIXMLDocumentPart(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel){
+        this.relations = new LinkedList<POIXMLDocumentPart>();
+        this.packagePart = part;
+        this.packageRel = rel;
+        this.parent = parent;
+    }
+
     /**
      * When you open something like a theme, call this to
      *  re-base the XML Document onto the core child of the
@@ -274,7 +290,7 @@ public class POIXMLDocumentPart {
                 }
 
                 if (!context.containsKey(p)) {
-                               POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);
+                               POIXMLDocumentPart childPart = factory.createDocumentPart(this, rel, p);
                                childPart.parent = this;
                                addRelation(childPart);
                     if(p != null){
index a434765bad2c506e8e12f46e63611abe61f58ab6..6eebbd9c2e89eb2a6c177b7175e959fbe7a40965 100644 (file)
@@ -30,11 +30,12 @@ public abstract class POIXMLFactory {
      * Create a POIXMLDocumentPart from existing package part and relation. This method is called
      * from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document
      *
+     * @param parent parent part
      * @param rel   the package part relationship
      * @param part  the PackagePart representing the created instance
      * @return A new instance of a POIXMLDocumentPart.
      */
-     public abstract POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part);
+     public abstract POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part);
 
     /**
      * Create a new POIXMLDocumentPart using the supplied descriptor. This method is used when adding new parts
index 34400ee436a0b788fa0440225686fbb359fceec5..7855e424583878914535eae1e4001baf8320d0fa 100644 (file)
@@ -34,7 +34,7 @@ import org.apache.poi.util.POILogger;
  * @author Yegor Kozlov
  */
 public final class XSSFFactory extends POIXMLFactory  {
-    private static POILogger logger = POILogFactory.getLogger(XSSFFactory.class);
+    private static final POILogger logger = POILogFactory.getLogger(XSSFFactory.class);
 
     private XSSFFactory(){
 
@@ -46,7 +46,8 @@ public final class XSSFFactory extends POIXMLFactory  {
         return inst;
     }
 
-    public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
+    @Override
+    public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
         POIXMLRelation descriptor = XSSFRelation.getInstance(rel.getRelationshipType());
         if(descriptor == null || descriptor.getRelationClass() == null){
             logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
@@ -62,6 +63,7 @@ public final class XSSFFactory extends POIXMLFactory  {
         }
     }
 
+    @Override
     public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
         try {
             Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
index 3316435190b11bd5d179b8a44b36e00e49f1dcc1..3a5b77effd94336474883fd9e559168ecded9a45 100644 (file)
@@ -109,7 +109,7 @@ public class XWPFHeaderFooterPolicy {
                        PackagePart hdrPart = doc.getPartById(ref.getId());
                        HdrDocument hdrDoc = HdrDocument.Factory.parse(hdrPart.getInputStream());
                        CTHdrFtr hdrFtr = hdrDoc.getHdr();
-                       XWPFHeader hdr = new XWPFHeader(hdrFtr);
+                       XWPFHeader hdr = new XWPFHeader(doc, hdrFtr);
 
                        // Assign it
                        Enum type = ref.getType();
@@ -119,7 +119,7 @@ public class XWPFHeaderFooterPolicy {
                        // Get the footer
                        CTHdrFtrRef ref = sectPr.getFooterReferenceArray(i);
                        PackagePart ftrPart = doc.getPartById(ref.getId());
-                       XWPFFooter ftr = new XWPFFooter(
+                       XWPFFooter ftr = new XWPFFooter(doc, 
                                        FtrDocument.Factory.parse(ftrPart.getInputStream()).getFtr());
 
                        // Assign it
@@ -460,6 +460,6 @@ public class XWPFHeaderFooterPolicy {
                shapeTextPath.setString(text);
                pict.set(group);
                // end watermark paragraph
-               return new XWPFParagraph(p, null);
+               return new XWPFParagraph(p, doc);
        }
 }
index 6525bfd0ccdffe0396a20ef984f33344178feda5..a85ec7408acedd5e31b2bca602e0f13e6d6ba89b 100644 (file)
@@ -62,8 +62,7 @@ public interface IBody {
         *  the text of the header or footer.
         */
     public List<XWPFParagraph> getParagraphs();
-       
-       
+
        /**
         * Return the table(s) that holds the text
         *  of the IBodyPart, for complex cases
@@ -123,6 +122,11 @@ public interface IBody {
         * @param cell
         */
        XWPFTableCell getTableCell(CTTc cell);
-       
+
+    /**
+     * Return XWPFDocument
+     */
+    public XWPFDocument getXWPFDocument();
+
 }
 
index 1f8e799fc240aad4c8da7d372c2a6c132a5b4f68..d193983c7918468209b8561cc08b65cb29aa811a 100644 (file)
@@ -31,7 +31,7 @@ public class XWPFComment
     protected String author;
     protected StringBuffer text;
     
-    public XWPFComment(CTComment comment)
+    public XWPFComment(CTComment comment, XWPFDocument document)
     {
         text = new StringBuffer();
         id = comment.getId().toString();
@@ -39,7 +39,7 @@ public class XWPFComment
         
         for(CTP ctp : comment.getPList())
         {
-            XWPFParagraph p = new XWPFParagraph(ctp, null);
+            XWPFParagraph p = new XWPFParagraph(ctp, document);
             text.append(p.getText());
         }
     }
index 7335c89086450bf904eeca65ef3b57bf6e2c3801..d4c22045918b35609502144b6508ff10432f8f5e 100644 (file)
@@ -165,25 +165,18 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
                 String relation = p.getPackageRelationship().getRelationshipType();
                 if(relation.equals(XWPFRelation.STYLES.getRelation())){
                        this.styles = (XWPFStyles) p;
-                }
-                else if(relation.equals(XWPFRelation.NUMBERING.getRelation())){
+                } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())){
                        this.numbering = (XWPFNumbering) p;
-
-                }
-                else if(relation.equals(XWPFRelation.FOOTER.getRelation())){
+                } else if (relation.equals(XWPFRelation.FOOTER.getRelation())){
                        footers.add((XWPFFooter)p);
-                }
-                else if(relation.equals(XWPFRelation.HEADER.getRelation())){
+                } else if (relation.equals(XWPFRelation.HEADER.getRelation())){
                        headers.add((XWPFHeader)p);
-                }
-
-                else if(relation.equals(XWPFRelation.COMMENT.getRelation())){
+                } else if (relation.equals(XWPFRelation.COMMENT.getRelation())){
                     CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream());
                     for(CTComment ctcomment : cmntdoc.getComments().getCommentList()) {
-                        comments.add(new XWPFComment(ctcomment));
+                        comments.add(new XWPFComment(ctcomment, this));
                     }
-                }
-                else if(relation.equals(XWPFRelation.SETTINGS.getRelation())){
+                } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())){
                        settings = (XWPFSettings)p;
                 }
             }
@@ -254,6 +247,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
     /**
      * Create a new CTWorkbook with all values set to default
      */
+    @Override
     protected void onDocumentCreate() {
         hyperlinks = new ArrayList<XWPFHyperlink>();
         comments = new ArrayList<XWPFComment>();
@@ -429,6 +423,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
     /**
      * Get the document's embedded files.
      */
+    @Override
     public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
         List<PackagePart> embedds = new LinkedList<PackagePart>();
 
@@ -1116,4 +1111,8 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
                }
                return tableRow.getTableCell(cell);
        }
+
+    public XWPFDocument getXWPFDocument() {
+        return this;
+    }
 }//end class
index b6c641b4adad7e5b690fccc1598a3ab868138f08..c6b2c2d3e4ccf484db59a3dc2a09965fe45c25bb 100644 (file)
@@ -33,7 +33,7 @@ import org.apache.poi.util.POILogger;
  */
 public final class XWPFFactory extends POIXMLFactory  {
 
-    private static POILogger logger = POILogFactory.getLogger(XWPFFactory.class);
+    private static final POILogger logger = POILogFactory.getLogger(XWPFFactory.class);
 
     private XWPFFactory(){
 
@@ -45,7 +45,8 @@ public final class XWPFFactory extends POIXMLFactory  {
         return inst;
     }
 
-    public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
+    @Override
+    public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
         POIXMLRelation descriptor = XWPFRelation.getInstance(rel.getRelationshipType());
         if(descriptor == null || descriptor.getRelationClass() == null){
             logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
@@ -54,13 +55,19 @@ public final class XWPFFactory extends POIXMLFactory  {
 
         try {
             Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
-            Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(PackagePart.class, PackageRelationship.class);
-            return constructor.newInstance(part, rel);
+            try {
+                Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(POIXMLDocumentPart.class, PackagePart.class, PackageRelationship.class);
+                return constructor.newInstance(parent, part, rel);
+            } catch (NoSuchMethodException e) {
+                Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(PackagePart.class, PackageRelationship.class);
+                return constructor.newInstance(part, rel);
+            }
         } catch (Exception e){
             throw new POIXMLException(e);
         }
     }
 
+    @Override
     public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
         try {
             Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
index 1bce85fb47efe3a805e6fa7abd8007cd55064fc5..73834d767216417814c294e2c42e92854f67e68d 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Map;
 
 import javax.xml.namespace.QName;
 
+import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.xmlbeans.XmlCursor;
@@ -41,13 +42,12 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
  * Sketch of XWPF footer class
  */
 public class XWPFFooter extends XWPFHeaderFooter {
-       public XWPFFooter() {
-               super();
-       }
-       
-       
-       public XWPFFooter(CTHdrFtr hdrFtr) throws IOException {
-               super(hdrFtr);
+    public XWPFFooter() {
+        super();
+    }
+
+       public XWPFFooter(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException {
+               super(doc, hdrFtr);
                bodyElements = new ArrayList<IBodyElement>();
                paragraphs = new ArrayList<XWPFParagraph>();
                tables = new ArrayList<XWPFTable>();
@@ -69,8 +69,8 @@ public class XWPFFooter extends XWPFHeaderFooter {
         getAllPictures();
        }
 
-       public XWPFFooter(PackagePart part, PackageRelationship rel) throws IOException {
-               super(part, rel);
+       public XWPFFooter(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) throws IOException {
+               super(parent, part, rel);
        }
        
        /**
@@ -149,5 +149,4 @@ public class XWPFFooter extends XWPFHeaderFooter {
                public BodyType getPartType() {
                        return BodyType.FOOTER;
                }
-
 }
index 624a740c95c6467570d89712404968ff3a7d12f4..d49aa5d7ecdbc7c1e5463380c3bd8b00c76988ae 100644 (file)
@@ -42,17 +42,16 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
  * Sketch of XWPF header class
  */
 public class XWPFHeader extends XWPFHeaderFooter {
-       
-       public XWPFHeader() {
-               super();
-       }
-       
-       public XWPFHeader(PackagePart part, PackageRelationship rel) throws IOException {
-               super(part, rel);
+    public XWPFHeader() {
+        super();
+    }
+
+    public XWPFHeader(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) throws IOException {
+               super(parent, part, rel);
        }
        
-       public XWPFHeader(CTHdrFtr hdrFtr) throws IOException {
-               super(hdrFtr);
+       public XWPFHeader(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException {
+               super(doc, hdrFtr);
                paragraphs = new ArrayList<XWPFParagraph>();
                tables = new ArrayList<XWPFTable>();
                XmlCursor cursor = headerFooter.newCursor();
index 0bdff96a85d4dc44b38318384ee7a2f3b2ee2f58..5aedc15f9b98cc2eef866d7598497314726ab9e9 100644 (file)
@@ -51,18 +51,31 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
        protected XWPFDocument document;
        protected List<IBodyElement> bodyElements;
        
-       protected XWPFHeaderFooter(CTHdrFtr hdrFtr){
+       protected XWPFHeaderFooter(XWPFDocument doc, CTHdrFtr hdrFtr){
+        if (doc==null) {
+            throw new NullPointerException();
+        }
+
+        document = doc;
                headerFooter = hdrFtr;
                readHdrFtr();
        }
-       protected XWPFHeaderFooter() {
-          this(CTHdrFtr.Factory.newInstance());
-       }
 
-       public XWPFHeaderFooter(PackagePart part, PackageRelationship rel) throws IOException {
-               super(part, rel);
+    protected XWPFHeaderFooter() {
+        headerFooter = CTHdrFtr.Factory.newInstance();
+        readHdrFtr();
+    }
+
+
+    public XWPFHeaderFooter(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) throws IOException {
+               super(parent, part, rel);
                this.document = (XWPFDocument)getParent();
-      onDocumentRead();
+
+        if (this.document==null) {
+            throw new NullPointerException();
+        }
+
+        onDocumentRead();
        }
        
     @Internal
@@ -523,5 +536,11 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
                return tableRow.getTableCell(cell);
        }
        
-    
+    public XWPFDocument getXWPFDocument() {
+        if (document!=null) {
+            return document;
+        } else {
+            return (XWPFDocument)getParent();
+        }
+    }
 }//end class
index 6a299fafdfb988e119c7c7c523bcea1a9786c082..7b33d48c0480ee473afa2c976d991f402e274322 100644 (file)
@@ -55,7 +55,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment;
  * Sketch of XWPF paragraph class
  */
 public class XWPFParagraph implements IBodyElement{
-    private CTP paragraph;
+    private final CTP paragraph;
     protected IBody part;
     /** For access to the document's hyperlink, comments, tables etc */
     protected XWPFDocument document;
@@ -63,22 +63,16 @@ public class XWPFParagraph implements IBodyElement{
     
     private StringBuffer footnoteText = new StringBuffer();
 
-    public XWPFParagraph(CTP prgrph) {
-        this(prgrph, null);
-    }
-
-
     public XWPFParagraph(CTP prgrph, IBody part) {
         this.paragraph = prgrph;
         this.part = part;
         
-        // We only care about the document (for comments,
-        //  hyperlinks etc) if we're attached to the
-        //  core document
-        if(part instanceof XWPFDocument) {
-           this.document = (XWPFDocument)part;
+        this.document = part.getXWPFDocument();
+
+        if (document==null) {
+            throw new NullPointerException();
         }
-        
+
         runs = new ArrayList<XWPFRun>();
 
        // Get all our child nodes in order, and process them
index 7685a6f00a61a5880c7a8c74531c4e7a33d4b38d..656bf307a8680f514bbd1c40db2d2945a5a49541 100644 (file)
@@ -30,8 +30,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
 
 
 public class XWPFTableCell implements IBody {
-
-    private CTTc ctTc;
+    private final CTTc ctTc;
     protected List<XWPFParagraph> paragraphs = null;
     protected List<XWPFTable> tables = null;
     protected List<IBodyElement> bodyElements = null;
@@ -256,7 +255,7 @@ public class XWPFTableCell implements IBody {
         * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
         */
        public IBody getPart() {
-               return (IBody) tableRow.getTable().getPart();
+               return tableRow.getTable().getPart();
        }
 
 
@@ -351,4 +350,8 @@ public class XWPFTableCell implements IBody {
                }
                return tableRow.getTableCell(cell);
        }
+
+    public XWPFDocument getXWPFDocument() {
+        return part.getXWPFDocument();
+    }
 }// end class
index e495df7676ea68eec9f3478114ae79fa1fe0daa7..4e7f788e67d4e27d1c96a6103bf2585469794108 100644 (file)
@@ -57,7 +57,7 @@ public final class TestPOIXMLDocument extends TestCase {
         public TestFactory() {
             //
         }
-        public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
+        public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
             return new POIXMLDocumentPart(part, rel);
         }
 
index 8526a2005b9a597d77d206bcb72a6784ac738e51..b46f8d32acca0a68704b3082cda492bb155e4a3d 100644 (file)
@@ -76,12 +76,12 @@ public final class TestXWPFHeader extends TestCase {
                CTText t3 = ctR3.addNewT();
                t3.setStringValue("Second paragraph for the footer");
 
-               XWPFParagraph p1 = new XWPFParagraph(ctP1);
+               XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);
                XWPFParagraph[] pars = new XWPFParagraph[1];
                pars[0] = p1;
 
-               XWPFParagraph p2 = new XWPFParagraph(ctP2);
-               XWPFParagraph p3 = new XWPFParagraph(ctP3, null);
+               XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
+               XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);
                XWPFParagraph[] pars2 = new XWPFParagraph[2];
                pars2[0] = p2;
                pars2[1] = p3;
@@ -98,7 +98,7 @@ public final class TestXWPFHeader extends TestCase {
                assertNotNull(policy.getDefaultFooter());
                // ....and that the footer object captured above contains two
                // paragraphs of text.
-               assertEquals(footer.getParagraphs().size(), 2);
+               assertEquals(2, footer.getParagraphs().size());
                
                // As an additional check, recover the defauls footer and
                // make sure that it contains two paragraphs of text and that
@@ -111,9 +111,9 @@ public final class TestXWPFHeader extends TestCase {
                   paras[i++] = p;
                }
                
-               assertEquals(paras.length, 2);
-               assertEquals(paras[0].getText(), "First paragraph for the footer");
-               assertEquals(paras[1].getText(), "Second paragraph for the footer");
+               assertEquals(2, paras.length);
+               assertEquals("First paragraph for the footer", paras[0].getText());
+               assertEquals("Second paragraph for the footer", paras[1].getText());
        }
 
        public void testSetWatermark() {
index 6e8fe1e8ea626b35c083f235c3e95459b7c2853d..91f43f4dffc64efd976a14abb0c692a319e546d2 100644 (file)
@@ -42,15 +42,16 @@ public class TestXWPFTable extends TestCase {
     }
 
     public void testConstructor() {
+        XWPFDocument doc = new XWPFDocument();
         CTTbl ctTable = CTTbl.Factory.newInstance();
-        XWPFTable xtab = new XWPFTable(ctTable, null);
+        XWPFTable xtab = new XWPFTable(ctTable, doc);
         assertNotNull(xtab);
         assertEquals(1, ctTable.sizeOfTrArray());
         assertEquals(1, ctTable.getTrArray(0).sizeOfTcArray());
         assertNotNull(ctTable.getTrArray(0).getTcArray(0).getPArray(0));
 
         ctTable = CTTbl.Factory.newInstance();
-        xtab = new XWPFTable(ctTable, null, 3, 2);
+        xtab = new XWPFTable(ctTable, doc, 3, 2);
         assertNotNull(xtab);
         assertEquals(3, ctTable.sizeOfTrArray());
         assertEquals(2, ctTable.getTrArray(0).sizeOfTcArray());
@@ -59,6 +60,7 @@ public class TestXWPFTable extends TestCase {
 
 
     public void testGetText() {
+        XWPFDocument doc = new XWPFDocument();
         CTTbl table = CTTbl.Factory.newInstance();
         CTRow row = table.addNewTr();
         CTTc cell = row.addNewTc();
@@ -67,12 +69,14 @@ public class TestXWPFTable extends TestCase {
         CTText text = run.addNewT();
         text.setStringValue("finally I can write!");
 
-        XWPFTable xtab = new XWPFTable(table, null);
+        XWPFTable xtab = new XWPFTable(table, doc);
         assertEquals("finally I can write!\n", xtab.getText());
     }
 
 
     public void testCreateRow() {
+        XWPFDocument doc = new XWPFDocument();
+
         CTTbl table = CTTbl.Factory.newInstance();
         CTRow r1 = table.addNewTr();
         r1.addNewTc().addNewP();
@@ -84,7 +88,7 @@ public class TestXWPFTable extends TestCase {
         r3.addNewTc().addNewP();
         r3.addNewTc().addNewP();
 
-        XWPFTable xtab = new XWPFTable(table, null);
+        XWPFTable xtab = new XWPFTable(table, doc);
         assertEquals(3, xtab.getNumberOfRows());
         assertNotNull(xtab.getRow(2));
 
@@ -95,16 +99,18 @@ public class TestXWPFTable extends TestCase {
         assertEquals(2, table.getTrArray(0).sizeOfTcArray());
 
         //check creation of first row
-        xtab = new XWPFTable(CTTbl.Factory.newInstance(), null);
+        xtab = new XWPFTable(CTTbl.Factory.newInstance(), doc);
         assertEquals(1, xtab.getCTTbl().getTrArray(0).sizeOfTcArray());
     }
 
 
     public void testSetGetWidth() {
+        XWPFDocument doc = new XWPFDocument();
+        
         CTTbl table = CTTbl.Factory.newInstance();
         table.addNewTblPr().addNewTblW().setW(new BigInteger("1000"));
 
-        XWPFTable xtab = new XWPFTable(table, null);
+        XWPFTable xtab = new XWPFTable(table, doc);
 
         assertEquals(1000, xtab.getWidth());
 
@@ -113,9 +119,11 @@ public class TestXWPFTable extends TestCase {
     }
 
     public void testSetGetHeight() {
+        XWPFDocument doc = new XWPFDocument();
+
         CTTbl table = CTTbl.Factory.newInstance();
 
-        XWPFTable xtab = new XWPFTable(table, null);
+        XWPFTable xtab = new XWPFTable(table, doc);
         XWPFTableRow row = xtab.createRow();
         row.setHeight(20);
         assertEquals(20, row.getHeight());