抛出NullPionterException。我不知道哪里出问题了?

时间:2020-11-06 09:34:36

标签: nullpointerexception

抛出NullPointerException。我不知道哪里出了问题。这是一种显示特定歌曲的歌手姓名的方法

公共列表queryArtistForSong(String songName,int sortOrder){

    StringBuilder sb = new StringBuilder(QUERY_ARTIST_FOR_SONG_START);
    sb.append(songName);
    sb.append("\"");

    if(sortOrder != ORDER_BY_NONE) {
        sb.append(QUERY_ARTIST_FOR_SONG_SORT);
        if(sortOrder == ORDER_BY_DESC) {
            sb.append("DESC");
        } else {
            sb.append("ASC");
        }
    }

    System.out.println("SQL Statement: " + sb.toString());

    try (Statement statement = con.createStatement();
         ResultSet results = statement.executeQuery(sb.toString())) {

        List<SongArtist> songArtists = new ArrayList<>();

        while(results.next()) {
            SongArtist songArtist = new SongArtist();
            songArtist.setArtistName(results.getString(1));
            songArtist.setAlbumName(results.getString(2));
            songArtist.setTrack(results.getInt(3));
            songArtists.add(songArtist);
        }

        return songArtists;
    } catch(SQLException e) {
        System.out.println("Query failed: " + e.getMessage());
        return null;
    }
}

0 个答案:

没有答案