我在尝试连接Web服务器时遇到异常,我该如何解决?

时间:2015-08-02 21:03:31

标签: java android android-studio

在MainActivity.java的顶部,我添加了两个数组:

private final String[] ipaddresses = new String[2];
private final Integer[] ipports = new Integer[2];

然后在onCreate中我做了:

    ipaddresses[0] = "10.0.0.2";
    ipaddresses[1] = "1.0.0.5";
    ipports[0] = 8098;
    ipports[1] = 8088;
    addListenerOnButton();

在addListenerOnButton中我做了:

    public void addListenerOnButton() {
    btnClick = (Button) findViewById(R.id.checkipbutton);
    btnClick.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {

                text = (TextView) findViewById(R.id.textView2);
                boolean canConnect = true;
                try {
                    Socket s = new Socket();
                    s.connect( new InetSocketAddress(ipaddresses[0], ipports[0]) );
                    s.close();
                    text.setText("Connection Success");
                } catch (IOException e) {
                    canConnect = false;
                    text.setText("Connection Failed");
                    e.printStackTrace();
                }
        }
    });
}

在线上使用断点时:

s.connect( new InetSocketAddress(ipaddresses[0], ipports[0]) );

当它在此行停止然后我点击F11时,我会在变量窗口中以红色获得异常:

方法抛出' java.lang.NullPointerException'例外。无法评估android.system.StructAddrinfo.toString()

0 个答案:

没有答案
相关问题