Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDNumberTreeNode.java
===================================================================
--- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDNumberTreeNode.java	(Revision 1366001)
+++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDNumberTreeNode.java	(Arbeitskopie)
@@ -24,6 +24,8 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
@@ -33,22 +35,24 @@
 /**
  * This class represents a PDF Number tree. See the PDF Reference 1.7 section
  * 7.9.7 for more details.
- * 
+ *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>,
  *         <a href="igor.podolskiy@ievvwi.uni-stuttgart.de">Igor Podolskiy</a>
  * @version $Revision: 1.4 $
  */
 public class PDNumberTreeNode implements COSObjectable
 {
+    private static final Log log = LogFactory.getLog( PDNumberTreeNode.class );
+
     private COSDictionary node;
-    private Class<?> valueType = null;
+    private Class<? extends COSObjectable> valueType = null;
 
     /**
      * Constructor.
      *
      * @param valueClass The PD Model type of object that is the value.
      */
-    public PDNumberTreeNode( Class<?> valueClass )
+    public PDNumberTreeNode( Class<? extends COSObjectable> valueClass )
     {
         node = new COSDictionary();
         valueType = valueClass;
@@ -60,7 +64,7 @@
      * @param dict The dictionary that holds the name information.
      * @param valueClass The PD Model type of object that is the value.
      */
-    public PDNumberTreeNode( COSDictionary dict, Class<?> valueClass )
+    public PDNumberTreeNode( COSDictionary dict, Class<? extends COSObjectable> valueClass )
     {
         node = dict;
         valueType = valueClass;
@@ -93,12 +97,11 @@
      */
     public List getKids()
     {
-
-        List retval = null;
+        List<PDNumberTreeNode> retval = null;
         COSArray kids = (COSArray)node.getDictionaryObject( COSName.KIDS );
         if( kids != null )
         {
-            List pdObjects = new ArrayList();
+            List<PDNumberTreeNode> pdObjects = new ArrayList();
             for( int i=0; i<kids.size(); i++ )
             {
                 pdObjects.add( createChildNode( (COSDictionary)kids.getObject(i) ) );
@@ -114,8 +117,22 @@
      *
      * @param kids The children of this number tree.
      */
-    public void setKids( List kids )
+    public void setKids( List<? extends PDNumberTreeNode> kids )
     {
+        if (kids != null && kids.size() > 0)
+        {
+            PDNumberTreeNode firstKid = kids.get(0);
+            PDNumberTreeNode lastKid = kids.get(kids.size() - 1);
+            Integer lowerLimit = firstKid.getLowerLimit();
+            this.setLowerLimit(lowerLimit);
+            Integer upperLimit = lastKid.getUpperLimit();
+            this.setUpperLimit(upperLimit);
+        }
+        else if ( node.getDictionaryObject( COSName.NUMS ) == null )
+        {
+            // Remove limits if there are no kids and no numbers set.
+            node.setItem( COSName.LIMITS, null);
+        }
         node.setItem( COSName.KIDS, COSArrayList.converterToCOSArray( kids ) );
     }
 
@@ -125,7 +142,7 @@
      * @param index The index in the number tree.
      *
      * @return The value corresponding to the index.
-     * 
+     *
      * @throws IOException If there is a problem creating the values.
      */
     public Object getValue( Integer index ) throws IOException
@@ -139,15 +156,22 @@
         else
         {
             List kids = getKids();
-            for( int i=0; i<kids.size() && retval == null; i++ )
+            if ( kids != null )
             {
-                PDNumberTreeNode childNode = (PDNumberTreeNode)kids.get( i );
-                if( childNode.getLowerLimit().compareTo( index ) <= 0 &&
+                for( int i=0; i<kids.size() && retval == null; i++ )
+                {
+                    PDNumberTreeNode childNode = (PDNumberTreeNode)kids.get( i );
+                    if( childNode.getLowerLimit().compareTo( index ) <= 0 &&
                         childNode.getUpperLimit().compareTo( index ) >= 0 )
-                {
-                    retval = childNode.getValue( index );
+                    {
+                        retval = childNode.getValue( index );
+                    }
                 }
             }
+            else
+            {
+                log.warn("NumberTreeNode does not have \"nums\" nor \"kids\" objects.");
+            }
         }
         return retval;
     }
@@ -158,12 +182,12 @@
      * depend on where this class is being used.
      *
      * @return A map of COS objects.
-     * 
+     *
      * @throws IOException If there is a problem creating the values.
      */
     public Map getNumbers()  throws IOException
     {
-        Map<Integer,Object> indices = null;
+        Map<Integer, Object> indices = null;
         COSArray namesArray = (COSArray)node.getDictionaryObject( COSName.NUMS );
         if( namesArray != null )
         {
@@ -193,10 +217,10 @@
      */
     protected Object convertCOSToPD( COSBase base ) throws IOException
     {
-        Object retval = null;
+        COSObjectable retval = null;
         try
         {
-            Constructor<?> ctor = valueType.getConstructor( new Class[] { base.getClass() } );
+            Constructor<? extends COSObjectable> ctor = valueType.getConstructor( new Class[] { base.getClass() } );
             retval = ctor.newInstance( new Object[] { base } );
         }
         catch( Throwable t )
@@ -225,7 +249,7 @@
      *
      * @param numbers The map of names to objects.
      */
-    public void setNumbers( Map<Integer,Object> numbers )
+    public void setNumbers( Map<Integer, ? extends COSObjectable> numbers )
     {
         if( numbers == null )
         {
@@ -266,7 +290,7 @@
     {
         Integer retval = null;
         COSArray arr = (COSArray)node.getDictionaryObject( COSName.LIMITS );
-        if( arr != null )
+        if( arr != null && arr.get(0) != null )
         {
             retval = Integer.valueOf(arr.getInt( 1 ));
         }
@@ -286,8 +310,16 @@
             arr = new COSArray();
             arr.add( null );
             arr.add( null );
+            node.setItem( COSName.LIMITS, arr );
         }
-        arr.setInt( 1, upper.intValue() );
+        if ( upper != null)
+        {
+            arr.setInt( 1, upper.intValue() );
+        }
+        else
+        {
+            arr.set( 1, null );
+        }
     }
 
     /**
@@ -299,7 +331,7 @@
     {
         Integer retval = null;
         COSArray arr = (COSArray)node.getDictionaryObject( COSName.LIMITS );
-        if( arr != null )
+        if( arr != null && arr.get(0) != null )
         {
             retval = Integer.valueOf(arr.getInt( 0 ));
         }
@@ -319,7 +351,15 @@
             arr = new COSArray();
             arr.add( null );
             arr.add( null );
+            node.setItem( COSName.LIMITS, arr );
         }
-        arr.setInt( 0, lower.intValue() );
+        if ( lower != null )
+        {
+            arr.setInt( 0, lower.intValue() );
+        }
+        else
+        {
+            arr.set( 0, null );
+        }
     }
 }
