1

I am trying to add an object of class Track to my ArrayList of Track, playlist.

import java.util.ArrayList;

public class PlayList {
    private String playList;
    private ArrayList <Track> myTracks;

    //constructors
    public PlayList(String name) {
        this.playList = name;
    }

    public PlayList(String name, ArrayList<Track> tracks) {
        this.playList = name;
        this.myTracks = tracks;
    }

    public addTrack(Track track){
        myTracks.add(0,track)
    }
}

But both my constructor and addTrack method fails, showing java.lang.NullPointerException

Where am I going wrong?

2
  • 1
    you hven't initialize your arraylist hence your method fails, try only constructing an object your constructors must work Commented Oct 29, 2015 at 6:01
  • 1
    Your code doesn't compile. addTrack() should have a return type Commented Oct 29, 2015 at 6:08

4 Answers 4

2

You never get an exception in your constructor. There is no chance at all.

And the reason for add method is you are using myTracks.add(0,track) and you never initialize your list

private List<Track> myTracks = new ArrayList<Track>();
Sign up to request clarification or add additional context in comments.

Comments

0

You need to initialize the ArrayList and code to the interface. Use : private List<Track> myTracks = new ArrayList<>(); (for java versions below java -7, use new ArrayList<Track>();)

3 Comments

But why exception is constructor ?
@sᴜʀᴇsʜᴀᴛᴛᴀ - I guess the OP's code is incomplete. He must be trying to add items to myTracks . His current code doesn't compile
Yah. Probably he failed to understand the stacktrace.
0

I think your constructor fails because you try to initialize object from playList like this :-

playList myList = new playList();

This will give you error because there is no defualt constructor and it is not created by default (The default constructor is automatically generated unless you define another constructor)

If you didn't create your playList object like this there is no way you got an exception in the constructor

And the reason for addmethod error is you initialized your list

You have to do this :-

private List<Track> myTracks = new ArrayList<Track>();

Comments

0

You are getting "NullPointerException" in the Arraylist because you have not initialized it first. You can add items in list only after initializing it first. So write like this instead and try -

private List<Track> myTracks = new ArrayList<Track>();

As far as the constructor is concerned i think there you didnt give the required parameters. When you are calling the constructor of PlayList class you need to pass the correct values as parameters which you defined(2 Constructors in this case). if you don't then it will throw an error because there is no default(no argument) constructor. So try like this -

PlayList myList=new PlayList("//give name","//ArrayList name which contains the tracks")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.