-1

I am trying to read a file using FileReader but am getting null pointer access. Below is the code. Could you please tell me what the error is.

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();
        }
    }
}
4
  • Please include the stacktrace Commented Dec 27, 2016 at 13:29
  • The stack trace tells you. Please read it. Commented Dec 27, 2016 at 13:30
  • 2
    while ((line1 = br2.readLine()) != null) ; delete the semicolon here Commented Dec 27, 2016 at 13:31
  • @DimaSan is correct, the error is that semicolon. Commented Dec 27, 2016 at 13:36

1 Answer 1

0

The code like this will work correctly:

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.