解决java.lang.ClassCastException的java类型转换异常的问题
作者:bdlaxx 发布时间:2023-11-29 04:34:10
标签:java.lang.ClassCastException,java
在项目中,需要使用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


猜你喜欢
- 目录Spring是什么?Spring Boot是什么?Maven依赖项springboot为不同的Spring模块提供了许多启动程序依赖项。
- 前言本博客将分析一下大名鼎鼎的 Leakcanary 想必作为 Android 开发都多多少少接触过,新版本的 Leakcanary 也用
- 本文实例讲述了Android开发实现拨打电话与发送信息的方法。分享给大家供大家参考,具体如下:xml布局:<LinearLayout
- spring-MVC实现简单的登录功能,供大家参考,具体内容如下今天我学习了spring-MVC实现简单的登录功能,本篇博客就讲解如何使用s
- java的接口解耦方式我只想把抽象的东西说的具体,或者说,听起来简单些,明白些。。。学过java的人都知道,java是单继承的,也就是说一个
- 上一篇实现了移动端微信消息界面功能,以此为基础继续完善服务端功能服务端微信消息页实现微信消息界面的实现,和登录,注册是类似的,无非就是接受客
- 在该示例中,阐述了SpringMVC如何上传文件。1、上传页面upload.jsp<body> <form
- 一.正则基础1. []方括号表示匹配可以匹配方括号中的任意点单个字符,方括号只允许匹配单个字符。2. | 或表示两项之间的一个选择,它不能和
- C#调用MFC 窗口 DLLMFC DLL创建一个窗口类,加public和AFX_EXT_CLASSMFC DLL属性注意MFC的使用:在共
- 互连网早期的时候,主机间的互连使用的是NCP协议。这种协议本身有很多缺陷,如:不能互连不同的主机,不能互连不同的操作系统,没有纠错功能。为了
- 一、简介这是画板系列的第一篇,一步步开始,从简单的画板,到功能稍微齐全一点的画板,例如基本画笔、橡皮擦、背景、文字、撤销、反撤销、保存等这篇
- 界面中控件较多的话,每个控件都设置setOnClickListener(this)是很麻烦的,为此抽出了一个Context的扩展类:fun
- 注解作用注解是JDK1.5版本开始引入的一个特性,用于对代码进行说明,可以对包、类、接口、字段、方法参数、局部变量等进行注解。它主要的作用有
- 本文实例为大家分享了Android studio点击跳转WebView的具体代码,供大家参考,具体内容如下代码文件import androi
- 本文实例为大家分享了Android实现View滑动的具体方法,供大家参考,具体内容如下1.View的滑动简介View的滑动是Android实
- 你可能在上篇文章中《深入多线程之:双向信号与竞赛的用法分析》注意到了这个模式:两个Waiting 循环都要下面的构造:lock(_locke
- C语言求三次方根前话说到C语言求根,我们一般会想到用sqrt(x)函数,它的输入值和返回值都是double型,x取整将会导致编译器错误。但是
- 合成聚合复用原则合成复用原则又称为组合/聚合复用原则(Composition/Aggregate Reuse Principle, CARP
- 这篇文章主要介绍了Springboot如何设置静态资源缓存一年,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 早期的项目比较简单,多是用JSP 、Servlet + JDBC 直接搞定,后来使用 Struts1(Struts2)+Spring+Hib