简体   繁体   English

ArrayList返回null的方法

[英]Method with ArrayList returning null

I want this method to return a list of strings that contains all the filenames in a directory. 我希望此方法返回包含目录中所有文件名的字符串列表。 The List I have declared is returning as null from the method. 我声明的列表从该方法返回为null。 Does anyone know why? 有人知道为什么吗? Code snippet posted below. 下面发布了代码段。

   public static String docxList() {
        String path1 = "E:\\";
        String files = null;
        String display = null;
        String dpmt=null;

    //String variables declaration,
    try{
        Class.forName("oracle.jdbc.driver.OracleDriver");   
        java.sql.Connection 
        con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","user","pass");
        //Database connection Code
        PreparedStatement ps=con.prepareStatement("select * from EMP_DB where EMP_ID='EI-12'");
        ResultSet rs=ps.executeQuery();
        rs.next();
        dpmt=rs.getString("DEPARTMENT");

        path1 =path1+dpmt; //New path generation of specific directory according to user department

        File folder = new File(path1);
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                files = listOfFiles[i].getName();
                String filename=files;
                if(filename.indexOf(".")>0)
                {
                    filename=files.substring(0,files.lastIndexOf("."));
                }

                display=('<'+"A HREF"+'='+"\""+"PDFs/"+dpmt+"/"+files+"\""+" "+"target"+'='+"\""+"targetiframe"+"\""+'>'+'<'+"font"+" size"+'='+"\"4\""+" color"+'='+"\"white\""+'>'+filename+"</font>"+"<"+"/A"+'>');
                //Hyper-linked list of files. 
                ArrayList<String> al=new ArrayList<String>();
                al.add(display);
                //Adding all file names in a directory to a Arraylist.      
            }
        }
    }
    catch(Exception ex) 
    {
        System.out.print("Exception: "+ex+" in Dpmt Method.");  
        //Exception Display if any.
    }                                   

    return al; //ArrayList doesn't generate list of files in the drectory.
}

You declread your array list in a scope that can't be seen by your return statement. 您将数组列表扩展到return语句无法看到的范围内。 Place the following statement before your for loop. 将以下语句放在for循环之前。

ArrayList<String> al = new ArrayList<String>();

So, it will look like this- 因此,它看起来像这样-

File[] listOfFiles = folder.listFiles();
ArrayList<String> al = new ArrayList<String>();

for (int i = 0; i < listOfFiles.length; i++)

.. rest of your code .. ..其余的代码..

EDIT: 编辑:

You should also modify your method signature to return a list, not a String. 您还应该修改方法签名以返回列表,而不是字符串。

public static ArrayList<String> docxList()

You need to declare your List<String> outside the try/catch block so that it is in the scope of the return statement. 您需要在try/catch块之外声明List<String> ,以便它在return语句的范围内。 Also, your return type is currently String when you are trying to return a List<String> so this also needs changing if you do indeed want to return a List . 另外,当您尝试返回List<String>时,您的返回类型当前为String ,因此,如果您确实想返回List则也需要更改此类型。 Your method should look like: 您的方法应如下所示:

public static List<String> docxList() {
    String path1 = "E:\\";
    String files = null;
    String display = null;
    String dpmt=null;
    List<String> al = new ArrayList<String>();

    try { 
        // code... 
    }
    catch(Exception ex) {
        // handle exception..
    }                                   

    return al; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM