Hi! I have a school project where I have to make a array that includes facts about films. Every space in the array includes leanght of the film, authour, grade, company, and title.

So I know how to write it to a file but I have no idea how to write it back in to the array.
Here is my code so far:

try{
    FileReader fr = new FileReader ("filmer.txt");
    BufferedReader inFil = new BufferedReader(fr);
    String rad = inFil.readLine();
    while (rad != null){
        System.out.print(rad + "\n");
        rad = inFil.readLine();
    }
    inFil.close();
}
catch(FileNotFoundException e1){
    System.out.println("Filen hittas inte");
}
catch(IOException e2){
    System.out.println(e2);
}

Edited 3 Years Ago by Dani: Formatting fixed

Perhaps you should start by deciding what the array will look like, then write its declaration, then you can think about how to transfer data from the file to it. You also need to do the same for the data in the file, if you haven't already done so.

I have already done that. This is just my code for reading fron the file.

In that case all you need to do is take each record from the file as you read it and put it into the array- the first record goes into element [0]. the next one into element [1] etc

Yes I know that is what I need to do but I have no idea how I'm supposed to do it :)

I have done that multiple times and I don't find any good. Thats why I'm asking for advise.

I know how a array works but I don't know how to read anything from a txt file into an array.

OK, I think I'm getting closer to understanding your problem...
You can't just read a file into an array - you need a loop in which you read the records from the file into a String (which you've already got). In that loop just put each String into the next available element of the array - myArray = rad; where i starts at 0 and gets increased by 1 every time you add String to the array.

I writing it like this but "rad" won't work.

                    for (int n = 0; n < antal; n++){

                        filmer[n] = rad;
                    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Can you write a code example. I don't really understand :/

Sorry man, but the rules of this site say I shouldn't just give you the answer to your homework. Give it your best shot, then post your code with an explanation of exactly where you're stuck, then I (or someone) will give you some help on that particular point. Go for it

This is how far I am now. But it only says:

Exception in thread "main" java.lang.NullPointerException
    at DVDLeNiTest.main

DVDLeNiTest is the programs name.

                try{
                    FileReader fr = new FileReader ("filmer.txt");
                    BufferedReader inFil = new BufferedReader(fr);
                    String rad = inFil.readLine();
                    while (rad != null){
                        System.out.println(antal);
                        StringTokenizer tokenizer = new StringTokenizer (rad, "\n");
                        filmer[antal].sattTitel(tokenizer.nextToken());
                        filmer[antal].sattRegissor(tokenizer.nextToken());
                        filmer[antal].sattBolag(tokenizer.nextToken());
                        filmer[antal].sattLangd(tokenizer.nextToken());
                        filmer[antal].sattBetyg(tokenizer.nextToken());
                        antal++;
                    }
                    inFil.close();
                }
                catch(FileNotFoundException e1){
                    System.out.println("Filen hittas inte");
                }
                catch(IOException e2){
                    System.out.println(e2);
                }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You need to look at the exact line number where the exception was thrown. NullPointer means either something used in that line is uninitialised, or a function has returned a null value (check the API to see if/when this can happen).

This article has been dead for over six months. Start a new discussion instead.