返回空指针异常但预期 MovieListException?
Returning Null Pointer Exception BUT expected MovieListException?
所以我是运行一个JUnit测试,得到电影的评分(Hashmap:HashMap<String, Rating> movieList)
) 但是如果没有针对电影的评级抛出 MovieListException。
JUNIT 预期与实际
https://imgur.com/a/mdQfJgT
学习抛出异常所以我不知道我做错了什么?
- 在评级class中我检查对象值是否为空,然后抛出
MovieListException
- 在
MovieList
class 中,此行出现空错误:Integer value = movieList.get(string).getRating();
即使我正在捕获并返回 e 对象 (我认为它需要 RETURN?基于 JUNIT 测试?)
ISSUE: I am getting a null pointer instead of my MovieListException
测试class
/* 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");
}
电影列表class
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;
评分class
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;
}
}
异常class
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 方法 class
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";
}
在movieList.get(string).getRating()
的NullPointerException
很明显你搜索的电影不在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();
此外,您不应该在 getRating
方法中捕获 MovieListException
movieList.get(string).getRating();
的movieList.get(string)
是null
。这意味着您在 movieList
.
中没有名为 string
的电影
我认为你应该调用下面的方法
public void setRating(String string, Rating rating) {
// TODO Auto-generated method stub
movieList.replace(string, rating);
}
从你的 junits 适当地。
所以我是运行一个JUnit测试,得到电影的评分(Hashmap:HashMap<String, Rating> movieList)
) 但是如果没有针对电影的评级抛出 MovieListException。
JUNIT 预期与实际 https://imgur.com/a/mdQfJgT
学习抛出异常所以我不知道我做错了什么?
- 在评级class中我检查对象值是否为空,然后抛出
MovieListException
- 在
MovieList
class 中,此行出现空错误:Integer value = movieList.get(string).getRating();
即使我正在捕获并返回 e 对象 (我认为它需要 RETURN?基于 JUNIT 测试?)
ISSUE: I am getting a null pointer instead of my
MovieListException
测试class
/* 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");
}
电影列表class
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;
评分class
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;
}
}
异常class
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 方法 class
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";
}
在movieList.get(string).getRating()
的NullPointerException
很明显你搜索的电影不在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();
此外,您不应该在 getRating
方法中捕获 MovieListException
movieList.get(string).getRating();
的movieList.get(string)
是null
。这意味着您在 movieList
.
string
的电影
我认为你应该调用下面的方法
public void setRating(String string, Rating rating) {
// TODO Auto-generated method stub
movieList.replace(string, rating);
}
从你的 junits 适当地。