因此,對於我的一個項目,我試圖將數組存儲在數組內等數組等。 而且我已經問過關於如何做到這一點的前一個問題,並得到解決方案,它沒有返回編譯器錯誤,但在運行時我收到一條簡短的錯誤消息,程序剛剛結束。這裏是我的代碼:初始化多維數組問題
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];
我對錯誤進行了閱讀,但我不完全確定這意味着什麼。我認爲這是某種類型的轉換錯誤。但是,我不知道如何解決這個問題,使其工作。有任何想法嗎?
我只是在數組中使用數組,因爲這是我的一位導師在這裏提出的設計規範。所以,當你說使用定義良好的類時,你的意思是爲我要存儲在數組中的每個元素創建一個單獨的類?即如果docArray [0]是doc Title - >爲doc Title創建類,docArray [1]是標題title - > class for title title等? –
是的,這就是主意。 –
KK,我會試試看。謝謝。 –