0

I have used the code in Read all files in a folder. But when I try to implement the code, it gives me an error - java.lang.NoSuchMethodException: java.io.File.() Stack Trace:

java.lang.NoSuchMethodException: java.io.File.<init>()

at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
at 
org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:104)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1720)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

I have searched for long.. Most of answers were about setting up a default constructor. But in my case, the file doesnt have a default constructor. I may be looking at this completely wrong.. So here's hoping some one can help me out.. Thanks in advance :)

Code:

@RequestMapping(value="/retrieve", method=RequestMethod.POST)
    public ArrayList<String> listFiles() {
        ArrayList<String> listOfFiles =new ArrayList<String>() ;

    File folder = new File("E:/uploadedFiles/");
    File[] files = folder.listFiles();

    for (File file : files) {
        if (file.isFile()) {
            System.out.println(file.getName());
            listOfFiles.add(file.getName());
        }
    }
    return listOfFiles;
}
Community
  • 1
  • 1
User-8017771
  • 1,512
  • 1
  • 11
  • 17
  • 1
    It appears you are using a library which is trying to create a `File` using a default constructor. I would check how you are using the library and what assumptions it is making. – Peter Lawrey May 16 '17 at 06:36
  • Show your code. – Jay Smith May 16 '17 at 06:37
  • Have added the code @JaySmith – User-8017771 May 16 '17 at 06:43
  • 1
    The method runs without any problem as a standalone Java application, so it could be something with your servlet setup, or Spring usage. – Galcoholic May 16 '17 at 06:53
  • `BeanUtils` cannot seem to instantiate a `File` by reflection. So I'd guess that it has nothing to do with the code you presented, and rather suspect you've declared a bean of type `File` that Spring cannot instantiate. – Jeremy Grand May 16 '17 at 07:17
  • does your controller have a different method that has a `File` or an objecte containing a `File` as a parameter? Please show the whole code of your controller class. – P.J.Meisch May 16 '17 at 07:56
  • Im not sure what I did differently.. I refreshed the project, cleaned it.. And it worked like a charm.. :) Thanks for the responses. :) – User-8017771 May 16 '17 at 12:32

0 Answers0