You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by nise <dh...@yahoo.com> on 2008/07/14 06:57:08 UTC

BeanUtils Not working !!!

Hi,
I am using BeanUtils for copying the contents of one bean into another using
reflection.
The code is really simple and it compiles and runs fine but does not give
the desired output.
It basically *DOES NOT* copy the property 'name' from the source to the
destination bean

when i run the code .. it just prints out

john
null

instead of

john
john

which i was expecting


wht cd i be doing wrong

thanks

nise


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.LogFactory;

class A{

String name;

public String getName()
{

return name;

}

public void setName(String name)
{
this.name= name;
}


}

class B
{

String name;

public String getName()
{

return name;

}

public void setName(String name)
{
this.name= name;
}


}




public class test {

public static void main(String args[])
{

A a1= new A();
B b1= new B();

a1.setName("John");
System.out.println(a1.getName());
try{
BeanUtils.copyProperties(b1,a1);
System.out.println(b1.getName());
}
catch(Exception e){}

}




} 
-- 
View this message in context: http://www.nabble.com/BeanUtils-Not-working-%21%21%21-tp18437474p18437474.html
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: BeanUtils Not working !!!

Posted by James Carman <ja...@carmanconsulting.com>.
The BeanUtils logic needs to be able to see the classes.  It has
nothing to do with your code.

On Mon, Jul 14, 2008 at 12:05 PM, nise <dh...@yahoo.com> wrote:
>
> Thanks .... just making the bean classes public got them to work
>
> Though i wonder why it would be so.
> Maybe because the reflection classes are in a different package and
> therefore to access my bean classes in the default package, they needed that
> the method access modifiers be public and not default.
>
> --
> View this message in context: http://www.nabble.com/BeanUtils-Not-working-%21%21%21-tp18437474p18447096.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: BeanUtils Not working !!!

Posted by nise <dh...@yahoo.com>.
Thanks .... just making the bean classes public got them to work 

Though i wonder why it would be so.
Maybe because the reflection classes are in a different package and
therefore to access my bean classes in the default package, they needed that
the method access modifiers be public and not default.

-- 
View this message in context: http://www.nabble.com/BeanUtils-Not-working-%21%21%21-tp18437474p18447096.html
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: BeanUtils Not working !!!

Posted by James Carman <ja...@carmanconsulting.com>.
Try making your bean classes public?

On Mon, Jul 14, 2008 at 12:57 AM, nise <dh...@yahoo.com> wrote:
>
> Hi,
> I am using BeanUtils for copying the contents of one bean into another using
> reflection.
> The code is really simple and it compiles and runs fine but does not give
> the desired output.
> It basically *DOES NOT* copy the property 'name' from the source to the
> destination bean
>
> when i run the code .. it just prints out
>
> john
> null
>
> instead of
>
> john
> john
>
> which i was expecting
>
>
> wht cd i be doing wrong
>
> thanks
>
> nise
>
>
> import org.apache.commons.beanutils.BeanUtils;
> import org.apache.commons.beanutils.PropertyUtils;
> import org.apache.commons.logging.LogFactory;
>
> class A{
>
> String name;
>
> public String getName()
> {
>
> return name;
>
> }
>
> public void setName(String name)
> {
> this.name= name;
> }
>
>
> }
>
> class B
> {
>
> String name;
>
> public String getName()
> {
>
> return name;
>
> }
>
> public void setName(String name)
> {
> this.name= name;
> }
>
>
> }
>
>
>
>
> public class test {
>
> public static void main(String args[])
> {
>
> A a1= new A();
> B b1= new B();
>
> a1.setName("John");
> System.out.println(a1.getName());
> try{
> BeanUtils.copyProperties(b1,a1);
> System.out.println(b1.getName());
> }
> catch(Exception e){}
>
> }
>
>
>
>
> }
> --
> View this message in context: http://www.nabble.com/BeanUtils-Not-working-%21%21%21-tp18437474p18437474.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: BeanUtils Not working !!!

Posted by Marko Umek <um...@gmail.com>.
Try to exchange b1 and a1:

BeanUtils.copyProperties(a1,b1);

May be there is a documentation bug ;-)



nise schrieb:
> Hi,
> I am using BeanUtils for copying the contents of one bean into another using
> reflection.
> The code is really simple and it compiles and runs fine but does not give
> the desired output.
> It basically *DOES NOT* copy the property 'name' from the source to the
> destination bean
>
> when i run the code .. it just prints out
>
> john
> null
>
> instead of
>
> john
> john
>
> which i was expecting
>
>
> wht cd i be doing wrong
>
> thanks
>
> nise
>
>
> import org.apache.commons.beanutils.BeanUtils;
> import org.apache.commons.beanutils.PropertyUtils;
> import org.apache.commons.logging.LogFactory;
>
> class A{
>
> String name;
>
> public String getName()
> {
>
> return name;
>
> }
>
> public void setName(String name)
> {
> this.name= name;
> }
>
>
> }
>
> class B
> {
>
> String name;
>
> public String getName()
> {
>
> return name;
>
> }
>
> public void setName(String name)
> {
> this.name= name;
> }
>
>
> }
>
>
>
>
> public class test {
>
> public static void main(String args[])
> {
>
> A a1= new A();
> B b1= new B();
>
> a1.setName("John");
> System.out.println(a1.getName());
> try{
> BeanUtils.copyProperties(b1,a1);
> System.out.println(b1.getName());
> }
> catch(Exception e){}
>
> }
>
>
>
>
> } 
>   

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org