服务器端和客户端,Java
我试图让客户端向服务器发送一些数据。由于某种原因,它总是进入异常区域 我只想让服务器接受数据。然后,它需要完成这个简单的功能 套接字、读写器的初始化正常 客户端代码:服务器端和客户端,Java,java,client-server,Java,Client Server,我试图让客户端向服务器发送一些数据。由于某种原因,它总是进入异常区域 我只想让服务器接受数据。然后,它需要完成这个简单的功能 套接字、读写器的初始化正常 客户端代码: public void SendPlayer(String Name, float Score,int place) throws NullPointerException { out.println("New High Score"); try { while (!in.readLine(
public void SendPlayer(String Name, float Score,int place) throws NullPointerException
{
out.println("New High Score");
try
{
while (!in.readLine().equals("ACK"));
out.println(Name);
out.println(Score);
out.println(place);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(true)
{
try
{
if (in.ready())
{
option = in.readLine();
while(option == null)
{
option = in.readLine();
}
switch(option)
{
case ("New High Score"):
{
out.println("ACK");
System.out.println("ack has been sent");
this.setHighScore(in.readLine(),Integer.parseInt(in.readLine()),
Integer.parseInt(in.readLine()));
break;
}
default:
{
System.out.println("nothing");
break;
}
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
System.out.println("Exception e");
}
}
服务器端代码:
public void SendPlayer(String Name, float Score,int place) throws NullPointerException
{
out.println("New High Score");
try
{
while (!in.readLine().equals("ACK"));
out.println(Name);
out.println(Score);
out.println(place);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(true)
{
try
{
if (in.ready())
{
option = in.readLine();
while(option == null)
{
option = in.readLine();
}
switch(option)
{
case ("New High Score"):
{
out.println("ACK");
System.out.println("ack has been sent");
this.setHighScore(in.readLine(),Integer.parseInt(in.readLine()),
Integer.parseInt(in.readLine()));
break;
}
default:
{
System.out.println("nothing");
break;
}
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
System.out.println("Exception e");
}
}
您的问题是无法将null与字符串进行比较。 最好的方法是用try-and-catch函数调用
try {
SendPlayer (/*Here the params*/);
//Continue, since no null pointer exception has been thrown
} catch (NullPointerException) {
//Your handling code here...
}
根据您的评论-完全例外是if(in.ready)
从异常中可以清楚看出,中的变量未初始化。请再检查一遍
在比较String
时避免NullPoinerException
的最佳做法是按如下所示的相反顺序进行比较:
while (!"ACK".equals(in.readLine()));
out.println(Name); // String
out.println(Score);// float
out.println(place); // int
您的代码中还有一个问题
客户端向服务器发送三个值,如下所示:
while (!"ACK".equals(in.readLine()));
out.println(Name); // String
out.println(Score);// float
out.println(place); // int
现在,在服务器端,您正在使用Integer.parseInt(in.readLine())
将float
转换为int
,如下所示,这将导致NumberFormatException
this.setHighScore(in.readLine(),Integer.parseInt(in.readLine()),
Integer.parseInt(in.readLine()));// converting Score to int
比如说
Integer.parseInt("2.0");
将导致
java.lang.NumberFormatException: For input string: "2.0"
java.lang.NumberFormatException: For input string: "2.0"
还有一个示例代码
float val = 2;
String str = String.valueOf(val);
Integer.parseInt(str);
将导致
java.lang.NumberFormatException: For input string: "2.0"
java.lang.NumberFormatException: For input string: "2.0"
请分享您的while(!in.readLine().equals(“ACK”))的全部例外情况代码>包含代码>?等待直到我没有得到确认…完全异常是if(in.ready)行上的NullPointerException