What is the cause of my IndexOutOfBoundsException


What is the cause of my IndexOutOfBoundsException



I am trying to upload a file using primefaces and hibernate. My managed bean has the following function:


public void upload(){
if(file != null){
try {
FacesContext context = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
String dbPath = servletContext.getRealPath("/");
String webcut = dbPath.substring(0, dbPath.lastIndexOf("\"));
String buildcut = webcut.substring(0, webcut.lastIndexOf("\"));
String mainURLPath = buildcut.substring(0, buildcut.lastIndexOf("\"));
InputStream inputStream = file.getInputstream();
String path = mainURLPath + "\web\resources\images" + file.getFileName();
File destFile = new File(path);
if(!destFile.exists()){
FileUtils.copyInputStreamToFile(inputStream, destFile);
}
product.setProUrl(file.getFileName().toString());
} catch (IOException ex) {
Logger.getLogger(ProductMB.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.getMessage());
}

}
}



file is my org.primefaces.model.UploadedFile object and product is my object from a hibernate util named Product which is mapped directly to the database table named product as well.
The mapping function in my DAO class is as follows:


file


org.primefaces.model.UploadedFile


product


Product


product


public boolean addProduct(Product product){
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(product);
session.getTransaction().commit();
session.close();
return true;
} catch (HibernateException e) {
}
return false;
}



When I try to save the file, the following error is thrown:


Warning: #{productMB.addProduct()}: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
javax.faces.FacesException: #{productMB.addProduct()}: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:119)
at javax.faces.component.UICommand.broadcast(UICommand.java:330)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:870)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1418)
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:201)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:670)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.faces.el.EvaluationException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 35 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
at addHandler.ProductMB.upload(ProductMB.java:144)
at addHandler.ProductMB.addProduct(ProductMB.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.el.ELUtil.invokeMethod(ELUtil.java:304)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:535)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)



When I follow addHandler.ProductMB.addProduct(ProductMB.java:114)
it points me to the following line of code:


addHandler.ProductMB.addProduct(ProductMB.java:114)


String webcut = dbPath.substring(0, dbPath.lastIndexOf("\"));



Can someone please provide some insight into my problem.





Have you done any debugging into what the value of dbPath is when webcut is initialized? The error is basically saying that dbPath does not contain "\" (i.e. ), since lastIndexOf("\") is returning -1 (i.e. character not found).
– Jonny Henly
1 hour ago



dbPath


webcut


dbPath


"\"



lastIndexOf("\")


-1









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

What's the difference between DDS and SOME/IP?

How to make file upload 'Required' in Contact Form 7?