In: Computer Science
What is wrong with my add and remove method (in bold) under the SongList that it is creating a NullPointerException?
public class Song
{
// instance variables
private String m_artist;
private String m_title;
private Song m_link;
// constructor
public Song(String artist, String title)
{
m_artist = artist;
m_title = title;
m_link = null;
}
// getters and setters
public void setArtist(String artist)
{
m_artist = artist;
}
public String getArtist()
{
return m_artist;
}
public void setTitle(String title)
{
m_title = title;
}
public String getTitle()
{
return m_title;
}
public void setLink(Song link)
{
m_link = link;
}
public Song getLink()
{
return m_link;
}
}
-------------------------------------------------------------------
public class SongList
{
// instance variables
private Song m_last;
private int m_numElements;
// constructor
// Do not make any changes to this method!
public SongList()
{
m_last = null;
m_numElements = 0;
}
// check whether the list is empty
// Do not make any changes to this method!
boolean isEmpty()
{
if (m_last == null)
return true;
else
return false;
}
// return the size of the list (# of Song nodes)
// Do not make any changes to this method!
public int size()
{
return m_numElements;
}
// add a new Song to the circular linked list with the given
artist and
// title, keeping the list sorted by *song title*.
public void add(String artist, String title)
{
// TODO: implement this method
Song newNode = new Song(artist, title);
if(m_last == null)
{
m_last = newNode;
m_numElements++;
return;
}
Song previous = null;
Song current = m_last;
while (current != null)
{
//System.out.println(current.getTitle() + " comparedTo " + title +
": " + (current.getTitle()).compareTo(title));
int compareToResult = (current.getTitle()).compareTo(title);
if (compareToResult < 0)
{
previous = current;
current = current.getLink();
//m_numElements++;
}
else
break;
}
if (previous == null)
{
newNode.setLink(m_last);
m_last = newNode;
m_numElements++;
}
else
{
previous.setLink(newNode);
newNode.setLink(current);
m_numElements++;
}
}
// remove a Song associated with the given artist and title
from the list,
// keeping the list sorted by *song title*.
public boolean remove(String artist, String title)
{
Song previous = null;
Song current = m_last;
boolean match = false;
while (current != null)
{
int compareToTitle =
(current.getTitle()).compareToIgnoreCase(title);
int compareToArtist =
(current.getArtist()).compareToIgnoreCase(artist);
if (compareToTitle < 0 && compareToArtist < 0)
{
previous = current;
current = current.getLink();
}
else if (compareToTitle == 0 && compareToArtist == 0)
{
match = true;
current = current.getLink();
if (previous == null)
m_last = current;
else
previous.setLink(current);
}
else
return match;
}
//return match;
return true;
}
// return a string representation of the list
// Do not make any changes to this method!
public String toString()
{
String listContent = "";
Song current = m_last;
if (m_last != null)
do
{
current = current.getLink();
listContent += " [" + current.getArtist() + " - " +
current.getTitle() + "]\n";
} while (current != m_last);
return listContent;
}
}
-------------------------------------------------------------------------------------------------------------------
Your both methods are right. There is error in toString() method. It has 2 errors:
1) current node is set to next node before converting its contents to string
2) condition in do while loop is not correct. It should be while(current != null).
Correct code for toString():
public String toString()
{
String listContent = "";
Song current = m_last;
if (m_last != null){
do
{
listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";
current = current.getLink();
} while (current != null);
}
return listContent;
}
}
I tested it for follwing operations:
SongList slist = new SongList();
slist.add("Akon", "I wana love you");
slist.add("Akosn", "I wana lovsd su");
System.out.println(slist.toString());
slist.remove("Akon", "I wana love you");
System.out.println(slist.toString());
Output:
if it helps you do upvote as it motivates us a lot!
if you have any doubt, do share in comments section!