Click to See Complete Forum and Search --> : Help with a java.lang.NullPointerException error


bigkahuna
04-10-2007, 09:21 PM
Hey everyone,

I am trying to finish up a program i am developing and am running into the following error. Any help you could offer would be appreciated...Thanks a lot..

Exception in thread "main" java.lang.NullPointerException
at Playlist.toString(Playlist.java:136)
at Ipod.toString(Ipod.java:57)
at MyIpodDriver.main(MyIpodDriver.java:44)

The code for each file is below...

public class Playlist
{
private Song[] theList;
private int nextSongIndex;

/**
constructor 1
@param listSize represents the size of the array needing to be created
*/
public Playlist(int listSize)
{
theList = new Song[listSize];
this.nextSongIndex = 0;
}

/**
append method that attempts to fill the array, otherwise returns false
@param Song s represents the artist, title, and length of the Song object
@return false or true, depending on the conditions
*/
public boolean append(Song s)
{
if(nextSongIndex == theList.length)
return false;
else
{
theList[nextSongIndex]=(Song)s.clone();
nextSongIndex++;
return true;
}
}

/**
get the song method
@param int n represents the index variable passed to the method
@return the song at the given index if true, else return false
*/
public Song getSong(int n)
{
if(n < nextSongIndex)
return (Song)theList[n].clone();
else
return null;
}

/**
get number of songs in list method
@return nextSongIndex, representing the number of songs in the playlist
*/
public int getNumberOfSongsInList()
{
return nextSongIndex;
}

/**
sort the array by artist
*/
public void sortByArtist()
{
int i;
for(int next=1; next<nextSongIndex;next++)
{
i=0;
while(theList[next].compareArtist(theList[i])>0 && i>next)
i++;
Song temp=theList[next];
for(int j=next; j>i; j--)
{
theList[j]=theList[j-1];
}
theList[i]=temp;
}
}

/**
sort the array by title
*/
public void sortByTitle()
{
int i;
for(int next=1; next<nextSongIndex;next++)
{
i=0;
while(theList[next].compareTitle(theList[i])>0 && i>next)
i++;
Song temp=theList[next];
for(int j=next; j>i; j--)
{
theList[j]=theList[j-1];
}
theList[i]=temp;
}
}

/**
sort the array by length
*/
public void sortByLength()
{
int i;
for(int next=1; next<nextSongIndex;next++)
{
i=0;
while(theList[next].compareLength(theList[i])>0 && i>next)
i++;
Song temp=theList[next];
for(int j=next; j>i; j--)
{
theList[j]=theList[j-1];
}
theList[i]=temp;
}
}

/**
toString method
@return the playlist, formatted with all elements of the array
*/
public String toString() // less than nextSongIndex and return single line each time
{
int i;

String str="Playlist : \n";
for(i=0; i<theList.length; i++)
{
str += i + "." + " " + theList[i].getTitle() + "(" + theList[i].getArtist() + ")" + " : " + theList[i].getLengthInSeconds() + "\n";
}
return str;
}

}


public class Ipod
{
private Playlist playlist;
private int currentPos = 0;
String str = new String();

public Ipod()
{
playlist = new Playlist(100);
}

public boolean addSong(Song s)
{
return playlist.append(s);
}

public void playNext()
{
if(playlist.getNumberOfSongsInList() == 0)
System.out.println("There are no songs in the playlist");
else
System.out.println("Currently playing: " + playlist.getSong(currentPos));

if(currentPos == playlist.getNumberOfSongsInList() -1)
currentPos = 0;
else
currentPos++;
}

public void sortSongsByArtist()
{
playlist.sortByArtist();
}

public void sortSongsByTitle()
{
playlist.sortByTitle();
}

public void sortSongsByLength()
{
playlist.sortByLength();
}

public String toString()
{
str="#########################" + "\n" + "Current Position : " + getNumberOfSongsInList() + "\n" + playlist.toString();
return str;
}

public int getNumberOfSongsInList()
{
return playlist.getNumberOfSongsInList();
}

}

import java.util.*;
import java.text.*;

public class MyIpodDriver
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Song song = new Song();
Ipod ipod = new Ipod();
int choice;

do{
System.out.println("*************************");
System.out.println("* Main menu *");
System.out.println("* 1. Add a song *");
System.out.println("* 2. Display playlist *");
System.out.println("* 3. Play next song *");
System.out.println("* 4. Sort by artist *");
System.out.println("* 5. Sort by title *");
System.out.println("* 6. Sort by length *");
System.out.println("* 0. Exit *");
System.out.println("*************************");
System.out.print("Choose an option: ");

choice = input.nextInt();

if(choice == 1)
{
song.readInput();
ipod.addSong(song);
}
else if(choice == 2)
ipod.toString();
else if(choice == 3)
ipod.playNext();
else if(choice == 4)
ipod.sortSongsByArtist();
else if(choice == 5)
ipod.sortSongsByTitle();
else if(choice == 6)
ipod.sortSongsByLength();
else if(choice == 0)
System.exit(0);
else
System.out.println("Invalid entry");

}while(choice !=0);

}
}

bigkahuna
04-10-2007, 10:56 PM
Hey everyone,

I have fixed all of the errors, however, for some reason my sorting methods in the Playlist class are not working. If someone could look over them that would be great. Basically, I am wanting to sort them by length, title, and artist. I have attached the new class files in a zip file. Thanks.

-W

agent_x91
04-16-2007, 06:43 AM
I don't have time to search through your code, but a quick piece of advice regarding NullPointerExceptions: a null pointer is thrown during runtime when you attempt to call a method of a null object. To avoid all NullPointerExceptions, you should also check whether an object is null before trying to use any of its methods, whenever it is possible that it could be.

eg.


String s=an_input_stream.readLine();

if(s!=null)
{
if(s.equalsIgnoreCase("whatever"))
; //...
}