Skip to content

Suggestion to Resolve Small Warning in loadModel() #8

@apotvin96

Description

@apotvin96

loadModel will currently show a warning in eclipse and other java IDE that the reader of type BufferedReader has not been closed. This is due to a try/catch/finally/needed. I have been watching these to transfer from c++ opengl work to java and noticed this issue and just wanted to give a heads up. I edited my personal copy of the code here...

public static Model loadModel(File f) throws IOException {
    BufferedReader reader = null;
    Model m = null;
    try{
        reader = new BufferedReader(new FileReader(f));
        m = new Model();
        String line;
        while ((line = reader.readLine()) != null) {
            String prefix = line.split(" ")[0];
            if (prefix.equals("#")) {
                continue;
            } else if (prefix.equals("v")) {
                m.getVertices().add(parseVertex(line));
            } else if (prefix.equals("vn")) {
                m.getNormals().add(parseNormal(line));
            } else if (prefix.equals("f")) {
                m.getFaces().add(parseFace(m.hasNormals(), line));
            } else {
                throw new RuntimeException("OBJ file contains line which cannot be parsed correctly: " + line);
            }
        }
    }catch (Exception e){
        System.out.println(e.getMessage());
    }finally{
        reader.close();
    }

    return m;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions