java.lang.ArrayIndexOutOfBoundsException: 1错误

amberln 2005-01-06 10:10:38
下面程序实现一个从文件中读取信息插如数据库的功能.
单独测试插入数据库没有问题,但是从文件中读取时老报java.lang.ArrayIndexOutOfBoundsException: 1的错误.
public class TransferMgr
{

/**
* 空构造函数
*/
public TransferMgr()
{

}

public void UpdateTransfer()
{
BufferedReader reader;
TransferInfoDaoMysql transferInfoDaoMysql=new TransferInfoDaoMysql();
try
{
reader = new BufferedReader(new FileReader("conf/transfer.log"));
String line;

while((line=reader.readLine())!=null)
{

TransferInfo info=new TransferInfo(line);//这行出错.调用的下面的类.

transferInfoDaoMysql.transInsert(info);

}

System.out.println("quit while");
reader.close();


}
catch (IOException e)
{
e.printStackTrace();
}
catch (DAORuntimeException ex)
{
ex.printStackTrace();
}
}

}

/**
* 用字符串初始化一条传输记录
* @param transfer
*/
public TransferInfo(String transfer)
{
String[] files=new String[3];
files=transfer.split(" ");
this.fileName=files[0];
System.out.println(files[1]);
this.Size=Long.parseLong(files[1]);
this.transfer_Time=files[2];
String[] file=fileName.split("_");
this.CacheID=file[0];
this.ChannelID=file[1];
this.date=file[2];
}
...全文
6014 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
amberln 2005-01-06
  • 打赏
  • 举报
回复
to redex(cc)
我怎样写给他分配内存呢?谢谢.
xitianjile 2005-01-06
  • 打赏
  • 举报
回复
public TransferInfo(String transfer)
{
String[] files=new String[3];
files=transfer.split(" "); //最好改一下..这样不好..
System.out.println("数组1:"+files.length);
this.fileName=files[0];
System.out.println(files[1]);//按你的写法,这里是没错的.
this.Size=Long.parseLong(files[1]);
this.transfer_Time=files[2];
String[] file=fileName.split("_");
//多加1句.
System.out.println("数组2:"+file.length);
this.CacheID=file[0];
this.ChannelID=file[1];//也有可能在这里出错!
this.date=file[2];
}
xitianjile 2005-01-06
  • 打赏
  • 举报
回复
System.out.println(files[1]);
错误出现在这一行...

this.fileName=files[0];之前加入一句看看查看一下数组的长度

System.out.println(files.length);
redex 2005-01-06
  • 打赏
  • 举报
回复
String[] file=fileName.split("_");

file这个字符串数组没有分配内存.
csdnyong 2005-01-06
  • 打赏
  • 举报
回复
调试一下就ok了
amberln 2005-01-06
  • 打赏
  • 举报
回复
to hxzg001:原来就是那样写的,出同样的问题,我才改成先new的情况,也出同样的问题.
hxzg001 2005-01-06
  • 打赏
  • 举报
回复
...
String[] files=new String[3];
files=transfer.split(" ");
...
应该在这里出错,split出的数组元素个数超过了files的元素个数,改成
String[] files = transfer.split(" ");
试试
funcreal 2005-01-06
  • 打赏
  • 举报
回复
ArrayIndexOutOfBoundsException
数组下标越界。
单步跟踪你的数组
dvictor 2005-01-06
  • 打赏
  • 举报
回复
数组越界

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧