-2

I'm rather new to java so apologise if this is a stupid question.

I have the following function

public static List<String[]> read(String document) throws IOException{
        try{
            CSVReader reader = new CSVReader(new FileReader(document));
            List<String[]> data = reader.readAll();
            reader.close();
        } catch(IOException e){
            e.printStackTrace();
        }
        return data;
    }

however i am getting an error that data cannot be resolved to a variable. However if i but the return in the try statement the error goes away and states that the function should return. As the variable is inside the function i would have thought that regardless of the catch it would of allowed this. Can anyone explain to me where I am going wrong?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Liam Sorsby
  • 2,912
  • 3
  • 28
  • 51

5 Answers5

4

This is fairly straightforward. The problem is that "data" only exists in the scope of the "try" block. Outside of that, it is undefined. A simple solution might look like this:

public static List<String[]> read(String document) throws IOException{

        List<String[]> data = null;
        try{
            CSVReader reader = new CSVReader(new FileReader(document));
            data = reader.readAll();
            reader.close();
        } catch(IOException e){
            e.printStackTrace();
        }
        return data;
    }
David S.
  • 6,567
  • 1
  • 25
  • 45
  • Thank you for your answer. I have voted for the post to be closed but i will mark the answer as correct if i am able to before the post is closed. I was unaware that a try block was classed as a scope but thank you for your help. – Liam Sorsby Jan 17 '14 at 19:12
  • The `= null` is actually very important in the case of a failure! – BrainStone Jan 17 '14 at 19:13
1

you need to declare the data variable in the same block

public static List<String[]> read(String document) throws IOException{
    List<String[]> data;
    try{
        CSVReader reader = new CSVReader(new FileReader(document));
        data = reader.readAll();
        reader.close();
    } catch(IOException e){
        e.printStackTrace();
    }
    return data;
}
kmera
  • 1,725
  • 10
  • 22
1
public static List<String[]> read(String document) throws IOException{
    List<String[]> data = null; //Declare your variable here
    try{
        CSVReader reader = new CSVReader(new FileReader(document));
        data = reader.readAll(); //Initialize your variable here
        reader.close();
    } catch(IOException e){
        e.printStackTrace();
    }
    return data;
}

Declare your variable outside of your try block. When you do this, it will be accessible outside of that try block, for example, where your return statement is.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
1

The very simple answer to that would be:

public static List<String[]> read(String document) throws IOException{
    List<String[]> data = null;

    try{
        CSVReader reader = new CSVReader(new FileReader(document));
        data = reader.readAll();
        reader.close();
    } catch(IOException e){
        e.printStackTrace();
    }

    return data;
}

This is because data was declared inside the try catch block or as it is also called scope (I'll stick to block). Everything declared inside a block can only be accesed inside this block or in blocks that are inside aswell.

Another solution would be the following. It avoids declaring (and initialzing) the data variable if not neccessary:

public static List<String[]> read(String document) throws IOException{
    try{
        CSVReader reader = new CSVReader(new FileReader(document));
        List<String[]> data = reader.readAll();
        reader.close();

        // Return early. Note this only happens when everything went right.
        // (Which is what we hope for)
        return data;
    } catch(IOException e){
        e.printStackTrace();
    }

    // This will only happen when it caught a exception!
    return null;
}

However I would stick to the first solution!

BrainStone
  • 3,028
  • 6
  • 32
  • 59
0

The scope of data is in between the braces of the try clause so you can't access it outside of that scope. You need to define data outside of the try to return it outside of the try.

Thom
  • 14,013
  • 25
  • 105
  • 185