抛出的NullPointerException Java中的synchronized方法使用套接字 [英] NullPointerException thrown in java synchronised method using sockets

查看:588
本文介绍了抛出的NullPointerException Java中的synchronized方法使用套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写的一个Android应用程序。

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.

我有一个的连接类和监听器类。

连接类是通过构造函数,它建立在监听器类侦听来自设备的消息中展开。

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

发来的邮件中,监听器将消息发送到在Connection类中的方法, reportMessage()。这是处理该消息。

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.

在code如下所示。

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.

当在调试模式下运行时,我得到的 NullPointerException异常扔到什么是的 reportMessage()连接类中的第一道防线。

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

该计划暂停任何线是第一线,无论是的的System.out.println 或实际code的一条线。

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

和错误没有得到由try和catch捕获。这导致程序崩溃,并且没有处理,即使在try和catch它发生错误。

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.

这使我相信错误是不被在 reportMessage()方式什么异常。如果是这样的话,那么也许错误被抛出的Listener类的 .RUN()

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().

不过,我看不出任何的 NullPointerException异常可以从被抛出,因为我试图让所有的检查,当它被抛出,它是当有被发送的消息抛出。

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".

在LogCat中说:

  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)

谁能帮我?

我需要知道什么是抛NullPointerException异常,以及如何阻止它!

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

编辑:感谢很多帮助,现在事实证明这是我追赶并试图获得的原因,其中为空的异常。仍试图找出它是从哪里过来,虽然!

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!

推荐答案

我的猜测是,一个例外是抛出而processMessage,夹在抓波纹管,但的getCause为null。

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

1 / TRY处理之前登录异常

1/ Try to log the exception before handling it

2 /查找范围而processMessage,应该有一个的异常。

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

这篇关于抛出的NullPointerException Java中的synchronized方法使用套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆