]> source.dussan.org Git - poi.git/commitdiff
FindBugs fix
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 16 Feb 2014 16:30:24 +0000 (16:30 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 16 Feb 2014 16:30:24 +0000 (16:30 +0000)
- fixed/checked various null pointer related entries
- see http://findbugs.sourceforge.net/bugDescriptions.html#NP_NULL_PARAM_DEREF
- ... NP_NULL_ON_SOME_PATH, NP_NULL_ON_SOME_PATH_EXCEPTION

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

13 files changed:
src/java/org/apache/poi/hssf/usermodel/FontDetails.java
src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/signature/DigitalCertificatePart.java
src/ooxml/java/org/apache/poi/openxml4j/opc/signature/PackageDigitalSignature.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.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/XWPFPictureData.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java
src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/SectionProperties.java

index d9f42496d92383e8ddb0cca1701e7b1840f971e2..29bcf5ac4ba069507cd80ee8d9861396d6469d43 100644 (file)
@@ -66,10 +66,10 @@ public class FontDetails
     public int getCharWidth( char c )
     {
         Integer widthInteger = charWidths.get(Character.valueOf(c));
-        if (widthInteger == null && c != 'W') {
-            return getCharWidth('W');
+        if (widthInteger == null) {
+            return 'W' == c ? 0 : getCharWidth('W');
         }
-        return widthInteger.intValue();
+        return widthInteger;
     }
 
     public void addChars( char[] characters, int[] widths )
index 41ec97e0f26bcf6c498eee9bcd6d49361a572d13..6130311d609cb323fa6fd81961556e91bc0aaa62 100644 (file)
@@ -25,7 +25,7 @@ public class BaseNumberUtils {
 \r
 \r
     public static double convertToDecimal(String value, int base, int maxNumberOfPlaces) throws IllegalArgumentException {\r
-        if (value != null && value.length() == 0) {\r
+        if (value == null || value.length() == 0) {\r
             return 0.0;\r
         }\r
 \r
index 797f12f02a5b07691fbf718cc6c99519729c53df..fffa768fb68379962a11ee70780eb5554ddb92ca 100644 (file)
@@ -36,7 +36,7 @@ public final class DigitalCertificatePart extends PackagePart  {
 
        public DigitalCertificatePart() throws InvalidFormatException{
                super(null, null, new ContentType(""));
-               // Review constructor
+               // TODO: Review constructor
        }
 
        @Override
index 370e52e5128af40714bafbc1534a367b29333d5e..ee9bb64757d77ca14338339ab234903a099e6078 100644 (file)
@@ -30,6 +30,7 @@ public final class PackageDigitalSignature extends PackagePart {
 
        public PackageDigitalSignature() throws InvalidFormatException {
                super(null, null, new ContentType(""));
+               // TODO: Review constructor
        }
 
        @Override
index 6450d92018849f5d5f66d0f309b8aa1f38ee4661..5018c71ea394c9f6b7df6ff4fc771b4bb69508c5 100644 (file)
@@ -71,7 +71,21 @@ import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheets;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookProtection;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
 
 /**
  * High level representation of a SpreadsheetML workbook.  This is the first object most users
@@ -1240,6 +1254,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
         if(sheetname != null && sheetname.length() > 31) sheetname = sheetname.substring(0, 31);
         WorkbookUtil.validateSheetName(sheetname);
+        // findbugs fix - validateSheetName has already checked for null value
+        assert(sheetname != null); 
 
         if (containsSheet(sheetname, sheetIndex ))
             throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
index aeb5491175575deb82968ea17db2b9940793990c..38bf2095e7934d13f27a0c22c6b1c1b39d084b83 100644 (file)
@@ -55,11 +55,13 @@ public class ColumnHelper {
 
         CTCols aggregateCols = CTCols.Factory.newInstance();
         List<CTCols> colsList = worksheet.getColsList();
-        if (colsList != null) {
-            for (CTCols cols : colsList) {
-                for (CTCol col : cols.getColList()) {
-                    cloneCol(aggregateCols, col);
-                }
+        if (colsList == null || colsList.isEmpty()) {
+            return;
+        }
+        
+        for (CTCols cols : colsList) {
+            for (CTCol col : cols.getColList()) {
+                cloneCol(aggregateCols, col);
             }
         }
         
index 406c670c0f951c1c91b1bc573beecedd2fbb1436..978e69d351cbab2f371a027c2da38f3e5b59ec4f 100644 (file)
@@ -65,11 +65,11 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.FootnotesDocument;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
 
@@ -1156,7 +1156,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
                 throw new POIXMLException(e);
             } finally {
                 try {
-                    out.close();
+                    if (out != null) out.close();
                 } catch (IOException e) {
                     // ignore
                 }
index fa65abb2fd180350be52c27d039aee05ebb9c6d4..5fc2fdcd07c055ffcc93bfd63ea7fb85eadf747b 100644 (file)
@@ -257,7 +257,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
                 throw new POIXMLException(e);
             } finally {
                 try {
-                    out.close();
+                    if (out != null) out.close();
                 } catch (IOException e) {
                     // ignore
                 }
index 99bd5d194300c4f1b7954260f03549f0222f3550..526eea7fbe0938e1b252b57f38103fd5ea32d75c 100644 (file)
@@ -41,7 +41,6 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRunTrackChange;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRun;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSmartTagRun;
@@ -472,8 +471,11 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents {
      */
     public void setBorderTop(Borders border) {
         CTPBdr ct = getCTPBrd(true);
+        if (ct == null) {
+            throw new RuntimeException("invalid paragraph state");
+        }
 
-        CTBorder pr = (ct != null && ct.isSetTop()) ? ct.getTop() : ct.addNewTop();
+        CTBorder pr = (ct.isSetTop()) ? ct.getTop() : ct.addNewTop();
         if (border.getValue() == Borders.NONE.getValue())
             ct.unsetTop();
         else
index e819da5dbbb446222546665a02af6630504d93a7..d7f2253586c1c607a51ab292ac28b1cb2f9c2583 100644 (file)
@@ -160,7 +160,7 @@ public class XWPFPictureData extends POIXMLDocumentPart {
                 throw new POIXMLException(e);
             } finally {
                 try {
-                    is.close();
+                    if (is != null) is.close();
                 } catch (IOException e) {
                     throw new POIXMLException(e);
                 }
index bac458087a526741d92d97e6b3ee1b6f19836f79..a02d82ce06f9c67692bc6f18fc40ac97a282c86a 100644 (file)
@@ -29,8 +29,8 @@ import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
 import org.apache.poi.hslf.model.textproperties.TextProp;
 import org.apache.poi.hslf.model.textproperties.TextPropCollection;
 import org.apache.poi.hslf.record.ColorSchemeAtom;
-import org.apache.poi.util.POILogger;
 import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
 
 
 /**
@@ -325,6 +325,7 @@ public final class RichTextRun {
                        // paragraphStyle will now be defined
                }
 
+        assert(paragraphStyle!=null);
                TextProp tp = fetchOrAddTextProp(paragraphStyle, propName);
                tp.setValue(val);
        }
@@ -340,6 +341,7 @@ public final class RichTextRun {
                        // characterStyle will now be defined
                }
 
+               assert(characterStyle!=null);
                TextProp tp = fetchOrAddTextProp(characterStyle, propName);
                tp.setValue(val);
        }
index 70d0c59553367c2e04c587fff261dd4d53fc8839..81ff0c91f6411fd1ad9ba732b5363b145a33c606 100644 (file)
@@ -97,7 +97,7 @@ public final class SprmUtils
         break;
       default:
         //should never happen
-        break;
+        throw new RuntimeException("Invalid sprm type"); 
     }
     LittleEndian.putShort(sprm, 0, instruction);
     list.add(sprm);
index 8205e6be5dc7eeabb724425d82ea03cd734e0c11..031cdb0e7156b6eecfb2b0458591d41bd1627e4d 100644 (file)
@@ -62,7 +62,7 @@ public final class SectionProperties extends SEPAbstractType
                 {
                     continue;
                 }
-                if ( !obj1.equals( obj2 ) )
+                if ( obj1 == null || obj2 == null || !obj1.equals( obj2 ) )
                 {
                     return false;
                 }