Why is there a Null Pointer Exception in this Java Code?
- by algorithmicCoder
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 null pointer exception 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);
}
}