Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default Printing memory addresses and null values

    Hi everyone,
    Firstly, I hope I'm on the right part of this forum and thanks in advance for anyone who takes the time to read/reply.

    My program uses the JPcap API so there are a few things in the code print out below from that. In short, I am trying to caputre packets and at the moment, simply print out the payload.

    My problem: At the moment I can capture packets but as soon as it hits a null value I get a Null Exception Error. I've tried to put in a while loop and an If statement. (The IF is nested in a For loop so it should be picked up on each cycle).

    Secondly, when I do get a print out, so before I hit the null value, I seem to only be getting memory addresses. But I've tried putting in .toString to get the contents of memory with no luck :-(

    *****
    int index = 0;
    JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], -1, false, 20);

    //Capture the packets
    for (int o = 0; o < 10; i++) {
    //Store all the captured packets in data
    //Do not capture empty packets otherwise nullexception error thrown
    // while (captor.getPacket() != null) {
    // Packet packet = captor.getPacket();
    // byte[] data = packet.data;
    // System.out.println(data);


    if (captor.getPacket() != null) {
    Packet tempPacket = captor.getPacket();
    byte[] payload = tempPacket.data;
    System.out.println(payload.toString());

    // if (data.contains("MSG")
    // && data.contains("Content-Type: text/plain")) {
    // System.out.println(data);
    }

    }
    captor.close();
    }

    ******


    Does anyone have any suggestions for either problem?

    Thanks
    Ben

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Thumbs up

    Ben1,

    I have never used JPcap and without you identifying where exactly the error is thrown then I would have guess it is at the following line:

    byte[] payload = tempPacket.data;

    If so, then I would say that although each packet is being validated, its content is not. It may be that you are trying to print out the data when there is none hence the null value exception.

    The second part of your problem relates to the default String value of the toString method. This is always the memory address unless it has been overridden by a custom method which you will have to implement yourself.

    Regards.
    Last edited by Ronin; 10-07-2010 at 04:01 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    To see the contents of an array, use the Arrays.toString() method.
    What format do you want the bytes to be printed in? int or char?

  4. #4
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Hiya,
    Yeah sorry I should have posted the error I'm getting. You did guess correctly though :). It is the "byte[] payload = tempPacket.data;" line.

    I why the null value error is happening, but the code I have below doesn't seem to work, I was hoping someone could shed some light to why?


    On the second point, if toString isn't the best option, due to it accessing the memory address rather than the contents of memory, what would be the best method?

    Unfortunatly because I'm using JPcap I have to use byte[] to store the packet as soon as it's captured. Later, once I have the packet I will store it to a String variable and do my processing from there.

    Sorry if these seems a bit trivial, but Java isn't my strong point :(

    Thanks
    Ben

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    what would be the best method
    What are you trying to do?
    Look at the String class for a constructor to convert it to String.

  6. #6
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Sorry, I'm being unclear.

    The current problem is, I'm trying to capture network packets for a piece of software I'm producing. I currently have the program outputing, all be it memory addresses, but one problem at a time :), but as soon as there is a lapse in network activity and a null value is produced I get a Null Value Exception error.

    Is there a method of saying "If captor is null, then ignore it". In this case captor is the variable used to store the captured packet.

    **
    int index = 0;
    JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], -1, false, 20);

    //Capture the packets
    for (int o = 0; o < 10; i++) {

    if (captor.getPacket() != null) {
    Packet tempPacket = captor.getPacket();
    byte[] payload = tempPacket.data;
    System.out.println(payload.toString());

    }

    }
    captor.close();
    }
    **

    As you can see from the snippet above, I have tried to impliment an IF statement within the FOR loop. So every time a new packet is captured it checks if it's null before any System.out's take place.

    Does that make more sense?

    Thank you for taking the time to reply, it's appreciated :)

    Ben

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Are you still getting the NPE? If so, What variable is null?
    Does getPacket() change anything in the captor object? Working like an iterator: each get moves to the next item. So testing this one, doesn't show what the next one will be.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default

    Ben1,

    What I meant to imply in my last post was that although you are receiving packets are constantly being set through the network, there is not always data in the packets.
    You need to validate the content of each packet to ensure data exists before you can send it to the byte array.

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Does getPacket() change anything in the captor object?
    Valid point. It could be that the first call to getPacket returns a valid packet whilst the second call references the next packet as opposed to the one intended.

    Regards.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Yep, as Norm has noted, I suspect getPacket() returns the packet and then the captor moves onto the next packet...a bit like iterator.next().

    You want to do getPacket() and store that in temp, and then do the null check on temp.

    ETA: And just to be thoroughly pedantic, there's nothing that says that value output by Object.toString() is the memory address. It is in fact the hashcode(), which may or may not be the memory address (it is in most JVMs for an Object). This is important since if you override hashCode() and not toString() you'll get a different result from toString() than you would if you hadn't overridden hashCode().

  11. #11
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    You guys are worth your weight in gold!

    That worked a treat. I thought I'd tried all variations of what I had, but apparently not.

    I'm now getting a constant stream of different memory addresses through. That solves my first problem :-).

    If anyone else has this problem the code was:
    ***
    for (int o = 0; o < 10; i++) {
    //Store all the captured packets in data
    //Do not capture empty packets otherwise nullexception error thrown

    Packet tempPacket = captor.getPacket();
    if (tempPacket != null) {
    byte[] payload = tempPacket.data;
    System.out.println(payload.toString());

    }
    captor.close();
    ***


    Just on a quick note, does anyone happen to know why I'm getting memory addresses rather than the data itself? Or would you guys advise a fresh post?

    Thanks again
    Ben

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Your byte[] doesn't really have a toString. It's a basic array, so it defaults to the toString() of Object, which is the hashCode, which is defaulted to the emmory address.

    Use Arrays.toString().

  13. #13
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Hiya,
    I kind of understand where you're coming from, but lost at the same time.

    Why doesn't "(payload.toString()" count as a true toString statement?

    I've read the javadoc you posted but still no clearer for me :(

    Sorry, it's been a while since I've done any real programmng (VB doesn't count :p).


    Thanks
    Ben

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    That array doesn't have a toString() method of its own, so it defaults to Object's toString().
    Have you done inheritance?

    Call "Arrays.toString(payload);" insteand of payload.toString();

  15. #15
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Ahhh excellent.

    I'm now getting the data :-). Not what I was expecting to see, but it's the data none the less.

    Thank you so much for taking the time to reply :)

    Ben

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Not what I was expecting to see
    Is that because the bytes are printed as decimal values and you want to see characters or Strings? If the data is chars and can be changed to a String see the String class's constructor.
    Or to see the data in hex, make a loop over the byte array using the Integer.toHexString() method.

  17. #17
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    I'm getting:
    [96, 0, 0, 0, 0, 32, 58, -1, -2, -128, 0, 0, 0, 0, 0, 0, 53, -16, 47, -80, 127, -86, -82, -35, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 110, -116, 74, -121, 0, -124, 21, 0, 0, etc etc etc.

    And yes, I was expecting to see characters and such. Well just straight packet data really. Can the above be converted using a String constructor? If so, is there anyway you could give me some guidance?

    Sorry to come across so naive :(

    Thanks
    Ben

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    What do you mean by "packet data"...that is what do you expect the format to look like. Whatever format that is is probably simply a representation of the bytes that have been transmitted.

    byte in Java is signed, so you're getting the values from -128 to 127.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Read the API doc for the String constructor and select the one that works for data in a byte array.

    Those bytes do NOT look like String. The 0's are NOT valid characters.
    Try writing a loop over the array and using the Integer.toHexString() method to display the bytes. hex displays are sometimes easier to read (for me anyways, I coded assembler for years).
    Also print in a loop casting each byte to a char and see it your recognize the data in that format. The 0's will be ? or small boxes because they aren't viewable chars

  20. #20
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Ok, In Wireshark when you capture packets I can see this: (An MSN message I sent to my flatmate:

    ***
    USR 13 gobbstoppers@hotmail.com;{EBF76F8C-6976-4737-B01A-4AE031A7E858} 1657847730.2015721.21165237
    CAL 10 gobbstoppers@hotmail.com
    CAL 11 spikeysam667@hotmail.com
    MSG 12 N 159
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    X-MMS-IM-Format: FN=Calibri; EF=; CO=800000; CS=0; PF=22
    Ok gotta test my capturing again. Sorry
    ***

    I was hoping for the packets I was capturing to be something like this.

    The premise of my project is to capture MSN packets and examine what's being said. (working from a rule based system). So I need to be able to see the raw packets.

    So do you think I then need to convert all this -128 to 127 back into regular text?

    Ben

Page 1 of 2 12 LastLast

Similar Threads

  1. null values
    By jabo in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 03:44 PM
  2. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 05:19 AM
  3. How to get null values stored in array
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 10:29 PM
  4. Printing default Swing values
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:09 AM
  5. how to read memory addresses
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 01:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •