免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5047 | 回复: 9
打印 上一主题 下一主题

数组下标越界 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-23 16:37 |只看该作者 |倒序浏览
10可用积分
/*
        A basic extension of the java.applet.Applet class
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;


public class Test extends Applet { // implements TextListener {
        static private boolean isapplet = true;
        static private InetAddress arg_ip = null;
        static private int arg_port = 0;
    public tcpip gtp = null;;
        InetAddress reader_ip = null;
    //Image image;
    int port = 10001;
           public void init()
        {
                gtp = null;
                reader_ip = null;
                port = 10001;
        //image = getImage(getCodeBase(),"line2.jpg");
        }
    /*public void paint(Graphics g)
    {
    g.drawImage(image, 0, 0, this);
    }*/


        public void start()
        {
                setFont(new Font("Dialog",Font.BOLD,16));
                setLayout(null);
            setBackground(null);
                setSize(662,500);

                                                        /* Either get the IP address from the HTTP server if we're
                                                           an applet, or from the commandline (if passed). */
                if (isapplet) {
                        try{
                                reader_ip = InetAddress.getByName(getCodeBase().getHost());
                        }
                        catch (UnknownHostException e){}
                }
                else {
                        reader_ip = arg_ip;
                        if (arg_port != 0) {
                                port = arg_port;
                        }
                }
                                                        /* Open a socket to the device server's serial port */
                if (reader_ip != null) {
                        if (gtp == null) {
                                gtp = new tcpip(reader_ip, port);
                                if (gtp.s == null) {
                                        gtp = null;
                                        return;
                                }
                        }
                }
                if (gtp == null) {
                               
                        return;
                }
               
                                                /* You may now perform IO with the Device Server via
                                                 *                gtp.send(byte[] data_out);
                                                 *                byte[] data_in = gtp.receive();
                                                 * functions.
                                                 * In our example we'll use two TextBoxes which have been extended
                                                 * to handle IO to the device server.  Data typed in the upper
                                                 * text box will be sent to the Device Server, and data received
                                                 * will be displayed in the lower text box.
                                                */
        /* Start of custom application code */
                        Text_io a = new Text_io(gtp);
            a.run();
            Label label1 = new Label();
            label1.setText("UAB"+String.valueOf(a.data[0]));
            label1.setBounds(20, 480, 80, 15);
            add(label1,this);
            label1.setVisible(true);
             Label label2 = new Label();
            label2.setText("UCB"+String.valueOf(a.data[1]));
            label2.setBounds(100, 480, 80, 15);
            add(label2,this);
            label2.setVisible(true);
                  /* End of custom application code */
        }

        public void destroy()
        {
                if (gtp != null)
                        gtp.disconnect();
                gtp = null;
        }

        public void stop() {
        }

    public static void main(String[] args) {
                Frame frame = new Frame("TCP/IP Test");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
                if (args.length > 0) {
                        try{
                                arg_ip = InetAddress.getByName(args[0]);
                        }
                        catch (UnknownHostException e){}
                        if (args.length > 1) {
                                try {
                                        arg_port = Integer.valueOf(args[1]).intValue();
                                }
                                catch (NumberFormatException e) {}
                        }
                }
                Test ap = new Test();
                frame.add(ap);
                ap.init();
                isapplet = false;
                ap.start();
            frame.pack();
                frame.setVisible(true);
        }
}

最佳答案

查看完整内容

加时间延迟。

论坛徽章:
0
2 [报告]
发表于 2009-03-23 16:37 |只看该作者
原帖由 natoz 于 2009-3-24 12:45 发表
问题解决了,我把LABEL的setText放在了线程里,就能跟着线程走了;

很想知道楼上说的加点时间间隔是怎么做

加时间延迟。

论坛徽章:
0
3 [报告]
发表于 2009-03-23 16:39 |只看该作者
Text_io类如下:

import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class Text_io  implements Runnable{
    private tcpip gtp;
    String oldmessage = new String("");
          Thread timer;
     float data[]={0};
   
   
     
             public Text_io(tcpip tp) {
                gtp = tp;
                timer = new Thread(this);
             timer.start();
   
        }
        public void run() {
                int i;
                byte[] in;
            Thread me = Thread.currentThread();
            while (timer == me) {
                try {
                    Thread.currentThread().sleep(200);
                } catch (InterruptedException e) { }
                if ( (gtp != null) && ((i = gtp.available()) > 0) )
            {
                                in = gtp.receive();
                        //output_box.append(byte2HexStr(in));
                                                Getdata fr = new Getdata(in);
                       if(fr.Idata.length>1)
                       {
                           data = new float[fr.Idata.length];
                                   data = fr.Idata;
                         }
                                  }
            }
        }

论坛徽章:
0
4 [报告]
发表于 2009-03-23 16:39 |只看该作者
详细的错误信息?

论坛徽章:
0
5 [报告]
发表于 2009-03-23 17:13 |只看该作者
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at Test.start(Test.java:86)
        at Test.main(Test.java:126)


我补充一下,是否因为TEXT_io中的变量没有传送过来,或者线程内的对数组处理没成功?
我跟踪一下发现DATA还是等于{0};

[ 本帖最后由 natoz 于 2009-3-23 17:18 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-03-23 17:21 |只看该作者
看这段代码执行了吗?
                      if(fr.Idata.length>1)
                       {
                           data = new float[fr.Idata.length];
                                   data = fr.Idata;
                         }

你的初始data数组只有一个元素, 如果上面的代码没有执行的话,那你在start方法中用data[1]肯定会报错的!

论坛徽章:
0
7 [报告]
发表于 2009-03-24 08:57 |只看该作者
奇怪,我DEBUG程序,却能正确显示。
但运行时问题依旧。

楼上的朋友,谢谢回复,可以肯定的是线程内的语句没有执行完

论坛徽章:
0
8 [报告]
发表于 2009-03-24 10:15 |只看该作者
原帖由 natoz 于 2009-3-24 08:57 发表
奇怪,我DEBUG程序,却能正确显示。
但运行时问题依旧。

楼上的朋友,谢谢回复,可以肯定的是线程内的语句没有执行完

加点时间间隔就好了吧。调试的时候有时间间隔,运行的时候是连续的。

论坛徽章:
0
9 [报告]
发表于 2009-03-24 12:45 |只看该作者
问题解决了,我把LABEL的setText放在了线程里,就能跟着线程走了;

很想知道楼上说的加点时间间隔是怎么做

论坛徽章:
0
10 [报告]
发表于 2009-03-28 21:07 |只看该作者
原帖由 ziggler 于 2009-3-23 16:37 发表

加时间延迟。



谢谢,已散分
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP