为什么这个Java代码中存在空指针异常?

时间:2011-03-20 19:18:35

标签: java nullpointerexception

此代码从两个单独的文件中获取用户和电影,并计算电影的用户分数。当我运行代码时,我收到以下错误:

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

我认为NullPointerException是由于这个特殊课程中的错误而导致我无法发现的。有什么想法吗?

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);
    }
}

1 个答案:

答案 0 :(得分:1)

如果您发布的文件未从生成您发布的堆栈跟踪的文件中更改,则第75行的nullpointer异常位于此代码中的某处:

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

因为m_users不为空(否则它会先崩溃)m_users[j]m_users[userNumber]为空,即m_users中有一些空元素阵列。