Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6865449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:03:03+00:00 2026-05-27T03:03:03+00:00

I am trying to implement a MessageBodyWriter for List. with this implementation i have

  • 0

I am trying to implement a MessageBodyWriter for List. with this implementation i have a null pointer exception on line ; marshaller.marshal(o, entityStream);

@Provider
@Produces(MediaType.APPLICATION_XML) 
public class MyListProvider implements MessageBodyWriter<List<Instrument>> {

    private String myWrapElemName = "datas";
    private Marshaller marshaller;


    public InstrumentModelListProvider(){

        JAXBContext context;
        try {
            context = JAXBContext.newInstance(Data.class);
             marshaller = context.createMarshaller();
             marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
             marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
             //marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.TRUE);
             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        } catch (JAXBException e) {
            System.err.println("/////////////"+e.getMessage());
            //e.printStackTrace();
        }

    }
    @Override
    public long getSize(List<Data> as, Class<?> type, Type genericType, Annotation[] annotations,
            MediaType mediaType) {

        return -1;
    }

    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
            MediaType mediaType) {



        return MediaType.APPLICATION_XML_TYPE.equals(mediaType) 
                && List.class.isAssignableFrom(type) 
                && (((ParameterizedType)genericType).getActualTypeArguments()[0]).equals(Data.class);
    }

    @Override
    public void writeTo(List<Data> list, Class<?> type, Type genericType, Annotation[] annotations,
            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException, WebApplicationException {

        Charset c = Charset.forName("UTF-8");
        String cName = c.name();

        entityStream.write(String.format("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>", cName).getBytes(cName));

        entityStream.write(String.format("<%s>", myWrapElemName).getBytes(cName));

        for (Data o : list){
          try {
              marshaller.marshal(o, entityStream);
          } catch(JAXBException exp) {
             exp.printStackTrace();
              //System.exit(-1);
          }
        }

        entityStream.write(String.format("</%s>", myWrapElemName).getBytes(cName));
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:03:03+00:00Added an answer on May 27, 2026 at 3:03 am
    public void writeTo(List<Data> list, Class<?> type, Type genericType, Annotation[] annotations,
            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException, WebApplicationException {
    
        Charset c = Charset.forName("UTF-8");
        String cName = c.name();
    
        entityStream.write(String.format("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>", cName).getBytes(cName));
    
        entityStream.write(String.format("<%s>", myWrapElemName).getBytes(cName));
        InstrumentModelListProvider();
        for (Data o : list){
          try {
              marshaller.marshal(o, entityStream);
          } catch(JAXBException exp) {
             exp.printStackTrace();
              //System.exit(-1);
          }
        }
    
        entityStream.write(String.format("</%s>", myWrapElemName).getBytes(cName));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement an autocomplete based on this It looks like it's very straight
Trying to implement a function that will return a list of ints the represent
Trying to implement NSCopying for the first time, and I have a question about
Trying to implement what I thought was a simple concept. I have a user
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
iam trying to implement an Searchmachine into my site. Iam using Zend_Search_Lucene for this.
Trying to implement a shell, mainly piping. I've written this test case which I
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
Im trying to implement this code: http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api and want to add it to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.