-1

I trying to read a file, but I always get a NullPointerException. Somebody know what may be the problem? I get NullPointerException at java.io.Reader, java.io.BufferedReader, at elte.hu.eltecom.file.FileManager.readGraph and at elte.hu.eltecom.Main.main

Main:

public static void main(String[] args) {
 //   if (args.length == 0) return;

    FileManager fm = new FileManager();
    Manager manager;
    manager = fm.readGraph("szia");
    User user = manager.getUserById(3);

    if  (user instanceof AdminUser){
        ((AdminUser) user).kickUser(manager.getUserById(1));
        ((AdminUser) user).kickUser(manager.getUserById(2));

        User grabowski = new User("Grabowski", Language.HUN);

        manager.createUser(grabowski);

        manager.linkUsers(grabowski,user);

    }
    fm.writeGraph(manager,args[0]);
}

File Manager:

static public Manager readGraph(String s) {

    FileReader fr = null;
    try {
        fr = new FileReader(s + "/input.txt");
    } catch (FileNotFoundException e) {

    }
    BufferedReader br = null;
    br = new BufferedReader(fr);
    String sCurrentLine = null;

    try {
        sCurrentLine = br.readLine();
    } catch (IOException e) {

    }

    int x = Integer.parseInt(sCurrentLine);

    Graph graph = null;
    graph = initGraph(x);   
    Manager manager = null;
    manager = new Manager(graph);

    int temp = 0;
    boolean lineLeft = true;

    try {
        sCurrentLine = br.readLine();
    } catch (IOException e) {
    }

    User users[] = new User[x];

    while(lineLeft){
        if (temp < x){
            String line[] =  sCurrentLine.split(" ", 2);
            Language language = Language.valueOf(line[0]);
            User user;
            String name = line[1];
            if (line[1].contains("#")){
                name = name.substring(1);
                user= manager.createAdminUser(name, language);
            }
            else{
                user = manager.createUser(name, language);
            }
            users[temp] = user;
        }
        else {
            String line[] = sCurrentLine.split(" ");
            for (int i = 0; i < line.length ; i++){
                if (Boolean.valueOf(line[i])){
                    graph.linkNodes(users[temp-x], users[i]);
                }
            }
        }

        try {
        if ( (sCurrentLine = br.readLine()) == null)
            lineLeft = false;

        } catch (IOException e) {

        }
        temp++;
    }

    return manager;
}
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
robotorg
  • 7
  • 2

1 Answers1

0

Look here:

try {
        fr = new FileReader(s + "/input.txt");
    } catch (FileNotFoundException e) {

    }

You just wrote: "Read file 'whatever', if the file is not found ignore it. At this point fr is null and you pass it as input to BufferedReader without checking if it is null or not.

Either write a nullcheck before instantiating the BufferedReader or do something else if the file is not found.

Ignoring exceptions in that way is very dangerous and you code will not be reliable.

Another suggestion, String s has very poor readability, try to give a better name (path or appDir or whatever makes it understandable to a reader without the need to go back to read the full code).

gdantimi
  • 378
  • 3
  • 9
  • 20