java.lang.NumberFormatException: null in Integer.parseInt

Okay, the following are 2 classes that I am using in creating a simple TFTP (Trivial File Transfer Protocol) program. I am running a server instance and a client instance of this program. What I am trying to accomplish is this: The client connects to the server and sends a message of a specific file that it wants. The server locates the file and sends a response (either a 1 or 0... 1 meaning the file is there, and 0 meaning that it is not). Then the server will send the content of the file to the client application. The files I am trying to send are just simple text files. Right now, I am able to receive the name of the text file that the client wants, but then when I go to send back the response, I am not getting anything returned. Also below are the methods that the server and the client both run. This is the server instance SwingUtilities.invokeLater(new Runnable(){ public void run(){ String fileRequest = UDPReceiver(SERVER_PORT_NUMBER); outputArea.append("\n" + "File Requested: " + fileRequest + "\n"); outputArea.append("Determining if file exists...\n"); String checkFile = SHARED_DIR + "\\" + fileRequest; outputArea.append("Checking location: " + checkFile + "\n"); boolean check = fileCheck(checkFile); if(check == true){ outputArea.append("File location verified..." + "\n"); outputArea.append("Initiating transfer...." + "\n\n"); UDPSender(CLIENT_HOSTNAME, CLIENT_PORT_NUMBER, "1"); } else{ outputArea.append("File does not exist..." + "\n"); outputArea.append("Exiting run..." + "\n"); } } }); The client instance. SwingUtilities.invokeLater(new Runnable(){ public void run(){ UDPSender(SERVER_HOSTNAME, SERVER_PORT_NUMBER, FILE_REQUEST); String message = UDPReceiver(CLIENT_PORT_NUMBER); outputArea.append("\n\n" + message + "\n"); if(message == "1"){ // File exists outputArea.append("\n"); outputArea.append("File verified..." + "\n"); outputArea.append("Transfer initiated..." + "\n"); } else{ // File doesn't exist outputArea.append("\n"); outputArea.append("File does not exist..." + "\n"); outputArea.append("Terminating connection..."); } } }); Here are the Sender and Receiver methods. private void UDPSender(String hostname, String port, String message){ DatagramSocket socket = null; try{ // Create a datagram socket, look for the first available port socket = new DatagramSocket(); outputArea.append("Using local port: " + socket.getLocalPort() + "\n"); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); PrintStream pOut = new PrintStream(bOut); pOut.print(message); // Convert printstream to byte array byte[] bArray = bOut.toByteArray(); // Create a DatagramPacket, containing a maximum buffer of 256 bytes DatagramPacket packet = new DatagramPacket(bArray, bArray.length); outputArea.append("Looking for hostname " + hostname + "\n"); // Get the InetAddress object InetAddress remote_addr = InetAddress.getByName(hostname); // Check its IP Number outputArea.append("Hostname has IP Address = " + remote_addr.getHostAddress() + "\n"); // Configure the DatagramPacket packet.setAddress(remote_addr); packet.setPort(Integer.parseInt(port)); // Send the packet socket.send(packet); outputArea.append("Packet sent at: " + new Date() + "\n"); // Display packet information outputArea.append("Sent by: " + remote_addr.getHostAddress() + "\n"); outputArea.append("Sent from: " + packet.getPort() + "\n"); socket.close(); } catch(UnknownHostException ue){ outputArea.append("Unknown host: " + hostname + "\n"); outputArea.append("Unknown host: " + ue + "\n"); } catch(IOException e){ outputArea.append("Error: " + e + "\n"); } } private String UDPReceiver(String portNum){ String message = ""; DatagramSocket socket = null; try{ // Create a DatagramSocket socket = new DatagramSocket(Integer.parseInt(portNum)); outputArea.append("Listening on local port " + socket.getLocalPort() + "\n"); // Create a DatagramPacket, containing a maximum buffer of 256 bytes DatagramPacket packet = new DatagramPacket(new byte[256], 256); // Receive a packet - remember by default this is a blocking operation socket.receive(packet); outputArea.append("Packet received at " + new Date() + "\n"); // Display packet information InetAddress remote_addr = packet.getAddress(); outputArea.append("Sender: " + remote_addr.getHostAddress() + "\n"); outputArea.append("From Port: " + packet.getPort() + "\n"); CLIENT_HOSTNAME = remote_addr.getHostAddress(); //CLIENT_PORT_NUMBER = Integer.toString(packet.getPort()); // Display packet contents, by reading from byte array ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData()); // Display only up to the length of the original UDP packet for(int i = 0; i

以上就是java.lang.NumberFormatException: null in Integer.parseInt的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏