[英]NullpointerException in Client Server program Java
I am writing a simple Client Server program in java. 我正在用Java编写一个简单的Client Server程序。 Client sends a filename to server.
客户端向服务器发送文件名。 Server receives it, then parses it and after parsing the filename it sends response to client wheather filename is correct or invalid.
服务器接收它,然后解析它,并且在解析文件名之后,它将响应发送给客户端,而文件名正确或无效。
In server, when it parses the filename it throws NullPointerException error. 在服务器中,当解析文件名时会引发NullPointerException错误。
Here is the code: 这是代码:
Server.java
: Server.java
:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class Server extends Thread {
private static int port;
private ServerSocket serverSocket=null;
private String ReceivedFilename=null;
private String ServerResponse=null;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(20000);
}
public void run() {
while (true) {
try {
System.out.println("Waiting for client to send File name...");
Socket server = serverSocket.accept();
System.out.println("Connected to Client");
DataInputStream in = new DataInputStream(server.getInputStream());
ReceivedFilename = in.readUTF();
System.out.println("Received Filename: "+ReceivedFilename);
if(ReceivedFilename == "")
ServerResponse = "Filename is empty!";
else{
if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)
{
ServerResponse = "Correct file name provided.";
}
else
{
ServerResponse = "Invalid Filename! Server only accepts .bat files";
}
}
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF(ServerResponse);
server.close();
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
port = 9000;
try {
Thread t = new Server(port);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
: Client.java
:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
private static String serverName;
public static void main(String[] args) {
String sName = "localhost";
int port = 9000;
String Filename = "Scripts.bat";
try {
System.out.println("Connecting to Server");
Socket client = new Socket(sName, port);
System.out.println("Connected to Server");
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
System.out.println("Sending filename to Server");
out.writeUTF(Filename);
System.out.println("Filename sent");
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
} catch (IOException e) {
}
}
}
Following is the output and exception on Server: 以下是服务器上的输出和异常:
Please help out. 请帮忙。
ServerResponse=null
You dont set ServerResponse anywhere apart from in your if branch
, but it looks like it will go straight to your else branch
where you call ServerResponse.indexOf
- it is still null at this point. 您无需在
if branch
任何位置设置ServerResponse,但是看起来它将直接进入您的else branch
,在此您调用ServerResponse.indexOf
它仍然为null。
Also, you should name your variables starting with a lowercase, ServerResponse
should be serverResponse
. 另外,您应该以小写字母命名变量,
ServerResponse
应该是serverResponse
。
The ServerResponse
is null as the if
condition to check the ReceivedFilename
assigns a default value only when the ReceivedFilename
is blank. ServerResponse
为null,因为仅当ReceivedFilename
为空白时,用于检查ReceivedFilename
的if
条件才分配默认值。
if(ReceivedFilename.equals("")) // I changed it to use equals method
ServerResponse = "Filename is empty!"; // only here the default value is assigned
You need to assign the Receivedfilename
to your ServerResponse
looking at the logic in the code. 您需要查看代码中的逻辑,将
Receivedfilename
分配给ServerResponse
。
if(ReceivedFilename.equals("")) // I changed it to use equals method
ServerResponse = "Filename is empty!";
else{
// If it comes to the else part, the ServerResponse is still null as initialized at the start and therefore throws the NPE
ServerResponse = ReceivedFilename; // incase ReceivedFilename is not blank
if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)
...
}
As a side note, you need to use equals()
method to compare String
values and not use the ==
operator which compares the String object references. 附带说明,您需要使用
equals()
方法来比较String
值,而不要使用==
运算符来比较String对象的引用。 You could probably trim()
the ReceivedFilename when trying to check if its equals with blank String, but that's upto you. 尝试检查空白字符串是否等于ReceivedFilename时,您可能会
trim()
ReceivedFilename,但这取决于您。 Ofcourse you've many different ways to check if a String is empty or not. 当然,您可以通过多种方法来检查String是否为空。 You can either right the method to do that yourself or take help from third-party libraries which already have that method.
您可以自行修改该方法,也可以从已经拥有该方法的第三方库中寻求帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.