up vote 0 down vote favorite
share [g+] share [fb]

This code takes in users and movies from two separate files and computes a user score for a movie. When I run the code I get the following error:

Exception in thread "main" java.lang.NullPointerException
    at RecommenderSystem.makeRecommendation(RecommenderSystem.java:75)
    at RecommenderSystem.main(RecommenderSystem.java:24)

I believe the NullPointerException is due to an error in this particular class but I can't spot it. Any thoughts?

import java.io.*;
import java.lang.Math;

public class RecommenderSystem
{
    private Movie[] m_movies;
    private User[] m_users;

    /** Parse the movies and users files, and then run queries against them.
     */
    public static void main(String[] argv)
                       throws FileNotFoundException, ParseError, RecommendationError
    {
        FileReader movies_fr = new FileReader("C:\\workspace\\Recommender\\src\\IMDBTop10.txt");
        FileReader users_fr = new FileReader("C:\\workspace\\Recommender\\src\\IMDBTop10-users.txt");
        MovieParser mp = new MovieParser(movies_fr);
        UserParser up = new UserParser(users_fr);

        Movie[] movies = mp.getMovies();
        User[] users = up.getUsers();

        RecommenderSystem rs = new RecommenderSystem(movies, users);
        System.out.println("Alice would rate \"The Shawshank Redemption\" with at least a "
                           + rs.makeRecommendation("The Shawshank Redemption", "asmith"));
        System.out.println("Carol would rate \"The Dark Knight\" with at least a "
                           + rs.makeRecommendation("The Dark Knight", "cd0"));
    }

    /** Instantiate a recommender system.
     *
     * @param movies    An array of Movie that will be copied into m_movies.
     * @param users     An array of User that will be copied into m_users.
     */
    public RecommenderSystem(Movie[] movies, User[] users)
           throws RecommendationError
    {
        m_movies = movies;
        m_users = users;
    }

    /** Suggest what the user with "username" would rate "movieTitle".
     *
     * @param movieTitle    The movie for which a recommendation is made.
     * @param username      The user for whom the recommendation is made.
     */
    public double makeRecommendation(String movieTitle, String username)
                  throws RecommendationError
    {
        int userNumber;
        int movieNumber;
        int j=0;
        double weightAvNum =0;
        double weightAvDen=0;

        for (userNumber = 0; userNumber < m_users.length; ++userNumber)
        {
            if (m_users[userNumber].getUsername().equals(username))
            {
                break;
            }
        }

        for (movieNumber = 0; movieNumber < m_movies.length; ++movieNumber)
        {
            if (m_movies[movieNumber].getTitle().equals(movieTitle))
            {
                break;
            }
        }

        // Use the weighted average algorithm here (don't forget to check for
        // errors).
        while(j<m_users.length){
            if(j!=userNumber){
            weightAvNum = weightAvNum + (m_users[j].getRating(movieNumber)- m_users[j].getAverageRating())*(m_users[userNumber].similarityTo(m_users[j]));
            weightAvDen = weightAvDen + (m_users[userNumber].similarityTo(m_users[j]));
            }
            j++;
        }
        return (m_users[userNumber].getAverageRating()+ (weightAvNum/weightAvDen));

    }
}

class RecommendationError extends Exception
{
    /** An error for when something goes wrong in the recommendation process.
     *
     * @param s     A string describing the error.
     */
    public RecommendationError(String s)
    {
        super(s);
    }
}
link|improve this question

69% accept rate
Try printing out the contents of m_users. – Winston Ewert Mar 20 '11 at 19:23
Thanks this helped...there were null elements in the m_users array due to a size declaration in a previous class... – algorithmicCoder Mar 20 '11 at 19:47
feedback

1 Answer

up vote 1 down vote accepted

If the file you posted is unaltered from the file that generated the stack trace you posted, then the nullpointer exception on line 75 is somewhere in this code:

weightAvNum = weightAvNum + (m_users[j].getRating(movieNumber)- m_users[j].getAverageRating())*(m_users[userNumber].similarityTo(m_users[j]));

So since m_users is not null (otherwise it would have crashed earlier) either m_users[j] or m_users[userNumber] is null, i.e., there is some null element in the m_users array.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.