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 の内部で宣言されているため、それはそのエンクロージング クラスであるため、それへの非静的参照はスコープの規則に従う必要があります。

したがって、2 つのオプションがあります (静的メソッドをシミュレートするために 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 に答える