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!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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 = new File(FILE_PATH + username + "/" + buddy + ".xml");
// file.setReadable(true); file.setWritable(true); file.setExecutable(true);
if (!file.exists()) {
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");

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

Element fromE = doc.createElement("from");

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

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

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

Thank you so much for your help.


  • 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(
    W/System.err(1067): at java.util.ArrayList.add(
    W/System.err(1067): at org.apache.harmony.xml.dom.InnerNodeImpl.insertChildAt(
    W/System.err(1067): at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(
    W/System.err(1067): at com.demo.xmppchat.XMLReadWrite.writeHistory(
Sign In or Register to comment.