Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Custom Serialization

Error: Non static method importFrom cannot be referenced from a static context

here is the main method in Karaoke.java

    public static void main(String[] args) {
        SongBook songBook = new SongBook();
        SongBook.importFrom("songs.txt");
        KaraokeMachine machine = new KaraokeMachine(songBook);
        machine.run();
        System.out.println("Saving book....");
        songBook.exportTo("songs.txt");
    }

and here is the method in SongBook.java

   public void importFrom(String fileName) {
        try (
                FileInputStream fis = new FileInputStream(fileName);
                BufferedReader reader = new BufferedReader(new 
                        InputStreamReader(fis));
        ){
            String line;
            while((line = reader.readLine()) != null) {
                String[] args = line.split("\\|");
                addSong(new Song(args[0], args[1], args[2]));
            }
        } catch (IOException ioe) {
            System.out.printf("Problems loading %s %n", fileName);
            ioe.printStackTrace();
        }
    }

I can't figure out why I am getting this error? when I change importFrom to a static method it requires further methods being changed. It seems like importFrom should not need to be static but I can't figure out what else would cause this error.

1 Answer

Agh... figured it out right after I posted this. I capitalized SongBook when calling the method.