gpt4 book ai didi

java - 返回空指针异常但预期有 MovieListException?

转载 作者:行者123 更新时间:2023-12-02 11:20:16 27 4
gpt4 key购买 nike

所以我正在运行 JUnit 测试,它获取电影的评级(Hashmap: HashMap<String, Rating> movieList) )但是如果没有对电影的评分抛出 MovieListException。

JUNIT 预期与实际 /image/DnFE1.jpg

了解抛出异常,这样我就不知道自己做错了什么?

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

ISSUE: I am getting a null pointer instead of my 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 类中的 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";
}

最佳答案

一个NullPointerExceptionmovieList.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();

此外,您不应该捕获 MovieListExceptiongetRating方法

关于java - 返回空指针异常但预期有 MovieListException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com