0

I have object list with many values in object which comes from XML source:

iit.getHomeCurrency().getPrice().toString()

If I put System.out.println(iit.getHomeCurrency().getPrice().toString()); I get succesfully complete list of objects.

But, when I want to convert this object into CSV, using

CSVWriter csvWriter = new CSVWriter(new FileWriter("example.csv"));
csvWriter.writeNext(new String[]{iit.getHomeCurrency().getPrice().toString(),"/n"} 
csvWriter.close();

I got only one line in csv file :(

Code:

for(int i=0;i<contentElement.size();i++){
            JAXBElement<ListInvoiceType> invoiceTypeElement = (JAXBElement<ListInvoiceType>) contentElement.get(i);
            ListInvoiceType listInvoiceType = invoiceTypeElement.getValue();
            List<InvoiceType> invoiceTypeList = listInvoiceType.getInvoice();
            for(InvoiceType invoiceType:invoiceTypeList ){
                InvoiceHeaderType invoiceHeaderType = invoiceType.getInvoiceHead
er();
                InvoiceDetailType invoiceDetailType = invoiceType.getInvoiceDetail();                       

                InvoiceItemType iit = null;
                InvoiceAdvancePaymentItemType iapit = null;
                for(Serializable s:invoiceDetailType.getContent()){
                    if(s instanceof JAXBElement) {
                        JAXBElement<?> element = (JAXBElement)s;
                        if(element.getValue() instanceof InvoiceItemType){
                            iit = (InvoiceItemType)element.getValue();
                        }
                        else if(element.getValue() instanceof InvoiceAdvancePaymentItemType){
                            iapit = (InvoiceAdvancePaymentItemType)element.getValue();
                        }
                        else{
                            throw new Exception("xx");
                        }
                    }

                    else if(s instanceof String){

                    }
                    else{
                        throw new Exception("xx");
                    }       


                        if(iit!=null){          
                        CSVWriter csvWriter = new CSVWriter(new FileWriter("example.csv"));

                        csvWriter.writeNext(new String[]{iit.getHomeCurrency().getPrice().toString(), iit.getCentre().getIds()  ,invoiceHeaderType.getDate().toString(),invoiceHeaderType.getInvoiceType().value(),"\r\n"});

                        csvWriter.close();

What shall I change to make java write every object into new row of csv file?

Thank you

1 Answer 1

1

The main problem seems to be that you open a new file for each iteration of the loop, move

CSVWriter csvWriter = new CSVWriter(new FileWriter("example.csv"));

to before the for loop

CSVWriter csvWriter = new CSVWriter(new FileWriter("example.csv"));
for(Serializable s:invoiceDetailType.getContent()){
    if(s instanceof JAXBElement) {
        //... 
    }

   if( iit !=n ull){          
       csvWriter.writeNext(new String[]{iit.getHomeCurrency().getPrice().toString(), iit.getCentre().getIds()  ,invoiceHeaderType.getDate().toString(),invoiceHeaderType.getInvoiceType().value(),"\r\n"});
   }

and close the file after the for loop

} //end for loop

 csvWriter.close();
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Joakim, it helped. Now I have another simple problem, out of XML it may happen not all the fields are filled. I got null pointer exception in such case. So I try to fix the problem using : if(iit.getCentre().getIds()==null) {iit.getCentre().getIds()="data not filled";} if object is null, fill text "data not filled" in csv. But, by such code atempt, I got an error: the left-hand side of an assignment must be a variable. How could I fix null pointer exception please?
@bennes You get the error because getIds() is a method so you can't assign anything to it. Use a local variable instead in your if and either give it the value of getIds or the string you have and then use that variable in the writeNext call. Alternatively change getIds to return "data not filled" instead of null
Hi Joakim, I am sorry for my noob questions :( But when I try: String hu = iit.getCentre().getIds().toString(); if(hu==null) {hu= "data not filled" ;} I got NullPointer exception in first row (String hu = iit.getCentre().getIds().toString();) anyway :( Also other suggestions failed, I am doing wrong something :)
Why are you doing toString() when you know getIds might return null? First check with if if getIds() return null then act accordingly. if( iit.getCentre().getIds()==null) {hu = ...} else {...}
yes, also the problem was iit.getCentre(). could be null, now it`s fixed. Everything works correctly, thank you very much Joakim and have a good day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.