初始化多维数组的问题

时间:2011-07-06 18:02:19

标签: java arrays object casting multidimensional-array

所以对于我的一个项目,我试图将数组存储在数组中的数组等等。 我在这里问了一个前面的问题,如何做到这一点并得到一个没有返回编译器错误的解决方案,但在运行时我得到一个简短的错误消息,程序就结束了。这是我的代码:

Java代码

import java.io.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class Test
{   
    private static int index = 0;
    private static int index2 = 0;
    private static int heading1Instance = 0;
    private static int heading2Instance = 0;
    private static int heading3Instance = 0;
    private static int heading4Instance = 0;
    private static Object[][] docArray = new String[6][];
    private static Object[][] subDocArray = (Object[][])docArray[3][0];
    private static Object[][] sub2DocArray = (Object[][])subDocArray[3][0];
    private static Object[][] sub3DocArray = (Object[][])sub2DocArray[3][0];

    public static void main (String[] args)
    {
        traverse(new File("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items"));
    }

    private static final class SaxHandler extends DefaultHandler 
    {
        private boolean bHeading = false;
        private boolean bBodyText = false;


        // invoked when document-parsing is started:
        public void startDocument() throws SAXException 
        {
            //do nothing...
            //System.out.println("Document processing starting:");
        }

        // notifies about finish of parsing:
        public void endDocument() throws SAXException 
        {
            //do nothing...
            //System.out.println("Document processing finished. \n");
        }

        // we enter to element 'qName':
        public void startElement(String uri, String localName, 
                String qName, Attributes attrs) throws SAXException 
        {
            String headingVal = "";

            // if the qualified name equals "w:Style"...
            if(qName.equalsIgnoreCase("w:pStyle"))
            {
                // the headingVal = the value of the attribute "w:val"
                headingVal = attrs.getValue("w:val");

                // if the headingVal starts with/is "Heading1"
                if (headingVal.startsWith("Heading1", 0))
                {               
                    // increment the heading1Instance to keep track of it...
                    heading1Instance++;

                    // if heading1Instance equals 1, set boolean Heading 
                    // equal to true. Also set index 1 in docArray equal
                    // to the value of headingVal.
                    if (heading1Instance == 1)
                    {
                        bHeading = true;
                        docArray[index+1].equals(headingVal);
                    }

                    // if heading1Instance is greater than 1, set boolean Heading 
                    // equal to true. Also create a new subArray called siblingArray
                    // & set index 1 in siblingArray equal to the value of headingVal.
                    if (heading1Instance > 1)
                    {
                        bHeading = true;
                        Object[][] siblingArray = (Object[][])docArray[4][0];
                        siblingArray[index+1].equals(headingVal);
                    }
                }
                if (headingVal.startsWith("Heading2", 0))
                {               
                    heading2Instance++;
                    bHeading = true;
                    subDocArray[index+1].equals(headingVal);
                }
                if (headingVal.startsWith("Heading3", 0))
                {               
                    heading3Instance++;
                    bHeading = true;
                    sub2DocArray[index+1].equals(headingVal);
                }
                if (headingVal.startsWith("Heading4", 0))
                {               
                    heading4Instance++;
                    bHeading = true;
                    sub3DocArray[index+1].equals(headingVal);
                }
            }

            if(qName.equalsIgnoreCase("w:t"))
            {
                bBodyText = true;
            }
        }

        public void characters(char[] ch, int start, int length) 
        {
             if(bBodyText) 
             {
                 //System.out.print(new String(ch, start, length));
             }
        }

        public void endElement(String uri, String localName, String qName) throws SAXException 
        {
            if(qName.equalsIgnoreCase("w:pStyle"))
            {
                bHeading = false;
            }
            if(qName.equalsIgnoreCase("w:t"))
            {
                bBodyText = false;
            }
        }
     }

     private static void traverse(File directory)
     {
        //Get all files in directory
        File[] files = directory.listFiles();
        for (File file : files)
        {
            if (file.getName().equals("document.xml"))
            {
                try 
                {         
                    // creates and returns new instance of SAX-implementation:
                    SAXParserFactory factory = SAXParserFactory.newInstance();

                    // create SAX-parser...
                    SAXParser parser = factory.newSAXParser();

                    // prints out the current working proposal, traversing up the directory structure
                    // System.out.println(file.getParentFile().getParentFile().getParentFile().getName());
                    // .. define our handler:
                    SaxHandler handler = new SaxHandler();

                    // and parse:
                    parser.parse(file.getAbsolutePath(), handler);    
                } 
                catch (Exception ex) 
                {
                    ex.printStackTrace(System.out);
                }
                System.out.println(docArray);
            }
            else if (file.isDirectory())
            {
                //It's a directory so (recursively) traverse it
                traverse(file);
            }
        }
    }
}

这里的重点主要是代码的前1/2,具体来说:

    private static Object[][] docArray = new String[6][];
    private static Object[][] subDocArray = (Object[][])docArray[3][0];
    private static Object[][] sub2DocArray = (Object[][])subDocArray[3][0];
    private static Object[][] sub3DocArray = (Object[][])sub2DocArray[3][0];

在运行时,我收到以下错误:

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at Test.<clinit>(Test.java:20)
Exception in thread "main" 

所以这是指向这一行:

private static Object[][] subDocArray = (Object[][])docArray[3][0];

我读到了错误,但我不完全确定这意味着什么。我认为这是某种类型的铸造错误。但是,我不知道如何修复它以使其工作。有任何想法吗?

2 个答案:

答案 0 :(得分:2)

Object[][] docArray = new String[6][]

将docArray声明为字符串数组的数组。

所以下一行没有意义:

Object[][] subDocArray = (Object[][])docArray[3][0];

表示“获取docArray的第三个字符串数组的第0个字符串,并尝试将此String转换为对象数组的数组。”

除了类型问题,您还有另一个问题。初始化对象数组时,它充满了空值。从此数组中获取值将始终返回null。并且尝试获取null的第0个元素会导致NullPointerException。

请注意,拥有对象数组的数组非常难以阅读和理解,并且通常是缺乏设计的标志。使用定义良好的类而不是Object数组。

答案 1 :(得分:1)

由于您的docArray定义,docArray[3]为“null”。这就是你得到例外的原因。