Click here to Skip to main content
15,905,616 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Java
public static void rateSong() throws FileNotFoundException
	{
		System.out.println("Please enter the song ID: ");
		input = scan.nextInt();
		
		File songList = new File("songCollection.txt");
		
		if(songList.exists()) 
		{
			Scanner readSong= new Scanner(songList);
			
			String sid = null;
			
			while(readSong.hasNext())
			{
				String str = readSong.nextLine();
				
				sid = sid + "," + str.split(",")[0];
			}
			
			//split to return object only
			String[] songID = sid.split(",");
			
			
			for(int i=0; i<songid.length;>			{
				int id = Integer.parseInt(songID[i]);
				
				if(input==id)
				{
					System.out.println("found");
					break;
				}
				
				System.out.println("invalid input");
			}
		}
        }



I get this error when i run this code. Any idea how to solve this problem?

Exception in thread "main" java.lang.NumberFormatException: For input string: "null"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at iTunes.Rate.rateSong(Rate.java:43)
	at iTunes.iTunesDemo.main(iTunesDemo.java:83)
Posted
Updated 23-Oct-13 5:03am
v2

Hi,

You are initialize sid as null. If readSong has no next elements, sid will never be initialized. So sid.split is illigal.
Try:

Java
if(sid != null) {
    String[] songID = sid.split(",");
}


UPDATE

The error no occurs in situation above that I refered (but test sid for null).
Java
Integer.parseInt(songID[i]);


Maybe songID[i] is null. I took that error in a test. If you debug it, you will get more information.

Best regards,
Filipe Marques
 
Share this answer
 
v2
Here first initial sid with null.if once initialize the string.you can't modified that modified that one

Because String is Immutable.Thats U hot the String value as null .

Use StringBuffer in Place Of String Then It Will Work



ublic static void rateSong() throws FileNotFoundException
{
System.out.println("Please enter the song ID: ");
input = scan.nextInt();

File songList = new File("songCollection.txt");

if(songList.exists())
{
Scanner readSong= new Scanner(songList);

StringBuffer sid = null;

while(readSong.hasNext())
{
String str = readSong.nextLine();
sid = sid .append(",");
sid = sid .append( str.split(",")[0]);
}

//split to return object only
String[] songID = sid.toString().split(",");


for(int i=0; i<songid.length;> {
int id = Integer.parseInt(songID[i]);

if(input==id)
{
System.out.println("found");
break;
}

System.out.println("invalid input");
}
}
}
 
Share this answer
 
v3
Comments
Member 10351768 24-Oct-13 2:17am    
its working or not please give reply

please inform me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900