2011-07-30 5 views
0

:これは私のコードのNullPointerExceptionですが、私はエラーを追跡できませんでした:ブラックベリーでこのソケットコードの何が問題になっていますか?私がキャッチされないJVMエラー104を取得してい

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

は次のようになめらかにあなたfinallyブロックのコードを変更してください:

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

try {    
    if (connection != null) connection.close(); 
} catch (IOException ioe) {} 
関連する問題