开发者

Problem Initializing Multi-dimensional Array

开发者 https://www.devze.com 2023-03-17 14:25 出处:网络
So for a project of mine, I am trying to store arrays within arrays within arrays, etc, etc. And I had asked a previous question on here as to how to do that and gotten a solution which returned no co

So for a project of mine, I am trying to store arrays within arrays within arrays, etc, etc. And I had asked a previous question on here as to how to do that and gotten a solution which returned no compiler errors, but at run-time I get a short error message and the program just ends. Here is my code:

Java Code

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);
            }
        }
    }
}

The focus here is primarily on the first 1/2 of the code, specifically:

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

At run-time, I get the following error:

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

So that is pointing to the line:

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

I read up on the error, but I am not entirely sure what that means. I assume it is some type of casting error. However, I have no idea how to fix that to make it work. Any ideas?


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

declares docArray as an array of arrays of Strings.

So the next line doesn't make sense:

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

means "take the 0th String of the third array of Strings of docArray, and try to cast this String into an array of arrays of Objects."

Aside of the type problem, you also have another problem. When an array of objects is initialized, it's full of null values. Getting a value from this array will thus always return null. And trying to get the 0th element of null leads to a NullPointerException.

Note that having arrays of arrays of Objects is pretty hard to read and understand, and is often the sign of a lack of design. Use well-defined classes rather than Object arrays.


Due to your docArray definition, docArray[3] is `null'. It is the reason that you get the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消