Re: When object is null
I've come across similar scenarios fairly often - i.e. where a the construction of component is impossible if its model object is null, or some other construction parameter is null. I'm still not sure what the best approach is in this situation. One of the methods I've used is to late-bind the "optional" component in the onBeforeRender() of a special wrapper container (e.g. if the model object != null, add the component, make the wrapper visible. If model object == null, don't add the component and make the wrapper non-visible). This seems like a blatant hack, however. see these threads: http://apache-wicket.1842946.n4.nabble.com/Nullable-resource-link-td1886391.html#a1886395 http://apache-wicket.1842946.n4.nabble.com/Components-and-nullable-data-td1888023.html#none Abid K wrote: Thanks Josh and Daniel - both methods have worked. I have gone with checking if the object is null or not like so... public class SomePanel extends Panel { public SomePanel(String id) { super(id); if ( data == null ) { return; } Label label = new Label("someLabel", String.valueOf(data.getId())); add(label); } } I have decided to do this because I am not sure how I can format a date without doing this... SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/" ); Label dateLabel = new Label( "date", dateFormat.format( data.getDate( ) ) ); Thanks. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: When object is null
Thanks Josh and Daniel - both methods have worked. I have gone with checking if the object is null or not like so... public class SomePanel extends Panel { public SomePanel(String id) { super(id); if ( data == null ) { return; } Label label = new Label("someLabel", String.valueOf(data.getId())); add(label); } } I have decided to do this because I am not sure how I can format a date without doing this... SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/" ); Label dateLabel = new Label( "date", dateFormat.format( data.getDate( ) ) ); Thanks.
Re: When object is null
You could try using a PropertyModel instead like this: Label label = new Label("someLabel", new PropertyModel(DataView.this,"data.id"); PropertyModels take care of possible null pointers. Kind regards, Daniel www.joyn-it.at On 27.07.2010 10:28, Abid K 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 - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: When object is null
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 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 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 >> > >
Re: When object is null
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 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 >
When object is null
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