社区
Web 开发
帖子详情
急急...送分。。关于JSP的错误:java.lang.StringIndexOutOfBoundsException: String index
BlankMe
2003-05-05 01:10:00
在一个FOR循环中,我至少调用了十多个String类型,运行JSP程序有的时候可以执行,
有时出现错误:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
怎样释放String?? 怎样解决这个错误??
...全文
126
7
打赏
收藏
急急...送分。。关于JSP的错误:java.lang.StringIndexOutOfBoundsException: String index
在一个FOR循环中,我至少调用了十多个String类型,运行JSP程序有的时候可以执行, 有时出现错误: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 怎样释放String?? 怎样解决这个错误??
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
shidu
2003-05-05
打赏
举报
回复
我认为可能出现问题的地方:
sender=senderAddr.substring(start+1, end);
或者是数组越界了。
shidu
2003-05-05
打赏
举报
回复
调用String的某些方法时候和长度有问题。
BlankMe
2003-05-05
打赏
举报
回复
for(int i=0; i<messages.length; i++)
{
int InboxID=i+InboxMsgTotalNum+1; //要存储的邮件在INBOX的位置
javax.mail.Address[] addr=messages[i].getFrom();
String senderAddr=new String();
senderAddr=addr[0].toString();
senderAddr.trim();
int start=senderAddr.indexOf("<");
int end=senderAddr.indexOf(">");
String sender=new String();
sender=senderAddr.substring(start+1, end); // 获得发送者地址
String subject=new String();
subject=messages[i].getSubject();
Object MsgContent = messages[i].getContent();
MsgContent = messages[i].getContent(); //怎样存储到数据库中?????
//邮件的内容出错:javax.mail.internet.MimeMultipart@4d2197
java.text.Format formatter = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm");
String sendtime=new String();
sendtime=formatter.format(messages[i].getSentDate());
out.println(sendtime);
int size=messages[i].getSize();
//执行数据库插入操作
String ConnInsert =new String();
ConnInsert = "jdbc:odbc:webmail"; //数据源
Connection connectInsert =null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.println( e.getMessage());
}
try {
//连接数据库
connectInsert = DriverManager.getConnection(ConnInsert,logName,passwd);
Statement stmtInsert = connectInsert.createStatement();
String sqlInsert=new String();
//sqlInsert="insert into INBOX(ID,userID,sender,subject,size)";
//sqlInsert+=" values('"+InboxID+"','"+userID+"','"+sender+"','"+subject+"','"+size+"')";
sqlInsert="insert into INBOX(ID,userID,sender,subject,sendtime,content,size)";
sqlInsert+=" values('"+InboxID+"','"+userID+"','"+sender+"','"+subject+"','"+sendtime+"','"+contents+"','"+size+"')";
stmtInsert.executeUpdate(sqlInsert);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
/****************** 判断是否附件 ************************/
if(messages[i].isMimeType("multipart/*")) //附件
{
FetchProfile fp=new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(messages,fp);
Object content=messages[i].getContent();
Multipart multipart = (Multipart)content;
for (int c=0, n=multipart.getCount(); c<n; c++)
{
Part part = multipart.getBodyPart(c);
//String msg=getStr(part.getContent());
/***StringBuffer buf=new StringBuffer(msg.length()+6);
char ch=' ';
for(int u=0;u<msg.length();u++)
{
ch=msg.charAt(u);
if(ch=='\n')buf.append("");
else buf.append(ch);
}
String mm = buf.toString(); ********/
// out.println(new String(mm.getBytes("iso-8859-1"),"gb2312"));
String disposition = null;
disposition = part.getDisposition();
if ( disposition != null &&(disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE)))
{
//String fileName=getStr(part.getFileName()); //转换成中文GB2313
String fileName=null,AttachName =new String();
fileName=part.getFileName();
AttachName =fileName; //附件文件名
InputStream inputstream=part.getInputStream();
if(fileName==null)
{
fileName=File.createTempFile("attachment",".out").getName();
}
String filePath=new String();
filePath="c:\\MailAttachment\\"+username+"\\" ; // 存储路径
fileName=filePath+fileName;
File file=new File(fileName);
for(int j=0;file.exists();j++)
{
file=new File(fileName+j);
}
FileOutputStream fileoutputstream =new FileOutputStream(file);
BufferedOutputStream bufferedoutputstream =new BufferedOutputStream(fileoutputstream);
BufferedInputStream bufferedinputstream =new BufferedInputStream(inputstream);
int m;
while ((m=bufferedinputstream.read())!=-1)
bufferedoutputstream.write(m);
//save the filename
String SqlUpdate=new String();
SqlUpdate="UPDATE INBOX SET AttachmentName='"+AttachName+"',AttachmentPath='"+filePath+"' ";
SqlUpdate+="WHERE ID='"+InboxID+"'"; //InboxID
executeUpdateSql(SqlUpdate);
bufferedoutputstream.flush();
bufferedoutputstream.close();
bufferedinputstream.close();
}
}
//out.println("Attachment2");
}
connectInsert.close();
} //FOR 循环结束
hfprogramer
2003-05-05
打赏
举报
回复
超过了字符串的长度了吧。
yoken
2003-05-05
打赏
举报
回复
数组越界
lynx1111
2003-05-05
打赏
举报
回复
贴代码!·
有楼上兄弟在
尽管贴,保证摆平!
Philzzf
2003-05-05
打赏
举报
回复
数组越界了
比如
String[] s = {"1","2","3","4","5"};
System.out.println(s[5]);
这样就错了,编译能通过,但运行时会异常
代码折叠工具 For Eclipse ——修复版
针对此
错误
的修复版
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: 45 at
java
.
lan
g.
String
.charAt(Unknown Source) at com.cb.eclipse.folding.
java
.calculation.UserDefinedRegionHelper.isSentinel(UserDefinedRegionHelper.
java
:53) 使用方法如下:http://my.oschina.net/axes/blog/124714 他的jar包是未修复版的。大家可以测试。测试方法:在代码后边直接写两个斜杠,示例如下: private Object mObject;// 就会报我上边贴的
错误
。
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: -1
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: -1 at
java
.
lan
g.
String
.sub
string
(
String
.
java
:1967) at com.
java
2nb.novel.core.crawl.CrawlParser.parseBook
Index
AndContent(CrawlParser.
java
:219) at com.
java
2nb.novel.service.impl.CrawlSe
报错:
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: 25
报错:
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: 25 报错原因
String
字符串截取,被截取的字符串长度不够25 字符串截取时,需要判断非空,并且长度需要达到截取的长度 // 判断非空 if(!
String
Util.isNull(str)){ // 长度大于25就截取 if(str.length() > 25){ str = str.sub
string
(0,24);// 注意不能写(0,25)
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: 8
springboot报错 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: 8] with root cause
java
.
lan
g.Str
MyBatis报错: Cause:
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: -66 的解决方案
SpringBoot MyBatis报错: Cause:
java
.
lan
g.
String
Index
OutOfBoundsException
:
String
index
out of range: -66 的解决方案
Web 开发
81,115
社区成员
341,737
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章