Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-03-2010, 02:43 PM
Member
 
Join Date: Jan 2010
Posts: 1
Rep Power: 0
Dominator is on a distinguished road
Default reading problem with XML
Hello people,
I have to do a project in Java that is associated with XML. I have never used XML before, so problems appeared.

I need to make imitation of the Forum (but as a client - server based application)
and I managed to do most of the job! I use XML to store topics and posts.

When i run the application and write something in the XML file it all works normally, but when I want to read the same post i have entered moment before, it appears an error and a null exception.

Strange thing is that the next time i run the application, the new post is able to read normally !
So, the problem occurs only when in the same application window some text is posted and tried to read!

I don't know if maybe the problem is somewhere near initialization of XML so i'll copy-paste part of the code, related to XML and a method that throws exception.


Code:
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.parse (new File("topics.xml"));
    Element qu = doc.getElementById("Teme");

     public void upisivanjeUXML() {
		try {
		Transformer TR = TransformerFactory.newInstance().newTransformer();
		TR.setOutputProperty(OutputKeys.INDENT, "yes"); 
		StreamResult result = new StreamResult(new StringWriter());
		DOMSource source = new DOMSource(doc);
		TR.transform(source, result);

		String xmlString = result.getWriter().toString();
		System.out.println(xmlString);
				 
        OutputStream OS;
        byte bafer[] = xmlString.getBytes();
        OS = new FileOutputStream("teme.xml");
        for(int i=0; i < bafer.length; i++) {
        	OS.write(bafer[i]);
        }
        
        OS.close();
        bafer = null;
		} catch (Exception e) {System.out.println(e.getMessage()); }
	}
And a problematic method:
Code:
if (command.startsWith("POSTS")) {
					
			
					int brojPostova = 0;
					Node trazenaTema = null;
					Node trenutniPost, trenutniAutor, trenutniTekst;
					
					String[] naslovTeme = komanda.split("~`!");
					NodeList spisakTema = doc.getElementsByTagName("tema");
					
					if(naslovTeme[1].contains(" "))
					naslovTeme[1] = prebacivanjeRazmaka(naslovTeme[1]); 
					
					for (int i = 0; i < spisakTema.getLength(); i++) {
						if (naslovTeme[1].equals(spisakTema.item(i).getFirstChild().getNextSibling().getTextContent())) {
							trazenaTema = spisakTema.item(i);
							break;
						}
					}
					
					trenutniPost = trazenaTema.getFirstChild().getNextSibling().getNextSibling().getNextSibling();
					brojPostova = trazenaTema.getChildNodes().getLength() - 1;
					
					
					String sviPostovi = "";
					for(int i = 0; i < brojPostova; i++) {
						if (trenutniPost == null) break;
						trenutniAutor = trenutniPost.getFirstChild().getNextSibling();
						trenutniTekst = trenutniAutor.getNextSibling().getNextSibling();  // THIS LINE RETURNS NULL POINTER EXCEPTION						
if (i == 0)	sviPostovi = trenutniTekst.getTextContent() + "`" + trenutniAutor.getTextContent();
						else sviPostovi = sviPostovi + "`" + trenutniTekst.getTextContent() + "`" + trenutniAutor.getTextContent();
						
						
						trenutniPost = trenutniPost.getNextSibling().getNextSibling();
					}
					
					
					DOS.writeBytes(sviPostovi + "\n");
				}
Please help!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reg: File Reading Problem balaji csc New To Java 0 11-06-2009 05:22 PM
A bit of a complex reading and writing problem jigglywiggly New To Java 1 10-28-2009 07:08 PM
Problem in reading xml gayathri_g New To Java 5 08-28-2009 10:10 AM
problem with reading excel sheet data reading using poi libraries sandeepsai17 New To Java 5 08-21-2009 12:03 PM
Image reading from URL problem paras Advanced Java 4 04-18-2009 08:26 AM


All times are GMT +2. The time now is 08:56 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org