Search code examples
socketsblackberryircblackberry-eclipse-plugin

What's wrong with this socket code in blackberry?


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

Solution

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