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 9099839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:43:10+00:00 2026-06-17T00:43:10+00:00

Im trying to send a message using an object. I have a bit of

  • 0

Im trying to send a message using an object. I have a bit of code that create a GUI and another chunk of code that acts either creates a server if there isnt a connection or acts as a client if a server is available. The User types in a message through the GUI and sends it via the “communicator” as a “message” object. The user is able to send messages if the communicator is setup as a client but not if its set up as a server…

If the user tries to send a message with the program set up as a server I get the following error.

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

And the message doesn’t send

This is my code for the communicator, At the bottom is the function that is supposed to send the message

package hunterinstant;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Chris
 */
public class HunterCom implements Runnable {

    private static String SERVER_IP = "127.0.0.1";
    private static int SERVER_PORT = 5000;
    Scanner scanner = new Scanner(System.in);   //to read text from the console
    Socket socket = null;
    boolean client;
    Conversation window;
    ObjectOutputStream out = null;

    public void initialize(Conversation c, String ip, String port) {
        System.out.println(ip);
        System.out.println(port);
        SERVER_IP = ip;
        SERVER_PORT = Integer.parseInt(port);
        window = c; 
    }



    public void run() {

        try {
            socket = new Socket(SERVER_IP, SERVER_PORT);
            window.addText("Connected to server");
            client = true;
        } catch (Exception ex) {
            window.addText("No Users Online: Waiting for connection");
            client = false;

        }

        if (client == true) {

        ObjectInputStream in = null;
        while (true) {
            try {
                if (out == null) {
                    out = new ObjectOutputStream(socket.getOutputStream());
                }

                //get the reply from the server
                if (in == null) {
                    in = new ObjectInputStream(socket.getInputStream());
                }
                Message message = (Message) in.readObject();

                window.addText(message.getMessage());
                //System.out.println("Server said: " + message.getMessage());

            } catch (Exception ex) {
                window.addText("Error: " + ex);
            }
        }

    } // End Client

        else {
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(SERVER_PORT);
            } catch (IOException ex) {
                window.addText("Error occured while creating the server socket");
                return;
            }

            Socket socket = null;
            try {
                //Waits untill a connection is made, and returns that socket
                socket = serverSocket.accept();
            } catch (IOException ex) {
                window.addText("Error occured while accepting the socket");
                return;
            }
            window.addText("Connection created, client IP: " + socket.getInetAddress());
            ObjectInputStream in = null;
            while (true) {
                try {
                    if (in == null) {
                        in = new ObjectInputStream(socket.getInputStream());
                    }

                    Message message = (Message) in.readObject();

                    window.addText(message.getMessage());
                    if (out == null) {
                        out = new ObjectOutputStream(socket.getOutputStream());
                    }
                } catch (Exception ex) {
                    window.addText("Error: " + ex);
                }
            }
        }
    }

//THIS IS THE CODE THAT IS SUPPOSED TO SEND THE MESSAGE
    public void sendMessage(String str) 
    {
        try {
            out.writeObject(new Message(str));
            System.out.println("Hello");
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(HunterCom.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

This is the code that is supposed to send the message from the GUI

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    comunicator.sendMessage(jTextField1.getText());
    jTextArea2.append(jTextField1.getText());
}

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
    if(evt.getKeyCode()==10) // Has the user pressed enter?
    {
        comunicator.sendMessage(jTextField1.getText());
        jTextArea2.append(jTextField1.getText());
    }
}

Sorry for what I’m sure is a noobish question… I’m sure I’m doing something really obviously stupid here but I have only been doing java for a couple of months and I have spent hours trying to figure this one out with no joy….

  • 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-06-17T00:43:12+00:00Added an answer on June 17, 2026 at 12:43 am

    Hours? Oh, my.

    Start by reading the stack trace. It tells you the line number and the .java source file at which the NPE occurred. Go to that line, look at the object references, and see which one is null.

    NPE is easy to fix. It usually happens because you’ve made erroneous assumptions about object initialization. You’ll just have to figure out how to initialize that reference properly. Scope, constructor – you’ll find the problem.

    Get a decent IDE, like IntelliJ, and step through the code with a debugger. It’ll take ten minutes tops, not hours.

    Personally, I’m unwilling to debug lots of code for someone. Yours isn’t complete, so it can’t be compiled and run. You didn’t paste the stack trace in your question, so I can’t read it for you. You’ve done little to help us help you.

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

Sidebar

Related Questions

I'm simply trying to send a message to another object but its not exactly
I am trying to send a message with attachment using CDO object. When the
I am trying to send a message, using MailCore framework in iOS. This is
I'm trying to send post using API feed. I set fields: message , link
I'm using the CDO.Message class to create and send an email from a Visual
I have a Python application (with GUI, using PyQt4) that gets spawned by the
I'd appreciate any help on this... I have an Object I'm trying to send
I´m trying to send an email through gmail using this code: MailMessage msg =
I have a PHP page that uses PEAR Mail to send a message through
I have been trying to send a big attachment (9 MB) using C# ASP.NET

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.