Results 1 to 7 of 7
  1. #1
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Question XMLEncoder e XMLDecoder

    Hi!

    I have this problem...

    java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class Person with modifiers ""
    Continuing ...
    java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Person);
    Continuing ...

    My code

    Person class

    public class Person implements Serializable
    {

    private String name = null;
    private String addr = null;

    Person(){}

    Person(String n, String a)
    {
    name = n;
    addr = a;
    }

    public void setName(String n)
    {
    name = n;
    }

    public String getName()
    {
    return name;
    }

    public void setAddr(String a)
    {
    addr = a;
    }

    public String getAddr()
    {
    return addr;
    }


    public String toString()
    {
    return("Name: "+getName()+"\n "+"Address: "+getAddr()+");
    }
    }

    Main class

    try{

    FileOutputStream os = new FileOutputStream("person.xml");
    XMLEncoder encoder = new XMLEncoder(os);
    Person d = new Person();
    d.setName("Sara");
    d.setAddr("London");
    encoder.writeObject(d);
    encoder.close();

    }
    catch (FileNotFoundException e)
    {
    e.printStackTrace();
    }


    Can you help me please! :)

    Thanks

    Paula

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: XMLEncoder e XMLDecoder

    Your Person constructor has default access privileges...that is it is not marked as private/protected or public. This means it is only accessible by classes declared in the same package as it (which looks like the default package). The XMLEncoder seems to need access to these, but it can't access them because it isn't part of that package.

  3. #3
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: XMLEncoder e XMLDecoder

    Thanks for your help!
    That was easy it makes sense! :D

  4. #4
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: XMLEncoder e XMLDecoder

    Just one more thing...I promisse I don´t bother you anymore! :D

    The code above was just a test. I want to write an arrayList...so I did this...

    try{
    Person d = new Person();
    FileOutputStream os = new FileOutputStream("person.xml");
    XMLEncoder encoder = new XMLEncoder(os);
    for (int x=0; x<(Datastore.getSingletonObject().listPerson.getAr rayListSize()); x++)
    {
    d.setName(Datastore.getSingletonObject().listPerso n.getPerson(x).getName());
    d.setAddress(Datastore.getSingletonObject().listPe rson.getPerson(x).getAddress());
    System.out.println(d);
    encoder.writeObject(d);
    }
    encoder.close();

    The println its ok! Prints the arrayList, but the XML file shows just the first person in the arrayList...

    <?xml version="1.0" encoding="UTF-8"?>
    <java version="1.6.0_26" class="java.beans.XMLDecoder">
    <object id="Person0" class="Person">
    <void property="address">
    <string>London</string>
    </void>
    <void property="name">
    <string>Anna</string>
    </void>
    </object>
    <object idref="Person0"/>
    <object idref="Person0"/>
    <object idref="Person0"/>
    <object idref="Person0"/>
    </java>

    What I'm doing wrong? Must be obvious...I know... :/

    Thanks again! :)

    Paula

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: XMLEncoder e XMLDecoder

    We've seen something like this here before (I think).
    It's because you;re using the same Person object, and haven't flushed.
    The XMLEncoder knows it's already written d so simply sticks a reference to it (the idref ones).
    You should either be creating new Person objects for each entry you want to add, or call flush after each write.
    I would argue (quite a lot in fact) that you should do the former, since these are different Persons, and not simply the same one who likes to change their name a lot.
    So:
    Java Code:
    loop
        encoder.writeObject(new Person(...parameters...));
    (and I largely did that to show you code tags).

    ETA: Ooooooh, code blocks have changed (or have I hit some odd button or other)?

  6. #6
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: XMLEncoder e XMLDecoder

    flush worked! :D
    Thank you very very much Tolls!

    see ya

    Paula

  7. #7
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: XMLEncoder e XMLDecoder

    Here's another way...working with the arrayList :D

    XMLEncoder

    try{
    FileOutputStream os = new FileOutputStream("person.xml");
    XMLEncoder encoder = new XMLEncoder(os);
    encoder.writeObject(Datastore.getSingletonObject() .listPerson);
    encoder.close();
    }catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    XMLDecoder

    try{
    FileInputStream os = new FileInputStream("person.xml");
    XMLDecoder decoder = new XMLDecoder(os);
    Datastore.getSingletonObject().listPerson=(ListPer son)decoder.readObject();
    decoder.close();
    }
    catch(FileNotFoundException e) {
    e.printStackTrace();
    }

    PS1 - The class ListPerson must have a a constructor with no arguments and getters and setters...
    PS2 - The calsses ListPerson and Person must implements Serializable

Similar Threads

  1. Replies: 1
    Last Post: 01-10-2009, 02:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •