Hello, just starting to learn how to work with XML via Java and I have encountered an error.

I have an XML file with car owners and their cars.
XML file:
Java Code:
<?xml version="1.0" encoding="utf-8"?>
    <person id="1"> 		 <!-- id is required, unique (positive) integer 
				      (no two cars with same ID) -->
        <name>Pavel</name>	 <!-- name is required, non-empty string -->
        <surname>Novák</surname> <!-- surname is required, non-empty string -->
        <salary>12000.00</salary>   <!-- salary is required, positive number 
	                    	      with 2 decimal places precision, e.g. 10000.50 -->
        <note>Poznámka</note>    <!-- note optional element, repeatable, contains any string -->
        <cars>			 <!-- cars optional element, contains any number of child-element car -->
            <car id="ZNC 33-93"> <!-- id is required, format like this: LLL NN-NN where L is a letter, N a ciphers 0..9
	                              two or three letters, space, two ciphers, comma and 2 ciphers) -->
                <type>Škoda 120 GLX</type> <!-- type is required, non-empty string -->
                <fuel>gasoline</fuel>      <!-- fuel is required, enumeration with values:
						gasoline/diesel/lpg/electricity -->
                <year>1985</year>	   <!-- year is required, 4-ciphers positive integer number -->
                <country>cz</country>	   <!-- country is required, 2 chars string -->
                <price>15000</price>        <!-- price is required, positive integer -->

    <person id="2" age="21">

    <person id="3">

            <car id="6B6 82-51">
                <type>Škoda Octavia Combi</type>
            <car id="ZNH 28-51">
                <type>Škoda Octavia Combi</type>
            <car id="ZNE 11-75">
                <type>Škoda Favorit</type>
            <car id="ZN 85-51">
                <type>Prototyp č. 1 vyrobený v dílně Franty Vomáčky</type>


And I want to write a method, that changes the owner of the car, i.e. moves the car element to another person.
Here is what I got so fat, it seems to remove the element, but does not create the new one>
Java Code:
public class Main {

     * W3C object model representation of a XML document.
     * Note: We use the interface(!) not its implementation
    private Document doc;

     * Task 1, part A: Complete this method.
     * Sells the car identified by carId to the person identified by personId.
     * (i.e. moves the respective car element into the cars element 
     * of the recipient person, new owner.
     * Note that if the recipient does not have any cars yet,
     * the containing cars element must first be created before
     * the cars is moved into it.
     * if either the car or the recipient is not present, the method 
     * does not change nor report anything.
     * @param carId id of the car to be sold
     * @param personId person receiving the car
     * @throws IllegalArgumentException if any parameter is null or empty
    public void sellCarTo(String carId, String personId) {
        Element car = null;
        Element person;
        Element pomCar = null;
        NodeList carList = doc.getElementsByTagName("car");
        for(int i = 0; i < carList.getLength(); i++) {
            car = (Element) carList.item(i);            
            String pomCarId = car.getAttribute("id"); 
            if(pomCarId.equals(carId)) {
                String myCarId = pomCarId;
                pomCar = car;
        NodeList peopleList = doc.getElementsByTagName("person");        
        for(int i = 0; i < peopleList.getLength(); i++) {
            person = (Element) peopleList.item(i);
            String pomPersonId = person.getAttribute("id");
            if(pomPersonId.equals(personId)) {
                Element newCar = null;
                NodeList cars = person.getElementsByTagName("cars");
                if(cars.getLength() == 0) {
                    newCar = doc.createElement("cars");
                } else {
                    newCar = (Element) cars.item(carList.getLength() + 1);
And I can´t really find the problem. If anyone can point me in the right direction, I would be really grateful.