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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:18:39+00:00 2026-06-10T02:18:39+00:00

I have an android app written in java. The app basically connects to a

  • 0

I have an android app written in java.

The app basically connects to a device which sends the app messages. The app waits for the messages to come in and then reports them, before processing each message.

I have a Connection class and a Listener class.

The Connection class is started via the constructor, which sets up the Listener class to listen for messages coming in from the device.

When a message comes in, the Listener sends the message to a method in the Connection class, reportMessage(). This is where the message is processed.

The Listener class is on a separate thread.

The code is shown below.

public class Connection 
{
    private String response;
    private String newResponse;

    private DataInputStream reader;

    private DataOutputStream writer; 

    private Socket socket;

    private boolean keepListening;

    private Listener listener;

    public Connection (String _ipAddress, int portNumber)
    {
        newResponse = "";

        try 
        {
            socket = new Socket(_ipAddress, _port);   //Connect to the server and its socket

            writer = new DataOutputStream(socket.getOutputStream()); //Connect to the server to receive and send messages

            reader = new DataInputStream(socket.getInputStream());

            listener = new Listener(reader, this); //Create a new listener for the client

            new Thread(listener).start(); //Set the listener off running
        } 
        catch (Exception e) 
        {
            ...
        } 
    }

    public synchronized void reportMessage(String message)
    {
        try
        {
            if("".equals(newResponse))
            {
                newResponse = new String();
            }

            newResponse = newResponse + message;

            System.out.println("Message Received: " + newResponse);

            processMessage(); //Process the message
        }
        catch (Exception e)
        {
            response = e.getCause().toString();
        }
    }
}

public class Listener implements Runnable 
{
    private DataInputStream reader = null;

    private boolean keepListening;

    private String serverMessage;

    private Connection connection;

    public Listener (DataInputStream inFromServer, Connection connection)
    {
        reader = inFromServer;
                               //Get the client connection's message transfer
        keepListening = true;

        this.connection = connection;
    }

    public void run()
    {
        while (keepListening)
        {
            try
            {
                if (reader.available() > 0)
                {
                    byte[] readInData = new byte[reader.available()];  //Initialise the array

                    reader.read (readInData);  //Read in the data

                    serverMessage = Utils.byteToString(readInData);

                    connection.reportMessage(serverMessage);   //Report the message
                }
            }
            catch (IOException e)
            {
                System.out.println("Error reading." + e.getLocalizedMessage().toString());

                keepListening = false;
            }

            keepListening = connection.getKeepListening();
        }
    }
}

This works well for a time, then sometimes I receive an error which crashes the program.

When running in debug mode, I get a NullPointerException thrown on whatever is the first line of reportMessage() in the Connection class.

The program is suspended on whatever line is the first line, whether it is a System.out.println or a line of actual code.

And the error doesn’t get caught by the try and catch. It crashes the program and there is no handling of the error even though it occurs in the try and catch.

This leads me to believe the error is not being thrown by anything in the reportMessage() method. If this is the case, then perhaps the error is being thrown in the Listener class .run().

However, I cannot see where any NullPointerException can be thrown from, as I have tried to make all the checks and when it is thrown, it is thrown when there are messages being sent.

The debugger says “An exception stack trace is not available”.

The LogCat says:

  08-21 10:35:57.894: E/AndroidRuntime(1118): FATAL EXCEPTION: Thread-12
  08-21 10:35:57.894: E/AndroidRuntime(1118): java.lang.NullPointerException
  08-21 10:35:57.894: E/AndroidRuntime(1118):atCom.que.wifiaudio.Connection.reportMessage(Connection.java:339)
  08-21 10:35:57.894: E/AndroidRuntime(1118):   at com.que.wifiaudio.Listener.reportIncomingMessage(Listener.java:93)
  08-21 10:35:57.894: E/AndroidRuntime(1118):   at com.que.wifiaudio.Listener.run(Listener.java:67)

  08-21 10:35:57.894: E/AndroidRuntime(1118):   at java.lang.Thread.run(Thread.java:1102)

Can anyone help me?

I need to know what is throwing the NullPointerException and how to stop it!

EDIT: Thanks to a lot of help, it now turns out it is the Exception I am catching and trying to get the cause of which is null. Still trying to work out where it is coming from though!

  • 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-10T02:18:40+00:00Added an answer on June 10, 2026 at 2:18 am

    My guess would be that an exception is thrown in processMessage, caught in the catch bellow it, but the getCause is null.

    1/ Try to log the exception before handling it

    2/ Look in processMessage, there should be an exception thrown.

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

Sidebar

Related Questions

I have an android app written in java. The app basically connects to a
I have written an android app which listens to the phone signal strength using
I have an android app, written in java that contains, amongst other things, a
I have an android app which has native code. The native code needs to
I have an android app which let the user to submit a form ,
I have written an Android application that uses SQLite database which is saved in
I've written an Android app which various abstract classes that perform useful functions. These
Newbie question. After I have written the Android App (easy part) I need to
I have an android app which asks a question followed by x number of
I have written an Android app (target 3.2) using Eclipse 3.7, I tried 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.