简体   繁体   中英

Variable cannot be resolved to a variable

Could you please help me with following code ? I've created a class with static method which should return variable with type ArrayList<ArrayList<String>> . But I got an error in the return statement in the end of the function run() . It says that data cannot be resolved to a variable ...

    package com.ita;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public  class ReadCSV {

    public static  ArrayList<ArrayList<String>> run() {

        String csvFile = "RSS_usernames.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            ArrayList<String> RSSname = new ArrayList<String>();
            ArrayList<String> username= new ArrayList<String>();

             ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
            int i=0;

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                    // use comma as separator
                String[] country = line.split(cvsSplitBy);

                System.out.println("Country [code= " + country[0] 
                                     + " , name=" + country[1] + "]");

                RSSname.add(new String(country[0]));
                username.add(new String(country[1].trim()));

                 data.add(new ArrayList<String>());
                 data.get(i).add(country[0]);
                 data.get(i).add(country[1].trim());

                i=i+1;
            }

            System.out.println(data.get(0).get(1));



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("Done");

        return data;
      }
}

data is declared inside the try block, so it's not in scope after it. Move its declaration to be before the try block.

    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    try {
        ArrayList<String> RSSname = new ArrayList<String>();
        ArrayList<String> username= new ArrayList<String>();
        ...
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");

    return data;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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