2011-07-08 240 views
1

我有一個非常基本的問題。我是Java WebServices的新手。我想使用web服務來獲取服務器上的文件名,所以我寫下了代碼;但我有一個例外。錯誤在哪裏?Java Web服務

package gungor.webWord;

import java.io.File; 
import javax.jws.WebService; 
import javax.jws.WebMethod; 
import javax.jws.WebParam; 

@WebService(serviceName = "webWordWebService") 
public class webWordWebService { 

    /** This is a sample web service operation */ 
    @WebMethod(operationName = "hello") 
    public String hello(@WebParam(name = "name") String txt) { 
     return "Hello " + txt + " !"; 
    } 

    /** 
    * Web service operation 
    */ 
    @WebMethod(operationName = "getFileNames") 
    public String[] getFileNames(@WebParam(name = "parameter") java.lang.String path) { 
     File dir=new File(path); 
     String files[]=dir.list(); 
     String fileNames[]={""}; 
     int j=0; 
     for (int i = 0; i < files.length; i++) { 
      if(!files[i].startsWith(".")){ 
       fileNames[j]=files[i]; 
       j++; 
      } 
     } 
     return fileNames; 
    } 
} 

異常

Service invocation threw an exception with message : null; Refer to the 
server log for more details 


Exceptions details : java.lang.reflect.InvocationTargetException 

javax.servlet.ServletException: java.lang.reflect.InvocationTargetException 
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:330) 
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(WebServiceTesterServlet.java:106) 
at org.glassfish.webservices.JAXWSServlet.doPost(JAXWSServlet.java:133) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:755) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) 
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:662) Caused by: 
java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:301) 
... 29 more Caused by: javax.xml.ws.soap.SOAPFaultException: 1 
at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:193) 
at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:134) 
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:123) 
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:93) 
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:144) 
at $Proxy229.getFileNames(Unknown Source) ... 34 more Caused by: 
java.lang.ArrayIndexOutOfBoundsException: 1 
at gungor.webWord.webWordWebService.getFileNames(webWordWebService.java:37) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.glassfish.webservices.InstanceResolverImpl$1.invoke(InstanceResolverImpl.java:143) 
at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:150) 
at com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:261) 
at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:100) 
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641) 
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600) 
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585) 
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482) 
at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:116) 
at org.glassfish.webservices.MonitoringPipe.process(MonitoringPipe.java:142) 
at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119) 
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641) 
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600) 
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585) 
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482) 
at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:116) 
at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.processRequest(CommonServerSecurityPipe.java:212) 
at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.process(CommonServerSecurityPipe.java:144) 
at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119) 
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641) 
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600) 
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585) 
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482) 
at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:314) 
at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:608) 
at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:259) 
at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:162) 
at org.glassfish.webservices.JAXWSServlet.doPost(JAXWSServlet.java:145) ... 27 more 

回答

2

異常的成因是:

Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at gungor.webWord.webWordWebService.getFileNames(webWordWebService.java:37) at 

很難說沒有行號,但是這是一個可能的原因:

fileNames[j]=files[i]; 

...因爲f ileNames []是一個用一個元素初始化的數組,因此fileNames [j](其中j == 1)將導致堆棧跟蹤中顯示的異常。

+0

感謝您的幫助球員。我確實改變了fileNames [j] = files [i]行,而不是解決問題。我確實定義了一個List併爲其添加了字符串元素。 –