-2

I am getting error "java.lang.NullPointerException" by setting my adapter in ListFragment activity.

Here is my code:

private List<Music> musicList;
public View inflate;
ListView searchList;
public class SearchFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity().getApplicationContext();
        inflate = inflater.inflate(R.layout.search, container, false);
        musicList = new ArrayList<Music>();
        adapter = new CustomListAdapter(musicList, context);
        searchList.setAdapter(adapter);
}

I have a NullPointerException in the following code:

The error is at searchList.setAdapter(adapter); so i also try to change to adapter value from new ArrayList<Music>() to new <Music>ArrayList() but no luck.

My Music class looks like this:

public class Music {
    private String title, thumbnail, duration, uri, waveform;
    private int likes, playbackCount;

    public Music() {
    }

    public Music(String name, String thumbnailUrl, int likes, String rating, int playbackCount) {
        this.title = name;
        this.thumbnail = thumbnail;
        this.likes = likes;
        this.duration = rating;
        this.playbackCount = playbackCount;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String name) {
        this.title = name;
    }

    public String getThumbnail() {
        return thumbnail;
    }
    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    public int getLikes() {
        return likes;
    }
    public void setLikes(int likes) { this.likes = likes; }

    public String getDuration() {
        return duration;
    }
    public void setDuration(String duration) {
        this.duration = duration;
    }

    public int getPlayBackCount() {
        return playbackCount;
    }
    public void setPlayBackCount(int playbackCount) {
        this.playbackCount = playbackCount;
    }

    public String getUri() {
        return uri;
    }
    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getWaveform() {
        return waveform;
    }
    public void setWaveform(String waveform) {
        this.waveform = waveform;
    }
}
Jacob
  • 69
  • 2
  • 14
  • Try this You get the NullPointerException Because you are not write below statement in you code before the set adepter. You must initialize your list view ListView searchList=(ListView)inflate.findViewById(R.id.searchList); – Pravin Fofariya Apr 01 '16 at 10:59

2 Answers2

1

You already extends ListFragment so

setListAdapter(adapter);

take a look tutorial

M D
  • 47,665
  • 9
  • 93
  • 114
1

First, You have taken ListView searchList and the same object you haven't initialized so it's giving you NullPointerException while calling searchList.setAdapter(adapter);.

Second, you have taken ListFragment so you can directly get listview using getListView() method. Even you can directly call setListAdapter(adapter) method.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295