vendredi 24 avril 2015

NullPointerException when trying to create objects with data from Text File

Hey guys I am just having an issue with my Reader class. It is supposed to read in a file which I delimit with bars and then use the first piece of info (a string called type that can be one of four things) to create objects to be placed into an array. I am getting a nullpointerexception and I believe the issue may be with the actual reading and parsing as opposed to the creation and filling of the array with objects but I am not sure. Thanks.

public class Reader {

private File file;
private Poetry[] contestants = new Poetry[12];
private int i;

public Reader(File file){
    this.file=file;
    i=0;
}

public Poetry[] getList(){
    return contestants;
}

public void read() throws FileNotFoundException{
    Scanner fileScan = new Scanner(file);
    while(fileScan.hasNext()){
        String line = fileScan.nextLine();
        Scanner lineScan = new Scanner(line);
        lineScan.useDelimiter("\\|");
        while(lineScan.hasNext()){
            String style = lineScan.next();
            String title = lineScan.next();
            String description = lineScan.next();
            int length = lineScan.nextInt();
            String name = lineScan.next();
            contestants[i]=sort(style, title, description, length, name);
            i++;
        }
    }
}

public Poetry sort(String style, String title, String description, int length, String name){

    if(style.equalsIgnoreCase("word")){
        return new Word(style, title, description, length, name);
    }
    else if(style.equalsIgnoreCase("humorous")){
        return new Humorous(style, title, description, length, name);
    }
    else if(style.equalsIgnoreCase("dub")){
        return new Dub(style, title, description, length, name);
    }
    else if(style.equalsIgnoreCase("choreographed")){
        return new Choreographed(style, title, description, length, name);
    }

    return new Poetry(style, title, description, length, name);
}

}

Aucun commentaire:

Enregistrer un commentaire