Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6712141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:13:28+00:00 2026-05-26T08:13:28+00:00

This problem is driving me up the wall. This is for a very simple

  • 0

This problem is driving me up the wall. This is for a very simple online multiplayer game that I am currently working on.

I am currently able to send packets via udp to my client(s), and they seem to receive them fine. However, when I send a serialized object to my client and deserialize at the other end, I’m getting NullPointerExceptions when I try to access the values I need. I have verified that the object is being correctly serialized on the server side (deserialized it and checked the data), so I am 99% sure I am doing something very wrong with my code for sending the packet.

Here is the code for serializing and sending the “Datagram” object from the server:

    DatagramPacket sendPacket = null;

    byte[] buf = null;

    //Serialize the datagram object to send as a UDP packet
    try {

        // Serialize to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos);   
        out.writeObject(data);
        buf = bos.toByteArray(); 

        out.close();
        bos.close();

    } catch (IOException e) {
    }

    try {
        sendPacket = new DatagramPacket( buf, buf.length,
                InetAddress.getLocalHost(), 4004);
    } catch (UnknownHostException e){}

    try {
        DatagramSocket sendSocket = new DatagramSocket();
        sendSocket.send( sendPacket );
        changed = true;
    }catch (IOException e) {}

The “data” object being serialized is full of correct values; I am sure of this.

The other relevant code block is the receiving block on the client side:

public Datagram readDatagram() {

    byte[] buff = new byte[20000];
    DatagramPacket packet = new DatagramPacket(buff, buff.length);
    DatagramSocket receiver = null;

    try {
        receiver = new DatagramSocket(4004);
        receiver.receive(packet);
    } catch (IOException e) {
        System.out.println("ERROR2");
    }

    Datagram data = null;// = new Datagram();

    try {
        // Deserialize from a byte array
        ByteArrayInputStream bis = new ByteArrayInputStream(buff);
        ObjectInput in = new ObjectInputStream(bis);
        data = (Datagram) in.readObject();

        bis.close();
        in.close();
    } catch (ClassNotFoundException e) {
    } catch (IOException e) {
        System.out.println("ERROR3");
    }

    for (int i = 0; i < 35; i++) {
        System.out.print(data.getLevel()[i]);
    }

    receiver.close();

    return data;

}

When I attempt to read any values after this deserialization, I get the NullPointerException. If someone can point me in the right direction I will be extremely happy.

Oh, and I am sending to localHost right now intentionally just to test things out. My client and server are both running on my machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:13:29+00:00Added an answer on May 26, 2026 at 8:13 am

    On both the sending and receiving ends, you are catching and squashing exceptions. There is a good chance that this is hiding evidence that would help you diagnose the problem. Even if this is not the case, squashing exceptions like that is dangerous practice.

    My bet is that a ClassNotFoundException is being thrown in the receiving end code. This would leave you with data == null, and that would then lead to an NPE in the following loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this is an embarassingly simple-looking problem, but is driving me crazy. I'm learning
I have this problem that is driving me insane. I have a project to
This seems like it should be simple but it's driving me up the wall.
Very simple I'm sure, but driving me up the wall! There is a component
I am working on this problem for two days and this is driving me
I've been having this problem for a while and it's driving me nuts. I'm
This problem pertains to Java By using RandomAccessFile I intend to be able to
This is driving me up the wall. I have checked and rechecked spelling and
This has been driving me nuts all day. I've seen this problem crop up
This problem is driving me crazy, I'm sure I'm missing something. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.