使用FileReader时获取nullpointer访问错误

时间:2016-12-27 13:27:53

标签: java

我正在尝试使用FileReader读取文件但是正在获取空指针访问权限。下面是代码。你能告诉我错误是什么吗?

public class C {
    private static final String file = "a.csv";

    public static void main(String[] args) {
        try (BufferedReader br2 = new BufferedReader(new FileReader(file))) {
            String line1;
            while ((line1 = br2.readLine()) != null);
            {
                System.out.println(line1);
                String[] rules = line1.split(",");
                String sevkey = rules[0];
                String sevval = rules[1];
                String[] val = sevval.split("\\|");
                String actsevval = val[1];
                HashMap<String, String> sevs = new HashMap<String, String>();
                for (int i = 0; i <= sevs.size(); i++) {
                    sevs.put(sevkey, actsevval);
                    if (actsevval != "none") {
                        System.out.println(actsevval);
                    }

                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这样的代码可以正常工作:

public class C {
    private static final String file="a.csv";

    public static void main(String[] args) {
        try (BufferedReader br2 = new BufferedReader(new FileReader(file))) {
            String line1;
            while ((line1 = br2.readLine()) != null) // Here was extra semicolon!
            {
                System.out.println(line1);
                String[] rules = line1.split(",");
                String sevkey = rules[0];
                String sevval = rules[1];
                String[] val = sevval.split("\\|");
                String actsevval= val[1];
                HashMap<String, String> sevs = new HashMap<String,String>(); 
                for (int i =0; i<=sevs.size();i++){
                    sevs.put(sevkey, actsevval);
                    if (actsevval!="none"){
                        System.out.println(actsevval);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}