Android Null指针异常线程问题

时间:2012-01-10 03:00:02

标签: android nullpointerexception

嘿伙计们我在Android中运行此代码:

public ServiceServer(String serverName) {
    this.serverName = serverName;
    this.serverThread = new Thread() {
        public void run() {
            serverThreadProc();
        }
    };
}

private void serverThreadProc() {
    while (!this.stopRequested) {
        Socket sessionSocket = null;
        try {
            sessionSocket = this.serverSocket.accept();
            InetAddress sourceAddress = sessionSocket.getInetAddress();
            Log.i(LOG_TAG, "Accepted new incoming connection ... "
                    + sourceAddress.toString());
            if (clientMap.containsKey(sourceAddress)) {
                ClientProcessor legacyProcessor = clientMap
                        .get(sourceAddress);
                legacyProcessor.shutdown();
            }
            ClientProcessor processor = new ClientProcessor(sessionSocket);
            processor.start();
            clientMap.put(sourceAddress, processor);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public int start() {

    try {
        this.serverSocket = new ServerSocket(0);
        this.serverPort = this.serverSocket.getLocalPort();
        Log.i(LOG_TAG, "Server " + serverName + " started at "
                + this.serverSocket.getLocalSocketAddress());
    } catch (IOException ex) {
        Log.i(LOG_TAG, ex.toString());
    }

    this.serverThread.start();

    return this.serverPort;
}

public void stop() {
    try {
        stopRequested = true;
        if (null != this.serverSocket) {
            this.serverSocket.close();
        }
    } catch (IOException ex) {
        Log.i(LOG_TAG, ex.toString());
    }

    Log.i(LOG_TAG, "The server " + this.serverName + " has been stopped.");
}

我得到一个与我的线程有关的空指针异常错误?由于某种原因,它会挂在serverThreadProc()方法上。这是我从问题中得到的堆栈跟踪:

01-10 02:10:13.361: I/ActivityThread(2216): Pub com.whooznear.android: com.whooznear.android.ProfileProvider
01-10 02:10:13.642: D/dalvikvm(2216): GC_FOR_ALLOC freed 44K, 4% free 6336K/6595K, paused 95ms
01-10 02:10:13.662: I/dalvikvm-heap(2216): Grow heap (frag case) to 6.805MB for 588816-byte allocation
01-10 02:10:13.801: D/dalvikvm(2216): GC_FOR_ALLOC freed 8K, 4% free 6903K/7175K, paused 109ms
01-10 02:10:14.051: D/dalvikvm(2216): GC_CONCURRENT freed <1K, 4% free 6903K/7175K, paused 8ms+15ms
01-10 02:10:17.462: D/dalvikvm(2216): GC_FOR_ALLOC freed 603K, 11% free 6666K/7431K, paused 81ms
01-10 02:10:17.622: D/dalvikvm(2216): GC_CONCURRENT freed <1K, 4% free 7167K/7431K, paused 5ms+13ms
01-10 02:10:17.971: W/Profile(2216): Invalid profile, java.lang.ArrayIndexOutOfBoundsException: index=1 length=1
01-10 02:10:31.801: I/ConnectActivity(2216): IP Address:0
01-10 02:10:31.812: I/ServiceServer(2216): java.net.SocketException: Permission denied
01-10 02:10:31.812: I/ConnectActivity(2216): Started at -1
01-10 02:10:31.861: W/dalvikvm(2216): threadid=9: thread exiting with uncaught     exception (group=0x40014760)
01-10 02:10:31.861: E/AndroidRuntime(2216): FATAL EXCEPTION: Thread-12
01-10 02:10:31.861: E/AndroidRuntime(2216): java.lang.NullPointerException
01-10 02:10:31.861: E/AndroidRuntime(2216):     at com.whooznear.android.ServiceServer.serverThreadProc(ServiceServer.java:61)
01-10 02:10:31.861: E/AndroidRuntime(2216):     at com.whooznear.android.ServiceServer.access$0(ServiceServer.java:57)
01-10 02:10:31.861: E/AndroidRuntime(2216):     at com.whooznear.android.ServiceServer$1.run(ServiceServer.java:52)
01-10 02:10:32.301: D/dalvikvm(2216): GC_CONCURRENT freed 397K, 7% free 7187K/7687K, paused 9ms+11ms
01-10 02:10:34.761: I/Process(2216): Sending signal. PID: 2216 SIG: 9

2 个答案:

答案 0 :(得分:0)

您在ServiceServer.java的第61行遇到NullPointerException 您正在进行呼叫的对象之一似乎是Null

答案 1 :(得分:0)

  

显示java.lang.NullPointerException:   com.whooznear.android.ServiceServer.serverThreadProc(ServiceServer.java:61)

检查ServiceServer

类中的第61行

ps:这个问题,我不建议发布在这里,所有日志错误信息,你可以检查并找到错误的地方。