0

I have one query.i am creating 3 horizontal managers inside the vertical field manager. while compiling my code i am getting IllegalStatException.i am doing this.

VerticalFieldmanager vfm = new VerticalFieldManager();
  HorizontalFieldManager hfm1 = new HorizontalFieldManager();
  {somecode}
  HorizontalFieldManager hfm1 = new HorizontalFieldManager();
  {somecode}   
 HorizontalFieldManager hfm1 = new HorizontalFieldManager();
  {somecode}

Then i am adding hfm's to the vfm

 vfm.add(hfm1);
 vfm.add(hfm1);
 vfm.add(hfm1);
  add(vfm);    

i have done this but getting an exception.can anybody tell me solution for this..

favo
  • 5,426
  • 9
  • 42
  • 61
Sagar
  • 451
  • 1
  • 3
  • 13

3 Answers3

1

You cant add the same field/manager to a manager over and over again.

you can do something like that using a for or a while and creating a new object inside and adding it to the parent manager

Juanma Baiutti
  • 637
  • 1
  • 10
  • 27
0

The thing is you are creating the same object hfm1 again and again for 3 times You can either create 3 different objects like hfm1, hfm2 and hfm3 like

 VerticalFieldmanager vfm = new VerticalFieldManager();
    HorizontalFieldManager hfm1 = new HorizontalFieldManager();
    {somecode}
    HorizontalFieldManager hfm2 = new HorizontalFieldManager();
    {somecode}   
    HorizontalFieldManager hfm3 = new HorizontalFieldManager();
    vfm.add(hfm1);
    vfm.add(hfm2);
    vfm.add(hfm3);
     add(vfm)

or

VerticalFieldmanager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
hfm1 = new HorizontalFieldManager();
{somecode}   
hfm1 = new HorizontalFieldManager();
{somecode}

But in this only the last hfm1 is added since due to runtime polymorphism... the object of first hfm1 releases it memory when the second hfm1 is initialised and the third hfm1 releases the object memory of the second hfm1 added

So only the last hfm1 is added to vfm in this case ... Better follow the first approach.

Yatin
  • 2,969
  • 9
  • 34
  • 68
0

You're creating three HorizontalFieldManagers with the same name! Don't do that - try:

enter code here
VerticalFieldmanager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
hfm1 = new HorizontalFieldManager();
{somecode}   
hfm1 = new HorizontalFieldManager();
{somecode}

OR

enter code here
VerticalFieldmanager vfm = new VerticalFieldManager();
for(int i=0;i<3;i++)
{
 HorizontalFieldManager hfm1 = new HorizontalFieldManager();
 {somecode}
}
dragonfly
  • 411
  • 3
  • 11