Abid,

Hi,

I noted that you are adding the panel whether the data is null or not. I
would suggest you do this;

Data data = dataDao.getData(id)
if (data!= null){

     //construct and add the panel
     DataPanel panel = new DataPanel("panel", data);
     add(panel);
     //the panel has implementation of what to do with the data once it is
passed to it.
} else {
    //forget about the panel and only add the label that says there is no
data
     add(new Label("No data to display"));
}

regards
Josh


On Tue, Jul 27, 2010 at 11:48 AM, Josh Kamau <[email protected]> wrote:

> Abid,
>
> Hi,
>
> I noted that you are adding the panel whether the data is null or not. I
> would suggest you do this;
>
> if (data!= null){
>
>      //construct and add the panel
>
>
> }
>
> On Tue, Jul 27, 2010 at 11:28 AM, Abid K <[email protected]> wrote:
>
>> I hope someone can help a newbie who is learning Wicket.
>>
>> I have the following code which accepts a parameter and then does a
>> database
>> query to get the 'Data' object. If the user enters the wrong Id the
>> database
>> query will return null and in this case I want to notify the user the data
>> could not be found and any other component should be hidden.
>>
>> But, when I get an null object the code stops working and a null exception
>> is thrown, any ideas? Or is there a elegant way to do this?
>>
>> public class DataView extends WebPage {
>>
>>  private Data data;
>>
>>  public DataView(PageParameters parameters) {
>>    long dataId = parameters.getLong("dataId");
>>
>>    DataDao dataDao = new DataDao();
>>    data = dataDao.getData( dataId );
>>
>>    // display message that the data could not be found
>>    Label dataNotFound = new Label("dataNotFound", "Data could not be
>> found");
>>    dataNotFound.setVisible(data == null);
>>
>>    // otherwise display the panel containing the data
>>    SomePanel panel = new SomePanel("somePanel");
>>    panel.setVisible(data != null);
>>
>>    add(dataNotFound);
>>    add(panel);
>>  }
>>
>>  public class SomePanel extends Panel {
>>    public SomePanel(String id) {
>>      super(id);
>>      // this throws null exception when data is null
>>      Label label = new Label("someLabel", String.valueOf(data.getId()));
>>      add(label);
>>    }
>>  }
>> }
>>
>> Thanks
>>
>
>

Reply via email to