简体   繁体   English

java空指针异常-发生在while循环中的不同点

[英]java null pointer exception- Occurs at varying points in while loop

I have the following Java while loop: 我有以下Java while循环:

while(true){
    byte buffer[] = new byte[MAX_PDU_SIZE];
    packet = new DatagramPacket(buffer, buffer.length);

    socket.receive(packet);

    Pdu pdu = pduFactory.createPdu(packet.getData());

    System.out.print("Got PDU of type: " + pdu.getClass().getName());
    if(pdu instanceof EntityStatePdu){
        EntityID eid = ((EntityStatePdu)pdu).getEntityID();
        Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation();
        System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
        System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
        }
    System.out.println();
    }
}

The intended function of the while loop is to capture any PDUs that are being sent across the network, and display information about them. while循环的预期功能是捕获通过网络发送的所有PDU,并显示有关它们的信息。

When I run the code, I go get the output that I had intended in the console- at least initially... But after it has returned information about a number of the PDUs, I got an error displayed in the console (can't remember what it said now- but I thought it may be because it was trying to capture a PDU when there wasn't one being sent). 当我运行代码时,我至少会在控制台中获得预期的输出-至少在最初...但是在它返回有关多个PDU的信息后,控制台中显示了一个错误(无法还记得它现在说的话吗-但我想可能是因为它试图在没有人发送时捕获PDU)。

I have tried amending my code to account for the occasion that a PDU may not be being received over the network at the time that it is trying to capture the information about the PDU by surrounding the code with the following try- catch loop: 我尝试修改我的代码,以解决以下情况:在尝试通过以下try-catch循环将代码包围起来时,可能无法通过网络接收到PDU的情况:

try{
    socket = new MulticastSocket(EspduSender.PORT);
    address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
    socket.joinGroup(address);

    while(true){
        byte buffer[] = new byte[MAX_PDU_SIZE];
        packet = new DatagramPacket(buffer, buffer.length);

        socket.receive(packet);

        Pdu pdu = pduFactory.createPdu(packet.getData());

        System.out.print("Got PDU of type: " + pdu.getClass().getName());
        if(pdu instanceof EntityStatePdu){
            EntityID eid = ((EntityStatePdu)pdu).getEntityID();
            Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation();
            System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
            System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
            }
        System.out.println();
        }
    }
catch(Exception e){
    System.out.println(e);
    System.out.println("This is where the error is being generated");
}

However, when I now run the code- it still displays the first x number of DIS packets it captures (x varies every time I run the code), but then gives me a java.lang.NullPointerException . 但是,当我现在运行代码时-它仍然显示它捕获的前x个DIS数据包(每次运行代码时x都会变化),但随后却给出了java.lang.NullPointerException As I understand, this will occur either because the PDU that the code has captured does not contain any information, (ie an 'empty' PDU), or because it is attempting to receive a PDU when there isn't one being sent over the network. 据我了解,这可能是因为代码捕获的PDU不包含任何信息(即“空” PDU),或者是因为在没有人通过PDU发送时试图接收PDU。网络。

How can I make the code 'skip' the occasion that it doesn't receive a PDU, and just keep running? 在没有收到PDU的情况下,如何使代码“跳过”并继续运行? Or is there something else I should do to get rid of this error? 还是我应该做些其他事情来摆脱这个错误?

This may not fix your problem (won't know until you put a stack trace up), but you could check to see if pdu is null before using it. 这可能无法解决您的问题(直到您完成堆栈跟踪后才知道),但是您可以在使用前检查pdu是否为null。

while(true){
    byte buffer[] = new byte[MAX_PDU_SIZE];
    packet = new DatagramPacket(buffer, buffer.length);

    socket.receive(packet);

    Pdu pdu = pduFactory.createPdu(packet.getData());

    if (pdu != null) {
        System.out.print("Got PDU of type: " + pdu.getClass().getName());
        if(pdu instanceof EntityStatePdu){
            EntityID eid = ((EntityStatePdu)pdu).getEntityID();
            Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation();
            System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
            System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
            }
        System.out.println();
    }
}

If you get a stack trace out, I can change this for your actual problem. 如果您找到堆栈堆栈,我可以针对您的实际问题进行更改。

You code looks perfectly alright , except the below missing line. 您的代码看起来很不错,除了下面的缺失行。

if (pdu != null) {

//execute this
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM