gpt4 book ai didi

java - 将结果集中的对象添加到数组列表

转载 作者:行者123 更新时间:2023-12-01 10:29:27 25 4
gpt4 key购买 nike

我有一个包含电影的数据库,并且我有电影作为一类。我试图将数据库中的电影加载到我的 GUI 中,但我似乎无法将它们放入我的数组列表中。我得到一个 null 异常,但我不知道哪里出了问题。

public ArrayList getMovies(ArrayList movieList2)
{
movieList = movieList2;
Connection con = null;
try {
con = DriverManager.getConnection(
databaseURL, user, password);
Statement sta = con.createStatement();


ResultSet res = sta.executeQuery(
"SELECT * FROM MOVIES");
System.out.println("List of Movies: ");
while (res.next()) {
Movie movie = new Movie();
movie.setTitle(res.getString((String)"Title"));
movie.setYear(res.getInt("Yearmade"));
movie.setGenre(res.getString("Genre"));
movie.setDuration(res.getInt("Duration"));
movie.setActors(res.getString("Actor"));
movie.setDirector(res.getString("Director"));
movieList2.add(movie);
System.out.println(movie);
}
res.close();
sta.close();
con.close();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
System.out.println(movieList2);
return movieList2;
}

这是堆栈跟踪的输出运行:

List of Movies: 
Exception: null
null
BUILD SUCCESSFUL (total time: 0 seconds)

public static void main(String[] args) {
controller.getMovies(movieList);
}

最佳答案

Initialize movieList in caller as below,
public static void main(String[] args) {
movieList = new ArrayList();
controller.getMovies(movieList);
}

and Removing below line in getMovies method
movieList = movieList2;

关于java - 将结果集中的对象添加到数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35173008/

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