黑莓中的这个套接字代码出了什么问题?

时间:2011-07-30 04:32:05

标签: sockets blackberry irc blackberry-eclipse-plugin

我得到一个JVM错误104,Uncaught:NullPointerException,这是我的代码,我无法跟踪错误:

package com.rim.samples.device.socketdemo;

import java.io.*;
import javax.microedition.io.*;
import net.rim.device.api.ui.*;

public class ConnectThread extends Thread
{   
    //private InputStream _in;
    private OutputStreamWriter _out;        
    private SocketDemoScreen _screen;   

    // Constructor
    public ConnectThread()
    {
        _screen = ((SocketDemo)UiApplication.getUiApplication()).getScreen(); 
    }

    public void run()
    {
        StreamConnection connection = null;       
        String user = "Cegooow";
        String channel = "#oi";
        try
        {
            _screen.updateDisplay("Opening Connection...");
            String url = "socket://" + _screen.getHostFieldText() + ":6667" + (_screen.isDirectTCP() ? ";deviceside=true" : "");                                    
            connection = (StreamConnection)Connector.open(url);
            _screen.updateDisplay("Connection open");

            //_in = connection.openInputStream();

            _out = new OutputStreamWriter(connection.openOutputStream());            
            //StringBuffer s = new StringBuffer();

            _out.write("NICK " + _screen.getNickText() + "\r\n");
            _out.write("USER " + user + "8 * : Client\r\n");
            _out.write("JOIN " + channel + "\r\n");
            _out.write("PRIVMSG " + channel + " " + _screen.getMessageFieldText() + "\r\n");  
            _screen.updateDisplay("Done!");
        }
        catch(IOException e)
        {
            System.err.println(e.toString());
        }
        finally
        {              
            _screen.setThreadRunning(false);

            /*try
            {               
                _in.close();             
            }
            catch(IOException ioe)
            {                
            }*/
            try
            {       
                _out.close();             
            }
            catch(IOException ioe)
            {               
            }
            try
            {               
                connection.close();
            }
            catch(IOException ioe)
            {                
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试将finally块的代码改为smth,如下所示:

try {       
    if (_out != null) _out.close();
} catch (IOException ioe) {}

try {               
    if (connection != null) connection.close();
} catch (IOException ioe) {}