gpt4 book ai didi

java - 为 ArrayList 中的对象设置变量时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:00:05 25 4
gpt4 key购买 nike

我有一个包含 Movie 对象的 ArrayList,这些对象是我使用此方法从包含 File 对象的列表生成的:

// returns a list containing movie objects with correct names
private static ArrayList<Movie> createMovieObjs(Collection<File> videoFiles) {
ArrayList<Movie> movieArrayList = new ArrayList<>();
Matcher matcher;

for (File file : videoFiles) {
matcher = Movie.NAME_PATTERN.matcher(file.getName());
while (matcher.find()) {
String movieName = matcher.group(1).replaceAll("\\.", " ");
Movie movie = new Movie(movieName);

if (!movieArrayList.contains(movie)) {
movieArrayList.add(movie);
}
}
}
return movieArrayList;

}

上面的代码一切正常,获得正确的ArrayList。

然后我想解析此 ArrayList 中每个 Movie 对象的信息并将该信息设置为该 Movie 对象:

     // want to parse genre, release year and imdbrating for every Movie object
for (Movie movie : movieArrayList) {
try {
movie.imdbParser();
} catch (IOException e) {
System.out.println("Parsing failed: " + e);
}
}

这是使用 Movie.createXmlLink 的 Movie.imdbParser(createXmlLink 本身工作得很好,所以 imdbParser - 都经过了测试):

private String createXmlLink() {
StringBuilder sb = new StringBuilder(XML_PART_ONE);
// need to replace spaces in movie names to "+" - api works that way
String namePassedToXml = this.title.replaceAll(" ", "+");
sb.append(namePassedToXml);
sb.append(XML_PART_TWO);
return sb.toString();
}

// parses IMDB page and sets releaseDate, genre and imdbRating in Movie objects
public void imdbParser() throws IOException {
String xmlLink = createXmlLink();

// using "new Url..." because my xml is on the web, not on my disk
Document doc = Jsoup.parse(new URL(xmlLink).openStream(), "UTF-8", "", Parser.xmlParser());
Element movieFromXml = doc.select("movie").first();

// using array to extract only last genre name - usually the most substantive one
String[] genreArray = movieFromXml.attr("genre").split(", ");
this.genre = genreArray[genreArray.length - 1];

this.imdbRating = Float.parseFloat(movieFromXml.attr("imdbRating"));

// using array to extract only year of release
String[] dateArray = movieFromXml.attr("released").split(" ");
this.releaseYear = Integer.parseInt(dateArray[2]);

}

问题似乎出在访问 Movie 对象时,它没有创建良好的 XMLLink,因此当它尝试访问 XML 中的流派时,它会抛出 NPE。我的错误:

Exception in thread "main" java.lang.NullPointerException
at com.michal.Movie.imdbParser(Movie.java:79)
at com.michal.Main.main(Main.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Movie.java:79 是:

String[] genreArray = movieFromXml.attr("genre").split(", ");

Main.java:52 是:

             movie.imdbParser();

最佳答案

很多诸如此类的错误都是由于数据不符合您的预期而引起的。通常,在处理大量数据时,有时可能只是一条不“符合”的记录!

你说这条线给你带来了问题;

String[] genreArray = movieFromXml.attr("genre").split(", ");

因此,将线路分开并调试您的应用程序以查找问题。如果无法调试,请将结果导出到日志文件。

  1. 检查 movieFromXml 是否不为 NULL。
  2. 检查 movieFromXml 是否包含您期望的“流派”属性
  3. 检查属性中的数据是否可以按照您的预期进行拆分,并生成有效的输出。

通常,最好在调用之前查看正在检索的数据。我经常发现将数据转储到文件中然后将其加载到外部查看器中以检查它是否符合我的预期很有用。

关于java - 为 ArrayList 中的对象设置变量时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41716486/

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