Java XML: IndexOutOfBoundsException When appending a node - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Java XML: IndexOutOfBoundsException When appending a node

prierepriere Posts: 4Member
Dear all,

Now that I am writing a messenger program. The following code shall store the message history.
Sadly, I've encountered an "IndexOutOfBounds" exception on a line and I've been trying for a whole day,
but still can't figure out the problem.

[CODE] public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final String FILE_PATH = "/data/data/com.demo.xmppchat/";
public static Map messagesMap = new TreeMap();

// File should be saved as /username/buddy.xml
public static void writeHistory(String username, String buddy, Map messages)
{
try {

// Check if the file exists. If the file does not exist, create and initiate one.
File file = new File(FILE_PATH + username);
if (!file.exists()) {
file.mkdirs();
}
file = new File(FILE_PATH + username + "/" + buddy + ".xml");
// file.setReadable(true); file.setWritable(true); file.setExecutable(true);
if (!file.exists()) {
file.createNewFile();
initiateHistoryFile(username, buddy);
}

// Now that the XML file should exists as we have created and initialized one.

// TODO:
// Now we modify the XML file

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
Element root = doc.getDocumentElement();
Element rootE = doc.getDocumentElement();
String s = "";
Node n = null;

for (Map.Entry entry : messages.entrySet()) {

//
Element messageE = doc.createElement("message");
rootE.appendChild(messageE);

// Set attribute to element
// messageE.setAttribute("id", "1");

//
Element fromE = doc.createElement("from");
fromE.appendChild(doc.createTextNode(buddy));
messageE.appendChild(fromE);

//
Element dateE = doc.createElement("date");
s = SDF.format(entry.getKey());
n = doc.createTextNode(s);
dateE.appendChild(n);
messageE.appendChild(dateE);

//
Element textE = doc.createElement("text");
s = entry.getValue();
n = doc.createTextNode(s);
textE.appendChild(n);
messageE.appendChild(textE);

root.appendChild(messageE); // <- *** THIS IS WHERE "IndexOutOfBounds" exception OCCURS! ***
}

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);

// Write the file
// FileWriter fileWritter = new FileWriter(file.getName(),true);
// BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
// bufferWritter.write("");
// bufferWritter.close();

} catch (Exception e) {
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);
e.printStackTrace();
}
}[/CODE]

Thank you so much for your help.

Comments

  • prierepriere Posts: 4Member
    W/System.err(1067): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
    W/System.err(1067): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    W/System.err(1067): at java.util.ArrayList.add(ArrayList.java:143)
    W/System.err(1067): at org.apache.harmony.xml.dom.InnerNodeImpl.insertChildAt(InnerNodeImpl.java:126)
    W/System.err(1067): at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(InnerNodeImpl.java:52)
    W/System.err(1067): at com.demo.xmppchat.XMLReadWrite.writeHistory(XMLReadWrite.java:113)
Sign In or Register to comment.