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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:30:10+00:00 2026-05-28T15:30:10+00:00

I have an app that connects to a remote chat server and then the

  • 0

I have an app that connects to a remote chat server and then the user is able to chat. I have a form where the user enters hostname and port and then another form where the user sees all messages and types new ones.

For the networking, it seems, I have no other choice but to use an AsyncTask. But there is something that I don’t know how to do. I would start the networking in the UI when onCreate() is called which is not a problem. But then when I have to wire the networking and the interface it becomes a mess. I have created two asynchronous tasks – one for sending and one for receiving but yet it doesn’t work for me. Here is the code where I first start the SenderTask which is supposed to start communicating by calling the receiver task and if a button shall be pressed a new message is sent:

public class Second extends Activity {

    private SenderTask snd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        // ...

        final Button sendButton = (Button) findViewById(R.id.button2);

        snd = new SenderTask();
        snd.doInBackground(editTexts);

        sendButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // when the button is clicked the next screen is loaded
                snd.onProgressUpdate(true);
            }
        });

    }   // end of onCreate

}

Then here is the SenderTask class:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

import android.os.AsyncTask;
import android.widget.EditText;

public class SenderTask extends AsyncTask<EditText, Boolean, Void> {

    MulticastSocket so;
    InetAddress serverAddress;
    int port;
    EditText eText1;
    EditText eText2;
    EditText eText3;
    EditText messageBoard;

    @Override
    protected Void doInBackground(EditText... eTexts) {
        eText1 = eTexts[0];
        eText2 = eTexts[1];
        eText3 = eTexts[2];
        messageBoard = eTexts[3];
        setUp();

        return null;
    }

    private void setUp() {
        // convert the host name to InetAddress
        try {
            //serverAddress = InetAddress.getByName(eText1.getText().toString());
            serverAddress = InetAddress.getByName("my server name is here");
        } catch (Exception e) {}

        //convert the port to an int
        //port = Integer.parseInt(eText2.getText().toString());
        port = 4456;

        // create socket and start communicating
        try {
            so = new MulticastSocket(port);
            so.joinGroup(serverAddress);
        } catch (IOException e) {}


        // Start listening should be here
        ReceiverTask rec = new ReceiverTask();
        rec.doInBackground(messageBoard);
        rec.onProgressUpdate(so);
    }

    private void sendMessage() {

        // get the text that they contain and add the new messages to the old ones
        String message = eText3.getText().toString();
        String conversation = messageBoard.getText().toString();
        String newConverstion = conversation.concat("\n[You] ").concat(message);

        // make the messages text view editable
        messageBoard.setFocusable(true);
        messageBoard.setText(newConverstion);   // add the new message to the text view
        messageBoard.setFocusable(false);   // make the messages text view not editable

        // erase the text on the second text view that has just been sent
        eText3.setText("");

        // Send message to server

        // convert message to bytes array
        byte[] data = (message).getBytes();

        // create and send a datagram
        DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, port);

        try {
            so.send(packet);
        } catch (IOException e) {}


    }   // end of sendMessage

    protected void onProgressUpdate(boolean... go) {
        if (go.equals(new Boolean(true))) {
            sendMessage();
        }

    }

}

here is the ReceiverTask class:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;

import android.os.AsyncTask;
import android.widget.EditText;

public class ReceiverTask extends AsyncTask<EditText, MulticastSocket, Void> {

    MulticastSocket so;
    EditText messageBoard;

    @Override
    protected Void doInBackground(EditText... messBo) {
        messageBoard = messBo[0];
        return null;
    }

    @Override
    protected void onProgressUpdate(MulticastSocket... socket) {
        so = socket[0];

        byte[] data = new byte[1024];   // received data container

        while (true) {
            try {
                // create a datagram for receivin
                DatagramPacket packet = new DatagramPacket(data, data.length);

                // wait for the next message
                so.receive(packet);

                String message = new String(data, 0, packet.getLength());

                // add the new messages to the old ones
                String conversation = messageBoard.getText().toString();
                String newConverstion = conversation.concat("\n[Remote] ").concat(message);

                // make the messages text view editable
                messageBoard.setFocusable(true);
                messageBoard.setText(newConverstion);   // add the new message to the text view
                messageBoard.setFocusable(false);   // make the messages text view not editable


            } catch (IOException ioe) {}
        }
    }

}

The error is:

01-25 14:26:09.739: E/AndroidRuntime(661): FATAL EXCEPTION: main
01-25 14:26:09.739: E/AndroidRuntime(661): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.regeduseroox.projx/com.regeduseroox.projx.Second}: java.lang.NullPointerException
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.os.Looper.loop(Looper.java:137)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-25 14:26:09.739: E/AndroidRuntime(661):  at java.lang.reflect.Method.invokeNative(Native Method)
01-25 14:26:09.739: E/AndroidRuntime(661):  at java.lang.reflect.Method.invoke(Method.java:511)
01-25 14:26:09.739: E/AndroidRuntime(661):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-25 14:26:09.739: E/AndroidRuntime(661):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-25 14:26:09.739: E/AndroidRuntime(661):  at dalvik.system.NativeStart.main(Native Method)
01-25 14:26:09.739: E/AndroidRuntime(661): Caused by: java.lang.NullPointerException
01-25 14:26:09.739: E/AndroidRuntime(661):  at com.regeduseroox.projx.ReceiverTask.onProgressUpdate(ReceiverTask.java:33)
01-25 14:26:09.739: E/AndroidRuntime(661):  at com.regeduseroox.projx.SenderTask.setUp(SenderTask.java:53)
01-25 14:26:09.739: E/AndroidRuntime(661):  at com.regeduseroox.projx.SenderTask.doInBackground(SenderTask.java:27)
01-25 14:26:09.739: E/AndroidRuntime(661):  at com.regeduseroox.projx.Second.onCreate(Second.java:38)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.Activity.performCreate(Activity.java:4465)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-25 14:26:09.739: E/AndroidRuntime(661):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-25 14:26:09.739: E/AndroidRuntime(661):  ... 11 more

ReceiverTask.java:33 is so.receive(packet);

SenderTask.java:53 is rec.onProgressUpdate(so);

SenderTask.java:27 is setUp();

Second.java:38 is snd.doInBackground(editTexts);

I don’t know what is wrong now and I really don’t have anymore ideas how to solve this. Any help will be much appreciated.

  • 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-28T15:30:11+00:00Added an answer on May 28, 2026 at 3:30 pm

    Replace

    snd.doInBackground(editTexts);
    

    with

    snd.execute(editTexts);
    

    EDIT

    You should not be directly calling onProgressUpdate() or doInBackground() to initiate an AsyncTask, only execute(). Furthermore, onProgressUpdate() does its work on the UI thread, which is really only intended to be used to update a ProgressBar. You should move all of the work from onProgressUpdate() to doInBackground(), and return the data in onPostExecute().

    My suggestion is to create a custom class that includes the String (conversation) and MulticastSocket (so) as its fields. Send this object in your AsyncTask.execute() call, and retrieve from the varargs.

    Eg:

    public class MyObject {
         MulticastSocket so;
         EditText messageBoard;
    
         //... Getters and Setters omitted for brevity
    }
    

    Then in your SenderTask:

    ReceiverTask rec = new ReceiverTask();
    MyObject obj = new MyObject(so, messageBoard);
    rec.execute(obj);
    

    You should use this same idea for your Second/SenderTask (take the work out of onProgressUpdate()).

    Let me know if this is all clear, and be sure to re-read the docs on the proper usage of AsyncTask.

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

Sidebar

Related Questions

I have an app in java that is nothing but a remote server.The remote
I have a ASP.NET application that connects to a SQL server backend on another
I have a java swing App that connects to a remote Unix box using
I have Flex/AIR app that connects to a tomcat server via BlazeDS. I'm not
I have a console app that needs to connect to a remote sql server
I have a Linux/c client app that connects to a WCF web service over
I have a mobile app webservice client that connects to a WCF webservice(on my
If I have in my php app an object that connects to the database,
I have an android app, that connectos to the remote database with PHP+json but
I have Java code that connects to an SQL server DB but I cannot

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.