aspose file tools*
The moose likes I/O and Streams and the fly likes How to read from InputStream into poi HSSFWorkbook Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to read from InputStream into poi HSSFWorkbook" Watch "How to read from InputStream into poi HSSFWorkbook" New topic
Author

How to read from InputStream into poi HSSFWorkbook

Rishi Kesh
Greenhorn

Joined: Jul 31, 2011
Posts: 3
Hi Ranchers,

I am stuck in a situation from last two days, and now JavaRanch Forum is my hope.

I have a situation where user will upload excel file to the server , and on the server we will read data from excel file and write to the database.
I am using Websphere Portal server , RAD and JSF.

now my problem is when i upload the file i get an inputStream of UploadFile class, and i tried to provide that inputStream
to HSSFWorkbook class ,but it cause null pointer exception, i am not sure if i am doing it correctly, so if you can suggest
right way to do that, that will be great help, My requirement is User will upload Excel file and we need to read data from excel file,

Thanks in Advance for any help

Sample Code which i am trying:
try {
//uploadedFile.getBytes();
InputStream inStream=uploadedFile.getInputStream();
System.out.println(inStream.toString());
InputStream fstream=new FileInputStream(uploadedFile.getName());

HSSFWorkbook workbook=new HSSFWorkbook(fstream);
// Get first sheet from the workbook

HSSFSheet sheet = workbook.getSheetAt(0);
int row=sheet.getFirstRowNum();
int row1=sheet.getLastRowNum();
Row row2=sheet.getRow(sheet.getFirstRowNum());

for(int i=sheet.getFirstRowNum();i<sheet.getLastRowNum();i++)
{
Row row3=sheet.getRow(i);
if(row3==null)
continue;
System.out.println();
for(int j=row2.getFirstCellNum();j<row2.getLastCellNum();j++)
{
if(row3.getCell(j)==null)
continue;
String cell=row3.getCell(j).toString();

System.out.print("\t"+cell);



}

}
Exception Stack Trace:

[9/15/13 14:38:52:356 IST] 00000038 SystemOut O File size: 13824 bytes
[9/15/13 14:38:52:356 IST] 00000038 SystemOut O Reading from file started:
[9/15/13 14:38:52:356 IST] 00000038 SystemOut O java.io.ByteArrayInputStream@9ff09ff
[9/15/13 14:38:52:359 IST] 00000038 application E java.lang.NullPointerException
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:108)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:387)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:458)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:763)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1655)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1595)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:131)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:374)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:932)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:500)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1550)
Caused by: java.lang.NullPointerException
at mypackage.MyBean.submit(MyBean.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.apache.el.parser.AstValue.invoke(AstValue.java:131)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:67)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:94)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41461
    
  51
Not knowing which is line 87 of the MyBean class, you will have to tell us which object is null.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read from InputStream into poi HSSFWorkbook