1

I tried a lot but all in vain. I am not able to to display image from servlet into jsp.

I have developed a servlet which in turn calls another class that returns BufferedImage.

In servlet I written:

PngEncoder png =new PngEncoder(image,false,0,9);
    response.getOutputStream().write(png.pngEncode());
       response.getOutputStream().close();

And in jsp I have written:

IMG src="/WebApplication5/ChartServlet"

but when i run my web application nothing gets displayed.
When i directly browse by this url http://localhost:8080/WebApplication5/ChartServlet it showing :

HTTP Status 500 - type Exception report message descriptionThe server encountered an internal error () that prevented it from fulfilling this request. exception java.lang.NullPointerException note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1 logs. Sun Java System Application Server 9.1

What am I doing wrong?

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
irfan rasool
  • 158
  • 1
  • 2
  • 12
  • it tells you where to look - in the logs – Bozho Jan 30 '12 at 13:18
  • Hey Bozho thanks for your quick reply. I am using netbeen and not so much used to it. So please could you let me know where i can found the logs. i have gone through one log of netbeen View->ID log but didnt found any thing helping. – irfan rasool Jan 30 '12 at 14:20

1 Answers1

0

Check if the png object is created OR not (if it is getting null). That could be the only reason why you should get a NullPointerException

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
  • Hi Ravi, i sorted out the HTTP Status 500 thanks for pointing towards that. Right now i checked whether the PNG object is getting created i BufferedImage image = providerchart.createBufferedImage(480, 360, null); PngEncoder png = new PngEncoder(image, false, 0, 9); System.out.println(" image :- "+png); – irfan rasool Jan 31 '12 at 05:45
  • Hi Ravi, Thanks for your guiding me in sorting out the HTTP Status 500. I followed your instruction and I checked whether the PNG object is getting created BufferedImage image = providerchart.createBufferedImage(480, 360, null); PngEncoder png = new PngEncoder(image, false, 0, 9); System.out.println(" image :- "+png); And in output I got this:- Image :- com.keypoint.PngEncoder@713caa Means it’s not null. Still I am getting only blank page with no chart image. Please guide me where i need to tweek. – irfan rasool Jan 31 '12 at 06:00
  • Before writing the image to the outputstream, set the content type as response.setContentType("image/png"); – Ravindra Gullapalli Jan 31 '12 at 06:05
  • response.setContentType("image/png"); commoditychart o_providerchart = new commoditychart(); JFreeChart providerchart= o_providerchart.produceAllCharts("1","2012-01-09","C:/Documents and Settings/irfanr/Desktop/WebApplication5/web/images/"); BufferedImage image = providerchart.createBufferedImage(480, 360, null); PngEncoder png = new PngEncoder(image, false, 0, 9); System.out.println(" image :- "+png); response.getOutputStream().write(png.pngEncode()); this is what i have written in servlet – irfan rasool Jan 31 '12 at 06:15
  • providerchart= o_providerchart.produceAllCharts("1","2012-01-09","C:/Documents and Settings/irfanr/Desktop/WebApplication5/web/images/"); produceAllCharts:- is the method of commoditychart class which produces charts and return to BufferedImage object. produceAllCharts parameters are (product id, date, path); from these parameters i fetch data from database. If i run only commoditychart class the chart get generated on the specified path. But when i try to return the chart object that was genertaed in the commoditychart class to the servelt then its showing nothing in the browser.Thanks – irfan rasool Jan 31 '12 at 06:23
  • Were you able to save the png image in the disk? Try to save that and see if it is getting saved. – Ravindra Gullapalli Jan 31 '12 at 06:24
  • 1
    I think you have to use PngEncoderB instead of PngEncoder. Have a look at this - http://catcode.com/pngencoder/. To convert BufferedImage, you have to use PngEncoderB. – Ravindra Gullapalli Jan 31 '12 at 06:33
  • If i write folowing lines in produceAllCharts() method of commoditychart class. File ifle2 = new File(imagepath + product_id + ".png"); imagepath:- is the location to my image folder of my web application product_id :- that i pass to rt now its value is 1. ChartUtilities.saveChartAsPNG(ifle2, chart, 650, 490, info); commoditychart class contains main() method. when i run this java file only the data gets fethced nicely and png image is created at the path given to it. – irfan rasool Jan 31 '12 at 06:35
  • I think this is a simple solution - http://stackoverflow.com/questions/5272966/jfreechart-image – Ravindra Gullapalli Jan 31 '12 at 06:37
  • Thanks a lot Ravi for your time. I will try with your given urls. Thanks And warm regards – irfan rasool Jan 31 '12 at 06:41
  • Thanks a lot Ravi. What i did, placed all the code of my class that was generating chart in the servlet and it worked like charm thanks a lot for your time – irfan rasool Jan 31 '12 at 09:02
  • Great to see that finally you got a solution. I thought the code was in servlet. – Ravindra Gullapalli Jan 31 '12 at 09:04