编译错误ava.lang.ExceptionInInitializerError at test.MybatisTest.testUpdateUser(MybatisTest.java:67) a

本文详细解析了在使用MyBatis框架进行用户信息更新操作时遇到的ExceptionInInitializerError异常,深入分析了错误根源在于类型别名解析失败导致的ClassNotFoundException,并提供了具体的解决方案。

java.lang.ExceptionInInitializerError
    at test.MybatisTest.testUpdateUser(MybatisTest.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in mybatis/user.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com..mybatis.pojo.User'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com..mybatis.pojo.User

原因:

"com..mybatis.pojo.User"包多了一个点无法编译
`java.lang.ExceptionInInitializerError` 表示在类的静态初始化块、静态变量初始化或类的构造函数中发生了异常。而 `java.lang.RuntimeException: java.lang.ExceptionInInitializerError` 是将 `ExceptionInInitializerError` 封装在 `RuntimeException` 中抛出。以下是一些常见的解决办法: #### 检查静态代码块中的资源加载 在静态代码块中加载资源文件时,可能会因为文件不存在或加载路径错误导致 `NullPointerException`。例如在引用 [1] 和 [2] 中,使用 `getClassLoader().getResourceAsStream` 加载配置文件时,若文件路径错误或文件不存在,会返回 `null`,后续操作就会抛出 `NullPointerException`。 ```java // 错误示例,可能加载为 null InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); Properties properties = new Properties(); properties.load(is); // 若 is 为 null,会抛出 NullPointerException // 正确做法,添加空值检查 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); if (is != null) { Properties properties = new Properties(); try { properties.load(is); } catch (IOException e) { e.printStackTrace(); } } else { System.err.println("未能找到 druid.properties 文件"); } ``` #### 检查依赖冲突 项目中的依赖冲突可能会导致类加载异常,从而引发 `ExceptionInInitializerError`。可以使用 Maven 或 Gradle 的依赖分析工具来检查和解决依赖冲突。例如在 Maven 中使用 `mvn dependency:tree` 命令查看依赖树,找出冲突的依赖并排除它们。 ```xml <dependency> <groupId>冲突依赖的groupId</groupId> <artifactId>冲突依赖的artifactId</artifactId> <exclusions> <exclusion> <groupId>需要排除的依赖的groupId</groupId> <artifactId>需要排除的依赖的artifactId</artifactId> </exclusion> </exclusions> </dependency> ``` #### 检查 JDK 版本 确保项目使用的 JDK 版本与代码兼容。不兼容的 JDK 版本可能会导致类加载异常或其他编译问题。可以在 IDE 中检查项目的 JDK 配置,以及 `JAVA_HOME` 环境变量是否正确设置。 #### 检查静态变量初始化逻辑 静态变量初始化时可能会调用其他方法或依赖其他类,若这些方法或类出现异常,会导致 `ExceptionInInitializerError`。检查静态变量初始化的代码逻辑,添加异常处理或日志输出,以便定位问题。 ```java public class DruidUtils { static DataSource dataSource = null; static { InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("servletDemo.properties"); Properties prop = new Properties(); try { if (is != null) { prop.load(is); } else { System.err.println("未能找到 servletDemo.properties 文件"); } } catch (IOException e) { e.printStackTrace(); } try { if (!prop.isEmpty()) { dataSource = DruidDataSourceFactory.createDataSource(prop); } } catch (Exception e) { e.printStackTrace(); } } // 其他方法... } ``` #### 清理项目缓存 有时 IDE 或构建工具的缓存可能会导致编译问题。可以尝试清理 IDE 的缓存,例如在 IntelliJ IDEA 中选择 `File` -> `Invalidate Caches / Restart`。同时,也可以清理 Maven 或 Gradle 的本地仓库缓存,删除 `~/.m2/repository` 或 `~/.gradle/caches` 目录下的相关缓存文件,然后重新构建项目。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值