解决java.lang.ClassCastException的java类型转换异常的问题
作者:bdlaxx 时间:2023-11-29 04:34:10
在项目中,需要使用XStream将xml string转成相应的对象,却报出了java.lang.ClassCastException: com.model.test cannot be cast to com.model.test的错误。
原因:
项目中应该是采用了热部署,devtools,因为累加载器的不同所以会导致类型转换失败
措施:
在pom.xml中将以下代码注释掉:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
补充知识:TreeSet在add对象时报ClassCastException错误
TreeSet实现了SortedSet接口,可以对集合中的对象进行排序,但是在使用TreeSet时要注意一点,那就是要给TreeSet传递一个比较器,也就是指定比较规则,否则的话,它就不知道谁大谁小,也就不能排序了。此时它会报一个ClassCastException的异常。
jdk1.6文档里add方法关于这个异常是这样描述的:
Throws:
ClassCastException - if the specified object cannot be compared with the elements currently in this set
翻译:ClassCastException - 如果指定的对象不能与当前在此集合中的元素进行比较
public class TreeSetTest
{
public static void main(String[] args)
{
MyComparator comparator = new MyComparator();
// TreeSet<Student> set = new TreeSet<Student>(comparator);
// 错误的代码,少了比较器,运行则报下面的异常。
TreeSet<Student> set = new TreeSet<Student>();
Student s1 = new Student(50);
Student s2 = new Student(70);
Student s3 = new Student(40);
set.add(s1);
set.add(s2);
set.add(s3);
System.out.println(set);
}
}
class Student
{
int score;
public Student(int score)
{
this.score = score;
}
@Override
public String toString()
{
// TODO Auto-generated method stub
return String.valueOf(this.score);
}
}
class MyComparator implements Comparator<Student>
{
@Override
//按分数高低比较,int为返回负数、零、整数,这里我写的不咋好,但意思一样
public int compare(Student o1, Student o2)
{
// TODO Auto-generated method stub
int result = 0;
if(o1.score > o2.score)
{
result = 1;
}else
{
result = -1;
}
return result;
}
}
错误的运行结果:
Exception in thread "main" java.lang.ClassCastException: com.shengsiyuan2.Student cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1294)
at java.util.TreeMap.put(TreeMap.java:538)
at java.util.TreeSet.add(TreeSet.java:255)
at com.shengsiyuan2.TreeSetTest.main(TreeSetTest.java:17)
解决办法:
把 TreeSet set = new TreeSet(); 改成:TreeSet set = new TreeSet(comparator);即可。
来源:https://blog.csdn.net/mianyao1004/article/details/99738947
标签:java.lang.ClassCastException,java
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jvm垃圾回收之GC调优工具分析详解
2023-08-17 12:40:20
![](https://img.aspxhome.com/file/2023/5/111455_0s.png)
Android Studio实现仿微信APP门户界面详解及源码
2022-06-29 20:08:52
![](https://img.aspxhome.com/file/2023/6/100606_0s.png)
关于SpringBoot使用Redis空指针的问题(不能成功注入的问题)
2023-09-04 01:30:03
![](https://img.aspxhome.com/file/2023/2/61302_0s.png)
Spring Boot中如何使用Convert接口实现类型转换器
2023-05-09 20:28:37
![](https://img.aspxhome.com/file/2023/6/80906_0s.png)
Java TCP协议通信超详细讲解
2021-05-30 20:53:35
![](https://img.aspxhome.com/file/2023/7/131977_0s.png)
Android 动态菜单实现实例代码
2023-05-19 16:24:58
![](https://img.aspxhome.com/file/2023/4/117094_0s.gif)
c#多线程的应用全面解析
2023-03-03 21:15:02
![](https://img.aspxhome.com/file/2023/2/77272_0s.png)
解析:ClickOnce通过URL传递参数 XXX.application?a=1
2022-05-26 01:54:34
SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码
2023-08-15 04:12:46
Spring MVC4.1服务器端推送实现过程解析
2021-07-24 14:53:16
![](https://img.aspxhome.com/file/2023/9/69949_0s.jpg)
Lombok中@EqualsAndHashCode注解的使用及说明
2023-11-30 04:47:05
![](https://img.aspxhome.com/file/2023/4/64204_0s.png)
19款Javascript富文本网页编辑器
2021-12-08 23:48:45
![](https://img.aspxhome.com/file/2023/3/70623_0s.jpg)
Android的UI调优教程
2021-12-16 01:46:43
![](https://img.aspxhome.com/file/2023/0/137620_0s.png)
Hibernate5新特性介绍
2023-01-02 16:28:09
探讨Java中函数是值传递还是引用传递问题
2021-07-25 19:34:44
![](https://img.aspxhome.com/file/2023/1/85831_0s.png)
java JUC信号量Semaphore原理及使用介绍
2023-01-02 02:08:57
![](https://img.aspxhome.com/file/2023/1/84621_0s.png)
Springboot+WebSocket实现一对一聊天和公告的示例代码
2022-06-16 11:32:33
C# using语法糖图文详解
2023-08-05 02:57:36
![](https://img.aspxhome.com/file/2023/0/75590_0s.jpg)
C# 设计模式系列教程-状态模式
2022-11-07 13:31:55
![](https://img.aspxhome.com/file/2023/9/111239_0s.png)
C++语言实现线性表之链表实例
2023-06-20 22:21:20