2014-01-30 102 views
1

我需要能夠在我的Web服務方法中創建以下類的實例,並且出於某種原因存在錯誤。爲什麼我不能在Java WebMethods Services中創建本地Java類的實例?

問題:爲什麼我無法在我的Java WEBServices中聲明和實例化我的類?

**GetTheFileListClass FindArrayListOfFiles = new GetTheFileListClass(fileName);** 

錯誤:

The source was saved, but was not compiled due to the following errors: 
C:\SoftwareAG\IntegrationServer\packages\DssAccessBackup\code\source\DssAccessBackup\services\flow.java:48: non-static variable this cannot be referenced from a static context 

     GetTheFileListClass FindArrayListOfFiles = new GetTheFileListClass(fileName); 
    1 error 

代碼:

public final class ReturnListOfValidFileNames_SVC 

{ 

    /** 
    * The primary method for the Java service 
    * 
    * @param pipeline 
    *   The IData pipeline 
    * @throws ServiceException 
    */ 
    public static final void ReturnListOfValidFileNames(IData pipeline) 
      throws ServiceException { 
     IDataCursor pipelineCursor = pipeline.getCursor(); 
     String fileName = IDataUtil.getString(pipelineCursor,"FileName"); 
     ArrayList<String> listOfFileName = new ArrayList<String>(); 

     //This will get the file list and set it to the local parameter for the Service 

     **GetTheFileListClass FindArrayListOfFiles = new GetTheFileListClass(fileName);** 

     listOfFileName = FindArrayListOfFiles.getMyFileList(); 

     IDataUtil.put(pipelineCursor,"ListOfFileNames",listOfFileName.toArray()); 
     pipelineCursor.destroy(); 
    } 

    // --- <<IS-BEGIN-SHARED-SOURCE-AREA>> --- 

    public class GetTheFileListClass { 
     String fileName = new String(); 
     ArrayList<String> MyFileList = new ArrayList<String>(); 
     String InputFile = new String(); 

     GetTheFileListClass(String workFile){ 
      setInputFile(workFile); 
     } 

     public void setMyFileList(ArrayList<String> myList, String newFileValueToAdd) { 
      myList.add(newFileValueToAdd); 
     } 

     public ArrayList<String> getMyFileList() { 
      return MyFileList; 
     } 

     public void setInputFile(String wFile) { 
      fileName = wFile; 
     } 

     public String getInputFile(){ 
      return fileName; 
     } 

     private String returnFileName(String a) { 
      String matchEqualSign = "="; 
      String returnFile = new String(); 
      int index = 0; 

      index = a.indexOf(matchEqualSign,index); 
      index++; 

      while (a.charAt(index) != ' ' && a.charAt(index) != -1) { 
       returnFile += a.charAt(index); 
       //System.out.println(returnFile); 
       index++; 
      } 

      return returnFile; 
     } 

     private void locatedFileName(String s, String FoundFile, ArrayList<String> myFileListParm) { 
      final String REGEX = ("(?i)\\./\\s+ADD\\s+NAME\\s*="); 
      Pattern validStringPattern = Pattern.compile(REGEX); 
      Matcher validRegMatch = validStringPattern.matcher(s); 
      boolean wasValidRegMatched = validRegMatch.find(); 

      if (wasValidRegMatched) { 
       FoundFile = returnFileName(s); //OUTPUT variable should go here 
       setMyFileList(myFileListParm,FoundFile); 
      } 
     } 

     //This is the methods that needs to be called from the main method 
     private void testReadTextFile() throws IOException { 
       BufferedReader reader = new BufferedReader(new FileReader(fileName)); 
       String FileLine = null; 
       while ((FileLine = reader.readLine()) != null) { 
        locatedFileName(FileLine,fileName,MyFileList); //test to see if the "./ Add name=" is found in any of the strings 
       } 
     } 

     private void printArrayFileList(ArrayList<String> myList) { 
      for (String myIndexFileListVariable : myList) { 
       System.out.println("File Name: " + myIndexFileListVariable); 
      } 
     } 
    } 

    // --- <<IS-END-SHARED-SOURCE-AREA>> --- 
} 

回答

3

你的內部類也不是一成不變的,嘗試

public static class GetTheFileListClass { .... 
+0

謝謝。這工作。所以所有這些類都必須是公共靜態的。我注意到,WEBMethods不允許您像在Java中那樣將類放在主類之外。這是我第一次使用WEBMethods,因此這是一次學習體驗。有關於這個問題的好書嗎? –

3

規則的範圍仍然適用,即使GetTheFileListClass是(a)一個班,是(b)公開的。因爲它是在ReturnListOfValidFileNames_SVC中聲明的,所以它是它的封閉類,所以對它的任何非靜態引用都必須遵循範圍規則。

所以,你有兩個選擇(我使用主要以模擬你的靜態方法):

聲明內部類的靜態:

public final class Outer { 

    public static void main(String[] args) { 
    Inner inner = new Inner(); 
    inner.doIt(); 
    } 

    public static class Inner { 
    public void doIt() { 
     System.out.println("Do it"); 
    } 
    } 

} 

OR

在您的靜態方法,創建封閉類的一個實例並像這樣使用新操作符

public final class Outer { 

    public static void main(String[] args) { 
    Outer outer = new Outer();// Now we have an enclosing instance! 
    Inner inner = outer.new Inner(); 
    inner.doIt(); 
    } 

    public class Inner { 
    public void doIt() { 
     System.out.println("Do it"); 
    } 
    } 

} 

玩得開心!

相關問題