Hi Daniele, just an idea: @Override protected byte[] getImageData() { String basePath = "upload/images/"; Object name = logoModel.getObject(); File file = new File(".", basePath + name); LoggerFactory.getLogger(getClass()).info("serving file: " + file); try { return IOUtils.toByteArray(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
DefaultButtonImageResource img = new DefaultButtonImageResource("Image not found"); InputStream defautImg = img.getResourceStream().getInputStream(); byte[] bytes = new byte[defautImg.available()]; defautImg.read(bytes); return bytes; } You can show an image with this message while not found an solution for the null pointer exception. On Wed, Sep 2, 2009 at 6:36 PM, Daniele Dellafiore <ilde...@gmail.com>wrote: > Hi all, I use a DynamicWebResource to display images previously uploaded by > user ion the context of my application. I have this DWR subclass: > > class LogoResource extends DynamicImageResource { > > private final IModel logoModel; > > public LogoResource(IModel model) { > this.logoModel = model; > } > > @Override > protected byte[] getImageData() { > String basePath = "upload/images/"; > Object name = logoModel.getObject(); > File file = new File(".", basePath + name); > LoggerFactory.getLogger(getClass()).info("serving file: " + file); > try { > return IOUtils.toByteArray(new FileInputStream(file)); > } catch (FileNotFoundException e) { > e.printStackTrace(); > } catch (IOException e) { > e.printStackTrace(); > } > return null; > } > } > > now, when the file is found, everything is ok. > When it is not, I receive a NullPo9nterException, this is the output when > you can see my log and then the begin of the trace: > > INFO - LogoResource - serving file: ./upload/images/null > ERROR - RequestCycle - > java.lang.NullPointerException > at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:89) > at > > org.apache.wicket.markup.html.DynamicWebResource$1.getInputStream(DynamicWebResource.java:221) > > at > > org.apache.wicket.request.target.resource.ResourceStreamRequestTarget.respond(ResourceStreamRequestTarget.java:160) > > at > > org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:105) > > at org.apache.wicket.RequestCycle.respond(RequestCycle.java:1267) > > This is annoying and, more important, it seems to cause the page not to > reload properly. In fact when after a submit I call a setResponse on the > same page, the page is not reloaded. > > At the end of all the exceptions, I get: > > WARN - DataRequestCycle - Completed exception handling for > request [requestcy...@1ec3ffd thread=10193...@qtp-28693170-2] > INFO - DataRequestCycle - Request ended > [requestcy...@1ec3ffdthread=10193106@qtp-28693170-2](time: > 33 > > And the page has the same URL as before, correclty, but not the new > rendering: If I reload the page, I get the new stuff. > > In the same page I have other forms, with the same setResponse onSubmit, > that work perfectly. > Do you think that these exceptions could be the problem for the undone > refresh? Or maybe is better to look elsewhere? > > And, in any case, there is a clean way to avoid the getImageData to be > called if I do not have a file for the image? > > Thanks. > > -- > Daniele Dellafiore > http://blog.ildella.net > http://twitter.com/ildella >