1

我需要能够在我的 Web 服务方法中创建以下类的实例,但由于某种原因出现了错误。

问题:为什么我不能在我的 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>> ---
}
4

2 回答 2

3

你的内部类不是静态的,试试

public static class GetTheFileListClass { ....
于 2014-01-30T20:01:32.830 回答
3

范围规则仍然适用,即使 GetTheFileListClass 是 (a) 一个类并且 (b) 是公共的。因为它是在 ReturnListOfValidFileNames_SVC 内部声明的,也就是它的封闭类,所以对它的任何非静态引用都必须遵循范围规则。

所以你有两个选择(我正在使用 main 来模拟你的静态方法):

声明内部类静态:

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");
    }
  }

}

或者

在您的静态方法中,创建封闭类的实例并像这样在其上使用 new 运算符

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");
    }
  }

}

玩得开心!

于 2014-01-30T20:04:43.770 回答