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

发布于 2024-11-27 08:02:02 字数 2274 浏览 2 评论 0原文

我收到 JVM 错误 104,未捕获: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)
            {                
            }
        }
    }
}

i'm getting a JVM Error 104, Uncaught: NullPointerException, this is my code, I couldn't track errors:

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)
            {                
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-12-04 08:02:03

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

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

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

Try to change the code of your finally block to smth like this:

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

try {               
    if (connection != null) connection.close();
} catch (IOException ioe) {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文