You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by smallufo <sm...@gmail.com> on 2008/05/01 23:28:09 UTC

PropertyModel with default null model object ?

Sorry , this simple question bothers me a while , I don't know how to solve
it:

I have a MyObject model object , which is initially null.
MyPanel recevies the MyObject and prints it value field by field.


In the WebPage , I add MyPanel like this :

myPanel = new MyPanel("myPanel" , new PropertyModel(this , "myobj"));
myPanel.setEscapeModelStrings(false);
myPanel.setVisible(false);
myPanel.setOutputMarkupPlaceholderTag(true);
add(myPanel);

The problem is , because myobj is initially null ,
it will be later instantiated (and myPanel will be later switch to visible).
And in the construction time , MyPanel will throw a lot of NPE ...

How to solve it ? Thanks a lot .

Re: PropertyModel with default null model object ?

Posted by smallufo <sm...@gmail.com>.
A very good lesson learned.
Thanks to Per and Martijn very much..

2008/5/3 Martijn Dashorst <ma...@gmail.com>:

> You can nest models and use a model as the value for a PropertyModel.
> A PropertyModel knows how to cope with null values as it will return a
> null. Components and the label component in particular will render an
> empty string when the model value was found to be null.
>
> So nest or chain your model inside PropertyModels:
>
> add(new Label("xxx", new PropertyModel(model, "fieldX")));
> add(new Label("yyy", new PropertyModel(model, "fieldY")));
>
> Read the model documentation on the wiki [1]. Try to understand it and
> commit it to your brain. Print the page, and put it under your pillow
> at night. Read it while on the toilet, pin it to the side of your
> monitor, stick it to your rear view mirror, print it on transparent
> foil and stick it to the inside of your glasses.
>
> Martijn
>
> [1]
> http://cwiki.apache.org/WICKET/working-with-wicket-models.html#WorkingwithWicketmodels-Chainingmodels
>
> On 5/2/08, smallufo <sm...@gmail.com> wrote:
> > Yes .
> >
> > In the WebPage , I add MyPanel like this :
> >
> >  myPanel = new MyPanel("myPanel" , new PropertyModel(this , "myobj"));
> >
> > myPanel.setVisible(false);
> >  myPanel.setOutputMarkupPlaceholderTag(true);
> >  add(myPanel);
> >
> >
> > And in the MyPanel :
> >
> >  public class MyPanel extends Panel
> >  {
> >   private MyObj myObj;
> >
> >   public MyPanel(String id , IModel model)
> >   {
> >     super(id);
> >     this.myObj = (MyObj) model.getObject();
> >
> >     add(new Label("xxx" , myObj.getFieldX.toString()));
> >     add(new Label("yyy" , myObj.getFieldY.toString()));
> >
> >   }
> >  }
> >
> >  Because myObj passed to MyPanel is initially null ,
> >  In the MyPanel construction time , myObj.getFieldX , myObj.getFieldY
> will
> >  throw NPEs here...
> >
> >  I don't know how to solve it .....
> >
> >
> >
> >  2008/5/2 Per Newgro <pe...@gmx.ch>:
> >
> >
> >  > The code you provided should work. The NPEs comes from within the
> Panel?
> >  > So
> >  > can you give us an example how you access the model in the panel
> (with an
> >  > NPE
> >  > throwing component)?
> >  >
> >  > Cheers
> >  > Per
> >  >
> >  > ---------------------------------------------------------------------
> >  > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> >  > For additional commands, e-mail: users-help@wicket.apache.org
> >  >
> >  >
> >
>
>
> --
> Buy Wicket in Action: http://manning.com/dashorst
> Apache Wicket 1.3.3 is released
> Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.3
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: PropertyModel with default null model object ?

Posted by Martijn Dashorst <ma...@gmail.com>.
You can nest models and use a model as the value for a PropertyModel.
A PropertyModel knows how to cope with null values as it will return a
null. Components and the label component in particular will render an
empty string when the model value was found to be null.

So nest or chain your model inside PropertyModels:

add(new Label("xxx", new PropertyModel(model, "fieldX")));
add(new Label("yyy", new PropertyModel(model, "fieldY")));

Read the model documentation on the wiki [1]. Try to understand it and
commit it to your brain. Print the page, and put it under your pillow
at night. Read it while on the toilet, pin it to the side of your
monitor, stick it to your rear view mirror, print it on transparent
foil and stick it to the inside of your glasses.

Martijn

[1] http://cwiki.apache.org/WICKET/working-with-wicket-models.html#WorkingwithWicketmodels-Chainingmodels

On 5/2/08, smallufo <sm...@gmail.com> wrote:
> Yes .
>
> In the WebPage , I add MyPanel like this :
>
>  myPanel = new MyPanel("myPanel" , new PropertyModel(this , "myobj"));
>
> myPanel.setVisible(false);
>  myPanel.setOutputMarkupPlaceholderTag(true);
>  add(myPanel);
>
>
> And in the MyPanel :
>
>  public class MyPanel extends Panel
>  {
>   private MyObj myObj;
>
>   public MyPanel(String id , IModel model)
>   {
>     super(id);
>     this.myObj = (MyObj) model.getObject();
>
>     add(new Label("xxx" , myObj.getFieldX.toString()));
>     add(new Label("yyy" , myObj.getFieldY.toString()));
>
>   }
>  }
>
>  Because myObj passed to MyPanel is initially null ,
>  In the MyPanel construction time , myObj.getFieldX , myObj.getFieldY will
>  throw NPEs here...
>
>  I don't know how to solve it .....
>
>
>
>  2008/5/2 Per Newgro <pe...@gmx.ch>:
>
>
>  > The code you provided should work. The NPEs comes from within the Panel?
>  > So
>  > can you give us an example how you access the model in the panel (with an
>  > NPE
>  > throwing component)?
>  >
>  > Cheers
>  > Per
>  >
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>  > For additional commands, e-mail: users-help@wicket.apache.org
>  >
>  >
>


-- 
Buy Wicket in Action: http://manning.com/dashorst
Apache Wicket 1.3.3 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.3

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: PropertyModel with default null model object ?

Posted by smallufo <sm...@gmail.com>.
Thank you , I found a way to solve this :

myLink.add(new Image("hexagramImage" , new PropertyModel(model , "index")
    {
      @Override
      public Object getObject()
      {
        int index = ((Integer)super.getObject()).intValue();
        return new ResourceReference(MyObject.class , "icons/byIndex/"+
index+".gif");
      }
    }));

It seems working now...


2008/5/4 Per Newgro <pe...@gmx.ch>:

> Hello smallufo:
>
> the onclick and the onbeforerender methods will be called if page is
> presented
> (or immidiatly before). But this
> >   myLink.add(new Image("hexagramImage" , new
> > ResourceReference(MyObject.class , "icons/byIndex/"+ new
> > PropertyModel(model,"index").getObject().toString()+".gif")));  //FAILED
> will be called at custruction time of page. So there could be two possible
> causes for your problem.
> 1st: the bean which is assigned to panel model is null or
> 2nd: the index in that bean is null.
> 1st solution: Assign a bean - if not possible make getMyObjectFromInts
> null-aware and return a "default-image"
> 2nd solution: Assign a standard value for index (int = 0)
>
> I hope i got the issue - if not please ask again :-)
> HTH
> Per
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: PropertyModel with default null model object ?

Posted by Per Newgro <pe...@gmx.ch>.
Hello smallufo:

the onclick and the onbeforerender methods will be called if page is presented 
(or immidiatly before). But this
>   myLink.add(new Image("hexagramImage" , new
> ResourceReference(MyObject.class , "icons/byIndex/"+ new
> PropertyModel(model,"index").getObject().toString()+".gif")));  //FAILED
will be called at custruction time of page. So there could be two possible 
causes for your problem.
1st: the bean which is assigned to panel model is null or 
2nd: the index in that bean is null.
1st solution: Assign a bean - if not possible make getMyObjectFromInts 
null-aware and return a "default-image"
2nd solution: Assign a standard value for index (int = 0)

I hope i got the issue - if not please ask again :-)
HTH
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: PropertyModel with default null model object ?

Posted by smallufo <sm...@gmail.com>.
Thank you , I solve most of questions , except this :

In the MyPage extends WebPage :

//fieldX returns an Integer[] array
MyPanel myPanel = new MyPanel("myPanel" , new PropertyModel(model ,
"fieldX")
{
  @Override
  public Object getObject()
  {
    Integer[] ints = (Integer[]) super.getObject();
    return getMyObjectFromInts(ints);
  }
});
add(myPanel);

............................................

And in the MyPanel :

public MyPanel(String id, final IModel model)
{
  super(id , model);

  Link myLink = new Link("myLink")
  {
    @Override
    public void onClick()
    {
      Map<String , Object> parameterMap = new HashMap<String , Object>();
      parameterMap.put("index" ,  new PropertyModel(model ,
"index").getObject().toString()); // OK
      parameterMap.put("locale" , getSession().getLocale());
      PageParameters pps = new PageParameters(parameterMap);
      setResponsePage(HexagramPage.class , pps);
    }

    @Override
    protected void onBeforeRender()
    {
      super.onBeforeRender();
      add(new SimpleAttributeModifier("title" , new PropertyModel(model ,
"name").getObject().toString())); // , OK
    }
  };

  //draw an Icon depending on index value
  myLink.add(new Image("hexagramImage" , new
ResourceReference(MyObject.class , "icons/byIndex/"+ new
PropertyModel(model,"index").getObject().toString()+".gif")));  //FAILED

Here ! it goes problem!
It will throw a NPE :
java.lang.NullPointerException
  at ....
  at MyPage$2.getObject(MyPage.java:119)  <-------- the
getMyObjectFromInts() line
  at
org.apache.wicket.model.AbstractPropertyModel.getTarget(AbstractPropertyModel.java:187)
  at
org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:110)
  at MyPanel.<init>(MyPanel.java:49) <-------- the ResourceReference line


As MyPage shows , I use getMyObjectFromInts() to transform fieldX's return
value to my custom object....
And in MyPanel , I can successfully get myObject's index , name values , but
it fails at the last line!
It constructs a ResourceReference object by initializing a String , which
takes myObject's "index" value.
I use the same "new PropertyModel(model,"index").getObject().toString() " ,
successes first but fails second , why ?
How to solve this ?



2008/5/3 Per Newgro <pe...@gmx.ch>:

> Hello smallufo:
>
> > public class MyPanel extends Panel
> > {
> >   private MyObj myObj;
> >
> >   public MyPanel(String id , IModel model)
> >   {
> >     super(id);
> >     this.myObj = (MyObj) model.getObject();
> >
> >     add(new Label("xxx" , myObj.getFieldX.toString()));
> >     add(new Label("yyy" , myObj.getFieldY.toString()));
> >
> >   }
> > }
>
> This is the problem. Don't store the instance in the panel. Use the
> provided
> model. The Labels can get their data by a PropertyModel related to that
> model.
>  public class MyPanel extends Panel
>  {
>   // removed while not required private MyObj myObj;
>
>   public MyPanel(String id , IModel model)
>   {
>      super(id, model); <-- use this constructor
>     // removed while not required this.myObj = (MyObj) model.getObject();
>
>     // instead add(new Label("xxx" , myObj.getFieldX.toString()));
>     add(new Label("xxx" , new PropertyModel(model, "fieldX")));
>     // instead add(new Label("yyy" , myObj.getFieldY.toString()));
>     add(new Label("yyy" , new PropertyModel(model, "fieldY")));
>   }
>  }
>
> You simply wire the models together. So a model related to view can't be
> null
> (instanciated in panel self). That's what i mean if i always say "path to
> data". It's a description which properties have to be used to get the
> data.
> So the underlying business object can be null. The behavior if a null will
> be
> return will be determined by the component. A label for instance is
> displaying simply a blank. Textfield to.
>
> HTH
> Per
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: PropertyModel with default null model object ?

Posted by Per Newgro <pe...@gmx.ch>.
Hello smallufo:

> public class MyPanel extends Panel
> {
>   private MyObj myObj;
>
>   public MyPanel(String id , IModel model)
>   {
>     super(id);
>     this.myObj = (MyObj) model.getObject();
>
>     add(new Label("xxx" , myObj.getFieldX.toString()));
>     add(new Label("yyy" , myObj.getFieldY.toString()));
>
>   }
> }

This is the problem. Don't store the instance in the panel. Use the provided 
model. The Labels can get their data by a PropertyModel related to that 
model.
 public class MyPanel extends Panel
 {
  // removed while not required private MyObj myObj;

   public MyPanel(String id , IModel model)
   {
     super(id, model); <-- use this constructor
     // removed while not required this.myObj = (MyObj) model.getObject();

     // instead add(new Label("xxx" , myObj.getFieldX.toString()));
     add(new Label("xxx" , new PropertyModel(model, "fieldX")));
     // instead add(new Label("yyy" , myObj.getFieldY.toString()));
     add(new Label("yyy" , new PropertyModel(model, "fieldY")));
   }
 }

You simply wire the models together. So a model related to view can't be null 
(instanciated in panel self). That's what i mean if i always say "path to 
data". It's a description which properties have to be used to get the data. 
So the underlying business object can be null. The behavior if a null will be 
return will be determined by the component. A label for instance is 
displaying simply a blank. Textfield to.

HTH
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: PropertyModel with default null model object ?

Posted by smallufo <sm...@gmail.com>.
Yes .
In the WebPage , I add MyPanel like this :

myPanel = new MyPanel("myPanel" , new PropertyModel(this , "myobj"));
myPanel.setVisible(false);
myPanel.setOutputMarkupPlaceholderTag(true);
add(myPanel);

And in the MyPanel :

public class MyPanel extends Panel
{
  private MyObj myObj;

  public MyPanel(String id , IModel model)
  {
    super(id);
    this.myObj = (MyObj) model.getObject();

    add(new Label("xxx" , myObj.getFieldX.toString()));
    add(new Label("yyy" , myObj.getFieldY.toString()));

  }
}

Because myObj passed to MyPanel is initially null ,
In the MyPanel construction time , myObj.getFieldX , myObj.getFieldY will
throw NPEs here...

I don't know how to solve it .....



2008/5/2 Per Newgro <pe...@gmx.ch>:

> The code you provided should work. The NPEs comes from within the Panel?
> So
> can you give us an example how you access the model in the panel (with an
> NPE
> throwing component)?
>
> Cheers
> Per
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: PropertyModel with default null model object ?

Posted by Per Newgro <pe...@gmx.ch>.
The code you provided should work. The NPEs comes from within the Panel? So 
can you give us an example how you access the model in the panel (with an NPE 
throwing component)?

Cheers
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org