初始化多维数组时出现问题



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

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];

我读了这个错误,但我不完全确定这意味着什么。我认为这是某种类型的铸造错误。然而,我不知道如何解决这个问题。有什么想法吗?

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

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

所以下一行没有意义:

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

意思是"取docArray的第三个字符串数组的第0个字符串,并尝试将此字符串强制转换为对象数组。">

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

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

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

相关内容

  • 没有找到相关文章

最新更新