0
DatagramPacket sendingPackets;
byte[] hellodata;
public void sendData(String toServer, String ip, int port)
{

    try
    {
        DatagramSocket sendingSocket=new DatagramSocket();
        System.out.println("From manipulate ip="+ip+"and port ="+port+toServer);

       hellodata = toServer.getBytes();
        sendingPackets = new DatagramPacket(hellodata, hellodata.length,     InetAddress.getByName(ip), port);
        sendingSocket.send(sendingPackets);
        // sendingSocket.close();
    }
    catch (Exception EX)
    {
        JOptionPane.showMessageDialog(null, "Sending Error :" + EX, "Error", JOptionPane.ERROR_MESSAGE);
    }
}


public void sendmyData(String d, String ip,int port)
{
    sr = new SendAndRecieveZone();
    sr.sendData(d, ip, port);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)                                         
{                                             
    srs = new SendAndRecieveZone();
    data = jTextField1.getText();
    srs.sendData(d, ip, port);   
}

sendData() 这个函数在“SendAndRecieveZone”类中。sendmyData() 是一个单独的类“loginClass”的函数。jButton1ActionPerformed() 在另一个类“mainClass”中。我试图从 loginClass 和 mainClass 调用 sendData(),但是从“loginClass”的第一次调用是成功的,但是当我尝试从“mainClass”调用它会创建空指针异常。我调试它并在“srs.sendData(d, ip, port);”行发现错误 主类。不知道哪里出错了。此外,当 loginClass 多次调用 sendData() 函数时,它不会产生任何错误。

4

0 回答 0