Android套接字连接空指针异常

时间:2013-04-23 14:03:20

标签: android sockets nullpointerexception connection

我的套接字连接有问题。它表示此行中有一个null pionter异常:

Socket client = new Socket(ip,8000);

但是Ip没关系,端口应该没问题!

以下是完整代码:

public class socket { 

public static void out(String out, Context context){

    final String PREFS_NAME = "SAVEDIP";
    final String PREF_IP = "";
    String ip = null;
    SharedPreferences pref = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    ip = pref.getString(PREF_IP, "");
    Log.e("IP", ip);


    try {
        Socket client = new Socket(ip,8000);    
        //PrintWriter output = new PrintWriter(client.getOutputStream(), true);
        //BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        //output.println(out);
        Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
        //output.close();
        //in.close();
        client.close();
    } catch (UnknownHostException e) {
        Log.e("TAG", "UnknowHostExeption");
        e.printStackTrace();
    } 
    catch (IOException e) {
        Log.e("TAG", "IOExeption");
        e.printStackTrace();
    }
}

}

我传递上下文和文本:

c = getApplicationContext();
socket.out("exit", c);

我得到的错误如下:

    04-23 15:58:30.577: E/TAG(26540): IOExeption
04-23 15:58:30.577: W/System.err(26540): java.net.SocketException: The operation timed out
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:219)
04-23 15:58:30.577: W/System.err(26540):    at java.net.Socket.startupSocket(Socket.java:781)
04-23 15:58:30.577: W/System.err(26540):    at java.net.Socket.tryAllAddresses(Socket.java:194)
04-23 15:58:30.577: W/System.err(26540):    at java.net.Socket.<init>(Socket.java:258)
04-23 15:58:30.587: W/System.err(26540):    at java.net.Socket.<init>(Socket.java:222)
04-23 15:58:30.587: W/System.err(26540):    at com.example.andremote.socket.out(socket.java:28)
04-23 15:58:30.587: W/System.err(26540):    at com.example.andremote.Remote$11.onClick(Remote.java:175)
04-23 15:58:30.587: W/System.err(26540):    at android.view.View.performClick(View.java:2408)
04-23 15:58:30.597: W/System.err(26540):    at android.view.View$PerformClick.run(View.java:8817)
04-23 15:58:30.597: W/System.err(26540):    at android.os.Handler.handleCallback(Handler.java:587)
04-23 15:58:30.597: W/System.err(26540):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 15:58:30.597: W/System.err(26540):    at android.os.Looper.loop(Looper.java:143)
04-23 15:58:30.597: W/System.err(26540):    at android.app.ActivityThread.main(ActivityThread.java:4914)
04-23 15:58:30.607: W/System.err(26540):    at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:58:30.607: W/System.err(26540):    at java.lang.reflect.Method.invoke(Method.java:521)
04-23 15:58:30.607: W/System.err(26540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-23 15:58:30.607: W/System.err(26540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-23 15:58:30.607: W/System.err(26540):    at dalvik.system.NativeStart.main(Native Method)
请告诉我哪里弄错了,我找不到solotiun!

1 个答案:

答案 0 :(得分:0)

显然在这一行

ip = pref.getString(PREF_IP, "");

在右侧返回null,因此您的ip变量仍然为null或无效值,因此当您将其作为参数传递给Socket构造函数时,因为您的ip变量(主机名)无效,它会产生IOException。尝试检查你的ip变量是否为null或者在将它传递给Socket构造函数之前。