Gurus:
While executing the autoSizeColumn method of the XSSFSheet in my code (using
POI-3.7), I am getting a NPE. The stack trace is as below:
java.lang.NullPointerException
at
org.apache.poi.xssf.usermodel.XSSFRichTextString.getLengthOfFormattingRun(XSSFRichTextString.java:334)
at
org.apache.poi.xssf.usermodel.helpers.ColumnHelper.getColumnWidth(ColumnHelper.java:370)
at
org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:337)
at
org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:320)
at
com.xxx.yyyy.precalc.server.SpreadSheetMaker.formatDataSheet(SpreadSheetMaker.java:329)
at
com.xxx.yyyy.precalc.server.SpreadSheetMaker.createSpreadsheetForUpdate(SpreadSheetMaker.java:274)
at
com.xxx.yyyy.precalc.server.SpreadSheetMaker.getSpreadSheet(SpreadSheetMaker.java:75)
at
com.xxx.yyyy.InsertUpdateSpreadsheetServlet.doGet(InsertUpdateSpreadsheetServlet.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)
When I looked at the source code of XSSFRichTextString, i see this:
/**
* Returns the number of characters this format run covers.
*
* @param index the index of the formatting run
* @return the number of characters this format run covers
*/
public int getLengthOfFormattingRun(int index) {
if(st.sizeOfRArray() == 0) return length();
for(int i = 0; i < st.sizeOfRArray(); i++){
CTRElt r = st.getRArray(i);
if(i == index) return r.getT().length(); //<<<This is
where the NPE is thrown
}
return -1;
}
Looking at this code, I am assuming the the object "r"obtained in the line
above the offending code is null, thereby the NPE on the line below. Can we
add a null check on the if condition, like this:
if(r != null && i == index) return r.getT().length();
//<<<Add a null check for "r"
will this solution work? Not having looked at the deeper levels, i cannot be
certain, so will appreciate inputs.
Or, am I doing something wrong that needs to be fixed? If so, please let me
know.
Thanks for looking!
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/NullPointerException-in-org-apache-poi-xssf-usermodel-XSSFRichTextString-getLengthOfFormattingRun-tp3296746p3296746.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]