Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / October 2003

Tip: Looking for answers? Try searching our database.

What's a bean? XMLEncode question.

Thread view: 
ftoomch - 18 Oct 2003 17:23 GMT
Hi, I'm doing a uni student doing an assignment where I'm trying to save
objects in a more persistent place than memory, so i was having a look
at xml files. I came across XMLEncode and XMLDecode in java.beans
(anyone got a good concise description of what a bean is?) and they look
perfect for saving object representations to a file, but I'm having
trouble getting the buggers to work.

Firstly here is the runtime error message:

java.lang.IllegalAccessException: Class java.beans.Statement can not
access a member of class Cinema with modifiers ""
Continuing ...
java.lang.Exception: discarding statement LinkedList0.add(Cinema0);
Continuing ...

Secondly the offending Cinema class (just barebones for the moment):

import java.io.*;

public class Cinema implements Serializable
{
    public static int id;
   
    Cinema()
    {
        this.id = 0;
    }
}

Thirdly the calling code:

...
Cinema c = new Cinema();
LinkedList cl = new LinkedList();
XMLEncoder enc;
cl.add(c);
   
try
{
    enc = new XMLEncoder(new BufferedOutputStream(new
FileOutputStream(outFile)));
    enc.writeObject(cl);
                ...

Anyone got an idea what's wrong? I heard that objects have to be
serializable before you can do anything like this, but it didn't seem to
work.

Cheers in anticipation, Ben.
ftoomch - 18 Oct 2003 17:25 GMT
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_01" class="java.beans.XMLDecoder">
 <object class="java.util.LinkedList"/>
</java>

Seems the do the LinkedList ok, why won't it do my class???
David Postill - 18 Oct 2003 18:39 GMT
<snip />

| (anyone got a good concise description of what a bean is?) and they look

<http://java.sun.com/products/javabeans/FAQ.html>
<http://www.jguru.com/faq/JavaBeans>

<davidp />

Signature

David Postill

Chris Riesbeck - 21 Oct 2003 19:17 GMT
> Hi, I'm doing a uni student doing an assignment where I'm trying to save
> objects in a more persistent place than memory, so i was having a look
> at xml files. I came across XMLEncode and XMLDecode in java.beans
> (anyone got a good concise description of what a bean is?) and they look
> perfect for saving object representations to a file,

Not perfect, but appropriate for modestly sized amounts of data.

> but I'm having
> trouble getting the buggers to work.
[quoted text clipped - 3 lines]
> java.lang.IllegalAccessException: Class java.beans.Statement can not
> access a member of class Cinema with modifiers ""

You have to define public get/set methods for all content
you want to save. Instance variables, public or private are
not looked at.

> Secondly the offending Cinema class (just barebones for the moment):
>
> public class Cinema implements Serializable

OK

> {
>     public static int id;
>    
>     Cinema()

Good. You need the default constructor.

>     {
>         this.id = 0;
>     }

Define

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

Note capitalization. Then you should be fine.

If your bean has collections of objects, define
get/set methods that use arrays. E.g., if you have
a collection of doubles,

 public double[] getScores() { return --make array of scores -- }
 public void setScores(double[] scores) { -- store scores -- }

This is trivial if your internal collection is an array.
If it's a Collection, use things like toArray() and
Arrays.asList() to go back and forth.
John C. Bollinger - 21 Oct 2003 22:30 GMT
>>public class Cinema implements Serializable
>
[quoted text clipped - 17 lines]
>
> Note capitalization. Then you should be fine.

Also, chances are that you do not want the id variable to be static.
Static members are associated with the class, and shared by all
instances.  If the purpose is data persistence then you would normally
want each data object to have its own state instead of shared state.

John Bollinger
jobollin@indiana.edu
ftoomch - 23 Oct 2003 12:32 GMT
Thanks to everyone who took the time to comment. Problem solved.

Ben.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.