Java XML file error

1

I have the following program:

package exercici3;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
 *
 * @author Montse
 */
@XmlRootElement
@XmlType(propOrder ={"autor", "nom", "editorial","isbn", "llibre"})


public class Llibreria extends MarshalClass{

    static String JAXB_FORMATTED_OUTPUT;
    private String autor;
    private String nom;
    private String editorial;
    private int isbn;
    private ArrayList<LlistaLlibre> llibre=new ArrayList<LlistaLlibre>();

    @XmlElement
    public String getAutor(){return autor;}
    public void setAutor (String autor){this.autor =autor;}

    @XmlElement
    public String getNom(){return nom;}
    public void setNom (String nom){this.nom =nom;}

    @XmlElement
    public String getEditorial(){return editorial;}
    public void setEditorial (String editorial){this.editorial =editorial;}

    @XmlElement
    public int getIsbn(){return isbn;}
    public void setIsbn (int isbn){this.isbn =isbn;}

    @XmlElement
    public ArrayList <LlistaLlibre> getLlibre() { return llibre;}
    public void setLlibre(ArrayList<LlistaLlibre> llibre){ this.llibre= llibre;}

}





package exercici3;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Montse
 */
@XmlRootElement(name="llibr")

class LlistaLlibre extends MarshalClass {

    private String autor;
    private String nom;
    private String editorial;
    private int isbn;

    @XmlElement
    public String getAutor(){ return autor;}
    public void setAutor (String autor){ this.autor = autor;}

    @XmlElement
    public String getNom(){ return nom;}
    public void setNom (String nom){ this.nom = nom;}

    @XmlElement
    public String getEditorial(){ return editorial;}
    public void setEditorial (String editorial){ this.editorial = editorial;}

    @XmlElement
    public int getIsbn(){ return isbn;}
    public void setIsbn (int isbn){ this.isbn = isbn;}


    }






package exercici3;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Montse
 */
public class MarshalClass {

    public void generateXML (String nameFile) {

        try{
            File file = new File (nameFile);
            JAXBContext jc = JAXBContext.newInstance(this.getClass());
            Marshaller jaxbMarshaller = jc.createMarshaller();

            jaxbMarshaller.setProperty(Llibreria.JAXB_FORMATTED_OUTPUT , true);

            jaxbMarshaller.marshal(this, new FileWriter(nameFile, true));

        }catch (JAXBException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}







package exercici3;

import java.util.ArrayList;

/**
 *
 * @author Montse
 */
public class LlibreriaEscriure {

    public static void main(String[]args){

        EscriureLlibreria();

    }

    private static void EscriureLlibreria() {

        Llibreria cc = new Llibreria();
        cc.setAutor("Xavier");
        cc.setNom("Nosotros dos");
        cc.setEditorial("Columna");
        cc.setIsbn(978-84-664-2228-4);

        ArrayList<LlistaLlibre> alCU = new ArrayList<LlistaLlibre>();
        int init = 2000;
                for (int i =1; i<1000;i++){
        LlistaLlibre cu = new LlistaLlibre();
        cu.setAutor(""+i);
        cu.setNom(""+i);
        cu.setEditorial(""+i);
        cu.setIsbn(i);
        alCU.add(cu);       
    }
                cc.setLlibre(alCU);
                cc.generateXML("llibres.xml");


    }

}

-give me the following error:

    run:
Exception in thread "main" java.lang.IllegalArgumentException: name parameter must not be null
    at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:328)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
    at exercici3.MarshalClass.generateXML(MarshalClass.java:28)
    at exercici3.LlibreriaEscriure.EscriureLlibreria(LlibreriaEscriure.java:37)
    at exercici3.LlibreriaEscriure.main(LlibreriaEscriure.java:14)
C:\Users\Montse\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

It has to do with EscriureLlibreria ();

but I do not know what it can be? What is missing some argument?

    
asked by Montse Mkd 28.09.2017 в 09:06
source

1 answer

2

The problem is in this line of your class MarshalClass :

jaxbMarshaller.setProperty (Llibreria.JAXB_FORMATTED_OUTPUT, true);

You should write it like this:

jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT , true);

Then, in your class Llibreria this does not have any use:

static String JAXB_FORMATTED_OUTPUT;

The constant JAXB_FORMATTED_OUTPUT belongs to the interface Marshaller , which you already have: import javax.xml.bind.Marshaller;

    
answered by 28.09.2017 / 10:00
source