user2964315
user2964315

Reputation:

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. 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.
}

Upvotes: 0

Views: 4654

Answers (2)

Saj
Saj

Reputation: 18712

You declread your array list in a scope that can't be seen by your return statement. Place the following statement before your for loop.

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()

Upvotes: 3

Choc13
Choc13

Reputation: 796

You need to declare your List<String> outside the try/catch block so that it is in the scope of the return statement. 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. 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; 
}

Upvotes: 0

Related Questions