This discussion is archived
2 Replies Latest reply: Feb 7, 2013 2:30 AM by Raja_236 RSS

Exception while converting bean to XML persistace

Raja_236 Newbie
Currently Being Moderated
Hi,

am getting exception while executing the following code, please suggest that why am getting this exception.
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream; 

public class Main6 
{
     public static void main(String[] args) throws Exception
     { try
     
          
          {
     
      Item bean = new Item();
       bean.setId(new Long(1));
       bean.setItemName("a");
       bean.setItemColor("Red");
       bean.setItemQuantities(new Integer(100));
       XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("Bean1.xml")));
       encoder.writeObject(bean);
       encoder.close();
          }
          catch (Exception e)
     {
           System.out.println("Exception " + e);
     }
}
}
 class Item
{
     private Long id;
     private String itemName;
     private String itemColor;
     private Integer itemQuantities;

     public Long getId() {
     return id;
     }
     public void setId(Long id)
     {
     this.id = id;
     }

     public String getItemName() {
       return itemName;
     }
     public void setItemName(String itemName) {
         this.itemName = itemName;
     }

     public String getItemColor () 
     {
     return itemColor;
     }
     public void setItemColor(String itemColor)
     {
        this.itemColor = itemColor;
     }

     public Integer getItemQuantities() 
     {
          return itemQuantities;
     
     }
     public void setItemQuantities(Integer itemQuantities) {
       this.itemQuantities = itemQuantities;
      
     }
}
Result:
D:\Documents and Settings\ba80123524\Desktop>java Main6
java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class Item with mo
difiers ""
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Item);
Continuing ...
thanks in advance

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points