Java开发中遇到的异常记录

导读:本篇文章讲解 Java开发中遇到的异常记录,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一. java.lang.StringIndexOutOfBoundsException: String index out of range: -1
public class Demo {
    public static void main(String[] args) {
        String str = "How to Train Your Dragon";
        // 字符串长度为24
        System.out.println(str.length());
        // 打印内容:How to Train Your Dragon
        System.out.println(str.substring(0,24));
        // 索引超出范围报错:java.lang.StringIndexOutOfBoundsException: String index out of range: 25
        System.out.println(str.substring(0,25));
        // 找不到子字符串报错:java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        System.out.println(str.substring(0, str.lastIndexOf(".")));
    }
}

以下几个方法也会抛出该异常:

  • String.charAt()
  • String.codePointAt()
  • String.codePointBefore()
  • String.subSequence()
  • String.getChars()
  • String.getBytes()
二. java.lang.NumberFormatException: For input string: “”
public class Demo {
    public static void main(String[] args) {
        String str = "8";
        // 打印:8
        System.out.println(Integer.parseInt(str));
        str = "";
        // 报错:java.lang.NumberFormatException: For input string: ""
        System.out.println(Integer.parseInt(str));
        str = null;
        // 报错:java.lang.NumberFormatException: null
        System.out.println(Integer.parseInt(str));
    }
}
三. java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
public class Demo {
    public static void main(String[] args) {
        List<String> stars = new ArrayList<>();
        stars.add("迪丽热巴");
        // 打印:1
        System.out.println(stars.size());
        // 报错:java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        System.out.println(stars.get(1));
    }
}
四. IDEA 注释报红

解决方案:File —> Settings —> Editor —> Inspections —> Javadoc —> 将红色error的改成黄色警告

五. java.lang.NullPointerException
public class Demo {
    public static void main(String[] args) {
    	// 举个列子
        String str = null;
        // 报错:java.lang.NullPointerException
        System.out.println(str.split(","));
    }
}
六. Cause: java.sql.SQLException: Statement violates GTID consistency: Updates to non-transactional tables can only be done in either autocommitted statements or single-statement transactions, and never in the same statement as updates to transactional tables.?; uncategorized SQLException for SQL []; SQL state [HY000]; error code [1785];

解决方案:MySQL 版本提升后,之前有几张表的引擎用的是 MyIsam 引擎(没有事务,锁级别不一样),导致更新表失败。后来将表的引擎改为 InnoDB,就不报错了。

七. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column ‘request_data’ at row 1?; SQL [];

解决方案:这个看语义也能看出,就是插入数据的时候,有一个字段的数据大于设置的值了,导致插入失败,可以将字段大小增大,或者对数据大小进行限制。

八. Error updating database. Cause: java.sql.SQLSyntaxErrorException: ORA-01745: invalid host/bind variable name\n\n###

原因一:mybatis中的mapping映射时,sql语句中忘了加逗号,且逗号处有换行。
解决方案:把逗号补上就是,排查发现我不属于这种,继续找其他原因

原因二:当数据量过大时,拼接的sql语句长度太长,同样也会报这个异常。
解决方案:批量插入数据,我属于这类错误,改了之后就可以了

			// 为了防止SQL语句超出长度出错, 分成几次插入
            if (list.size() <= 1000){
                abcService.insertBatchs(list);
            }else {
                int times = (int) Math.ceil(list.size() / 1000.0);
                for (int i = 0; i < times; i++) {
                    abcService.insertBatchs(list.subList(i * 1000,Math.min((i+1)*1000,list.size())));
                }
            }
九. Every derived table must have its own alias

在做多表查询,或者查询的时候产生新的表的时候会出现这个错误:Every derived table must have its own alias(每一个派生出来的表都必须有一个自己的别名),最后给临时表加上别名就可以了

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/3022.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!