2011-07-06 134 views
0

因此,对于我的一个项目,我试图将数组存储在数组内等数组等。 而且我已经问过关于如何做到这一点的前一个问题,并得到解决方案,它没有返回编译器错误,但在运行时我收到一条简短的错误消息,程序刚刚结束。这里是我的代码:初始化多维数组问题

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
Object[][] docArray = new String[6][] 

将docArray声明为Strings数组的数组。

所以下一行是没有意义的:

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

手段“采取docArray的字符串的第三组的第0字符串,并试图将此字符串投进去对象数组的数组。 “

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

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

+0

我只是在数组中使用数组,因为这是我的一位导师在这里提出的设计规范。所以,当你说使用定义良好的类时,你的意思是为我要存储在数组中的每个元素创建一个单独的类?即如果docArray [0]是doc Title - >为doc Title创建类,docArray [1]是标题title - > class for title title等? –

+0

是的,这就是主意。 –

+0

KK,我会试试看。谢谢。 –

1

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