-2

I am trying to access contents of a custom arraylist from a different class to display texts and an image on a layout. There are no build errors, however when I try to open the layout from my list, my app crashes. I am only able to access the error from my log cat in Android studio

My activity

public class PlayingActivity extends AppCompatActivity {

    private Song songs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playing);

        // Find the image view in the activity_playing.xml layout with the ID playing_album_art_img_view.
        ImageView imageView = findViewById(R.id.playing_album_art_img_view);
        // Get the Album art from the currentSong object and set this text on
        // the song ImageView
        imageView.setImageResource(songs.getAlbumArt());

        // Find the TextView in the activity_playing.xml layout with the ID playing_artiste_text_view.
        TextView pArtisteTextView = findViewById(R.id.playing_artiste_text_view);
        // Get the Artiste from the currentSong object and set this text on
        // the Artiste TextView.
        pArtisteTextView.setText(songs.getArtiste());

        // Find the TextView in the activity_playing.xml layout with the ID playing_song_text_view.
        TextView pSongTitleTextView = findViewById(R.id.playing_song_text_view);
        // Get the song title from the currentSong object and set this text on
        // the song title TextView.
        pSongTitleTextView.setText(songs.getSongTitle());

    }

` My song class

public class Song extends ArrayList<Song> {

/** Song title */
private String mSongTitle;

/** Artiste's name */
private String mArtiste;

//** song's album art*/ private Integer mAlbumArt;

/**
 * Create a new Song object.
 *
 * @param songTitle is the song item user is about to play
 *
 * @param Artiste is the musician that owns the song
 *                *
 * @param albumArt is the album art image of the song
 */
public Song(String songTitle, String Artiste, int albumArt)
/*public Song(String songTitle, String Artiste)*/{
    mSongTitle = songTitle;
    mArtiste = Artiste;
   mAlbumArt = albumArt;
}

/**
 * Get the name of song.
 */
public String getSongTitle() {
    return mSongTitle;
}

/**
 * Get the artiste
 */
public String getArtiste() {
    return mArtiste;
}


/**
 * Get the album art
 */
public Integer getAlbumArt() { return mAlbumArt; }

}

and SongAdapter for my listView

  public SongAdapter(Context context, ArrayList<Song> songs) {
    super(context, 0, songs);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.song_item, parent, false);
    }

    // Get the {@link Song} object located at this position in the list
    Song currentSong = getItem(position);

    // Find the TextView in the song_item.xml layout with the ID artiste_text_view.
    TextView artisteTextView = listItemView.findViewById(R.id.artiste_text_view);
    // Get the Artiste from the currentSong object and set this text on
    // the Artiste TextView.
    artisteTextView.setText(currentSong.getArtiste());

    // Find the TextView in the song_item.xml layout with the ID song_title_text_view.
    TextView songTitleTextView = listItemView.findViewById(R.id.song_title_text_view);
    // Get the song title from the currentSong object and set this text on
    // the song title TextView.
    songTitleTextView.setText(currentSong.getSongTitle());

    // Find the ImageView in the song_item.xml layout with the ID playing_album_art_img_view.
    ImageView songAlbumArtImageView = listItemView.findViewById(R.id.playing_album_art_img_view);
    // Get the song album art from the currentSong object and set this text on
    // the song album art ImageView.
    songAlbumArtImageView.setImageResource(currentSong.getAlbumArt());

    // Return the whole list item layout (containing 2 TextViews) so that it can be shown in
    // the ListView.
    return listItemView;
}

My song_item layout

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

     <TextView
        android:id="@+id/song_title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Ada" />

    <TextView
        android:id="@+id/artiste_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Flavour" />

Here's my error message

Process: com.example.android.musicalstructureapp, PID: 30680
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at com.example.android.musicalstructureapp.SongAdapter.getView(SongAdapter.java:58)
    at android.widget.AbsListView.obtainView(AbsListView.java:2491)
Lilvinco
  • 95
  • 1
  • 1
  • 7

1 Answers1

3

Your this line is faulty:

ImageView songAlbumArtImageView = listItemView.findViewById(R.id.playing_album_art_img_view); // May be id is wrong.

songAlbumArtImageView.setImageResource(currentSong.getAlbumArt()); // So it's crashing here.
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • Thanks for your response.. Even after I remove everything related to the ImageView, I get the same error for the TextView following – Lilvinco Oct 04 '18 at 17:51
  • can you post your `R.layout.song_item` xml file? two possibilities are Ids you're taking are wrong else your `currentSong` object is null. – Jeel Vankhede Oct 04 '18 at 18:01
  • I have added my song_item layout file however I believe it has to do with my currentSong – Lilvinco Oct 04 '18 at 19:55
  • By removing the ImageView implementation of my problem, I evaded the NPE however, for others experiencing similar problem.. A better approach would have been to add a new contructor in my word class that would handle the view with image to avoid those without images from causing errors – Lilvinco Oct 09 '18 at 09:03