#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Java XML: IndexOutOfBoundsException When appending a node


    Java XML: IndexOutOfBoundsException When appending a node

    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<Date, String> messagesMap = new TreeMap<Date, String>();
    	
    	// File should be saved as /username/buddy.xml
    	public static void writeHistory(String username, String buddy, Map<Date, String> 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<Date, String> entry : messages.entrySet()) {		
    				
    				// <message>
    				Element messageE = doc.createElement("message");
    				rootE.appendChild(messageE);
    				
    				// Set attribute to <message> element
    				// messageE.setAttribute("id", "1");
    				
    				// <from>
    				Element fromE = doc.createElement("from");
    				fromE.appendChild(doc.createTextNode(buddy));
    				messageE.appendChild(fromE);
    				
    				// <date>
    				Element dateE = doc.createElement("date");
    				s = SDF.format(entry.getKey());
    				n = doc.createTextNode(s);
    				dateE.appendChild(n);
    				messageE.appendChild(dateE);
    				
    				// <text>
    				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();
    		}
    	}
    Thank you so much for your help.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    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)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    I have removed
    Code:
    rootE.appendChild(messageE);
    and it works.

IMN logo majestic logo threadwatch logo seochat tools logo