This question already has answers here:
What is a NullPointerException, and how do I fix it?
                            
                                (12个答案)
                            
                    
                2年前关闭。
        

    

因此,我正在运行一个JUnit测试,如果没有针对电影抛出的MovieListException的评级,则它将获得movie(Hashmap:HashMap<String, Rating> movieList))的评级。

JUNIT预期与实际
https://imgur.com/a/mdQfJgT

了解有关引发异常的信息,因此我不知道自己在做什么错?


在Rating类中,我检查对象值是否为null,然后抛出MovieListException
MovieList类中,此行的错误为null:Integer value = movieList.get(string).getRating();即使我正在捕获并返回e对象(我假设它需要返回?还是基于JUNIT TEST?)



  问题:我得到的是空指针而不是我的MovieListException


测试班

    /* Test 5: Can't get a rating for an unrated movie
     */
    @Test(expected = MovieListException.class)
    public void nonexistentRating() throws MovieListException {
        String result = movies.getRating("The Ghost in the Invisible Bikini");

    }


MovieList类

public class MovieList {
private HashMap<String, Rating> movieList = new HashMap<String, Rating>();

public void addMovie(String string) {
    // TODO Auto-generated method stub
    movieList.put(string, new Rating(RatingType.NONE, null));
}



public String getRating(String string) throws MovieListException {
    // TODO Auto-generated method stub
    String ratingString = "";

    try {
        Integer value = movieList.get(string).getRating();

        if(value != null && value > 0) {
            for(int i = 0; i < value; i++) {
                ratingString += "*";
            }
            return ratingString;
        }
    } catch(MovieListException e) {
        System.out.print(e.getMessage());
        return e.getMessage();
    }


    return "No rating";
}

public void setRating(String string, Rating rating) {
    // TODO Auto-generated method stub
    movieList.replace(string,  rating);
}

package movieListQuestion;


等级等级

public class Rating {


private Integer ratingValue;
private RatingType typeValue;

public Rating(RatingType type, Integer rating) {
    this.ratingValue = rating;
    this.typeValue = type;
}

public Integer getRating() throws MovieListException {
    if(this.ratingValue == null) {
        throw new MovieListException("No rating");
    }
    return this.ratingValue;
}


}

例外类别

    package movieListQuestion;

/* A trivial exception class for the Movie List program.
 */
@SuppressWarnings("serial")
public class MovieListException extends Exception {

    public MovieListException() {
        super();
    }

    public MovieListException(String message) {
        super(message);
    }

}


MovieList类的UPDATED getRating方法

public String getRating(String string) throws MovieListException {
        // TODO Auto-generated method stub
        String ratingString = "";

            Integer value = movieList.get(string).getRating();

            if (!movieList.containsKey(string)) {
               throw new MovieListException("Movie not found");
            }

            if(value != null && value > 0) {
                for(int i = 0; i < value; i++) {
                    ratingString += "*";
                }
                return ratingString;
            }


        return "No rating";
    }

最佳答案

NullPointerException处的movieList.get(string).getRating()显然表明您搜索的电影不在movieList

您可能决定在找不到电影时抛出相同的MovieListException。你可以做到

if (!movieList.containsKey(string)) {
   throw new MovieListException("Movie not found");
}
//Rest of the code

//Or using Optional
Rating rating = Optional.ofNullable(movieList.get(string))
        .orElseThrow(() -> new MovieListException("Movie not found"));
Integer value = rating.getRating();


另外,您不应该在MovieListException方法中捕获getRating

07-24 15:24