문제

My current program reads from a file adds that data to a customer object and then adds that data to a list. There is a second file, I wish to read from, that would also add data to my customer object. A customer rating file, however not all customers have ratings. This is what I've done so far:

public void readFromFile()
{
    try
    {
        BufferedReader inF = new BufferedReader(new FileReader("Customers.txt"));
        BufferedReader inFR = new BufferedReader(new FileReader("Ratings.txt"));

        String s = inF.readLine();
        String s2 = inFR.readLine();
        while(s != null)
        {
            String z[] = s.split(",");
            String y[] = s2.split(" ");      
            Customer c = new Customer(z[0], z[1], z[2], Integer.parseInt(z[3]),z[4]);
            if(c.getCustomerNr().equals(y[0]))
            {
               c.setRating(Integer.parseInt(y[1]));
            }
            else
            {
                c.setRating(0);
            }
            myList.add(c);
            s = inF.readLine();
            s2 = inFR.readLine();
        }

    }
    catch (FileNotFoundException ex)
    {
        System.out.println("File does not exsit.");
        System.exit(0);
    }
    catch (IOException ex)
    {
        System.out.println("Could not find line");
        System.exit(0);
    }
}

However I keep getting the error "Null pointer exception" at my split method for s2. I'm not sure what I'm doing wrong. It would also be awesome if someone could give me a hint as to where I need to go, as I like to try and figure things out on myself, to the best of my ability. However, if you provide an answer, I will also be happy.

Thanks

도움이 되었습니까?

해결책

Your code assumes the same number of lines in the files, whereas apparently "Ratings.txt" has fewer lines.

My recommendation would be to process the files in separate loops. When reading the ratings file, search through your customer list for the customer with the correct customer number.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top