This site requires JavaScript, please enable it in your browser!
Greenfoot back
javaNewbie246
javaNewbie246 wrote ...

2020/12/14

Greenfoot read file error window

javaNewbie246 javaNewbie246

2020/12/14

#
I have a program for my college final that I am having issues with, I have so far that on a key press, it should begin to read from a txt file for location information about birds and where to place them in the world, however when I press the key, I get a null error and a seperate error window pops up and simply repeats some method returns I have that state the birds health and infection count. if anybody knows what I have wrong please let me know. Below are the image of the error window and code I have that I believe to be in question.
public void act()
    {    
        
        if(Greenfoot.isKeyDown("l")){
            
            FileDialog gui = null;
            
            gui = new FileDialog(gui, "select a bird file", FileDialog.LOAD);
            gui.setVisible(true);
            
            
            String fname = gui.getDirectory() + gui.getFile();
            File f = new File(fname);

            Scanner fread;
            try{
                fread = new Scanner(f);
            }
            catch(FileNotFoundException e){
                System.out.print("Error: " + fname);
                gui.dispose();
                return;
            }
            while(fread.hasNext())
            { String word = fread.next();

                if(word.equalsIgnoreCase("bird")) {
                    addObject(new Bird(fread.nextInt()), fread.nextInt(), fread.nextInt());

                }
                else if(word.equalsIgnoreCase("distance")) {
                    Bird.setSocialDistance(fread.nextInt());
                }
                else if(word.charAt(0) == '*') {
                    fread.nextLine();
                }
                else {
                    System.out.println("error, unknown token: " + word);
                }

            }
        }
You need to login to post a reply.