线程“main”中的异常 [英] Exception in thread "main"

查看:98
本文介绍了线程“main”中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   static   void  rateSong() throws  FileNotFoundException 
{
System.out.println( 请输入歌曲ID:);
input = scan.nextInt();

文件songList = 文件( songCollection.txt);

if (songList.exists())
{
Scanner readSong = 扫描仪(songList);

String sid = null;

while (readSong.hasNext())
{
String str = readSong.nextLine();

sid = sid + + str.split( )[ 0 ];
}

// 拆分为仅返回对象
字符串 [] songID = sid.split( );


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

if (input == id)
{
System.out.println( 找到);
break ;
}

System.out .println( 无效输入);
}
}
}







我运行此代码时出现此错误。任何想法如何解决此问题?



线程main中的异常java.lang.NumberFormatException:对于输入字符串:null
在java .lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lan g.Integer.parseInt(未知来源)
在iTunes.Rate.rateSong(Rate.java:43)
在iTunes.iTunesDemo.main(iTunesDemo.java:83)

解决方案





你初始化 sid as null 。如果 readSong 没有下一个元素, sid 将永远不会被初始化。所以 sid.split 是illigal。

尝试:



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





更新



错误没有发生在上面提到的情况(但测试 sid 为null)。

 Integer.parseInt(songID [i] ); 





可能 songID [i] 为空。我在测试中接受了这个错误。如果您调试它,您将获得更多信息。



祝你好运,

Filipe Marques


这里首先用null.if初始化sid一次初始化string.you无法修改那个修改过的那个



因为String是Immutable.Thats U hot the String value as null。



在字符串中使用StringBuffer然后它会工作







ublic static void rateSong()抛出FileNotFoundException

{

System.out.println(请输入歌曲ID: );

input = scan.nextInt();



文件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]); < br $>
}



//拆分为仅返回对象

String [] songID = sid.toString()。 split(,);





for(int i = 0; I< songid.length;> {

int id = Integer.parseInt(songID [i]);



if(input == id)

{

System.out.println(找到);

休息;

}



System.out.println(无效输入);

}

}

}

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)

解决方案

Hi,

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

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



UPDATE

The error no occurs in situation above that I refered (but test sid for null).

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


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");
}
}
}


这篇关于线程“main”中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆