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.


Java 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:
Java 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();  [B][COLOR="Red"]// THIS LINE RETURNS NULL POINTER EXCEPTION[/COLOR][/B]						
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!