2024年3月7日发(作者:)

java异常解决方案

一、Hibernate

(1)entObjectException: object

references an unsaved

(2)ateSystemException:

Don't change the reference to a collection with

cascade="all-delete-orphan": ts; nested

exception is ateException: Don't change

the reference to a collection with cascade="all-delete-orphan":

ts

二、Tomcat

(1)tomcat启动时报错:eption

(2)tomcat内存溢出

三、JAVA基本

(1)astException:

(2)ortedClassVersionError: Bad version

number in .class file

四、JSP

(1)eption:

(2)Exception: Unable to compile

class for JSP:

(3)e() for servlet jsp threw exception

: Unresolved compilation problem:

(4): Unresolved compilation problem:The

method contextInitialized(ServletContextEvent) of type

CreateDataSourceTableListener must override a superclass

method

(5)e() for servlet jsp threw exception

: Unresolved compilation problem:

The method setCharacterEncoding(String) is undefined for the

type ServletResponse

五、SSH整合

(1)otFoundException:

tLoaderListener

(2)Exception starting filter struts2 Class:

ObjectFactory File:

Method: getClassInstance

(3)(7)(8)eationException:

Error creating bean with name 'biz' defined in

(4)Exception starting filter struts2

The action name cannot be the same as the action suffix [Action]

- Class: ionNameBuilder

(5)eErrorException: Error thrown in

preDeregister method

Caused by: sDefFoundError:

org/apache/struts2/util/ObjectFactoryDestroyable

(6)Unable to load configuration. - bean -

jar:file:/D:/Tomcat/tomcat/apache-tomcat-6.0.30/webapps/test/WEB-INF/lib/!/:29:72

六、Struts

(1)MethodException:

Student()

(2)Caused by: astException:

11Configuration cannot be cast

to serConfiguration

(3)No result defined for action and result

七、Android

(1)There is no android project named 'cm-android'

一、Hibernate

一(1)

entObjectException: object references an

unsaved transient instance - save the transient instance before

flushing: 某个对象的某个属性是一个实体,在这个实体没有保存之前就保存这个对象而造成了这个错误。

(有可能是因为如:保存Student对象student时,为null(这里主要指id为null)。(Studnent学生表 引用 Klass班级表(多对一)))

一(2)

ateSystemException:

Don't change the reference to a collection with

cascade="all-delete-orphan": ts; nested

exception is ateException: Don't change

the reference to a collection with cascade="all-delete-orphan":

ts:先注意一下几方面:集合应该用HashSet,而不是ArrayList

更新的时候,无论原来的集合为空还是不为空,都应该在得到原来集合的基础上

对得到的集合进行增删操作,而不是新new一个集合设置为该对象的的集合 然后可参考一下两段代码,第一段是错误的,第二段正确:

= rentById(id);

();

ldren(newChildren);

parent(parent);

= rentById(id);

();

(newChildren);

parent(parent);

另外:

我在employee里有设置一个set

cascade="all-delete-orphan" >

class="nt" />

然后我添加employee时,第一次添加没事, 再添加时就抛出以上错误, 而且都不能再添加, 要服务器重启后才能再添加一个, 就是服务器启动一次只能加一个employee,大家帮忙看看怎么解决?? 加那个cascade="all-delete-orphan" 是为了级联删除 把employee删了的时候也能同时删了document。像这种情况,可以看看,Employee实体类中的documents字段是否是用了new,new出了对象。这里不能new,不用new的话,应该就没问题了。(但我不知道原因)

二、Tomcat

二(1)

tomcat启动时报错:eption

EOFException表示输入过程中意外地到达文件尾或流尾的信号,导致从session中获取数据失败。异常是tomcat本身的问题,由于tomcat上次非正常关闭时有一些活动session被持久化(表现为一些临时文件),在重启时,tomcat尝试去恢复这些session的持久化数据但又读取失败造成的。此异常不影响系统的使用。 因为保存在硬盘上的session数据读取失败,问题似乎不大,但是如果不处理一下,每次启动都会出现这个问题。

【解决办法】

将tomcat目录下的work下面的文件清空,主要是*.ser文件,或者只是删除掉亦可。

如果正常关闭服务端,该文件是自动删除的。

删除。如果正常关闭服务端,该文件是自动删除的。

注:yourProjectName 是你当前正在工作的Web项目名称。

考虑到每个人的tomat的工作目录不同,建议在“搜索”功能中找到你的文件,而且只需要删除..yourProjectName下的即可。

二(2)tomcat内存溢出

/blog/static/171626147/

三、JAVA基本

三(1)

astException:erContactDetailDto cannot be cast to

viderRegisterContact(可能是类型转换错误(这里就是指不能将ProviderContactDetailDto强制转换为SCMProviderRegisterContact))

三(2)

在Myeclipse中运行小应用程序时出现如下异常的解决办法

ortedClassVersionError: Bad version number

in .class file 异常

出现的原因:因为Tomcat6.0需要jdk1.6.0以上版本支持 , 但是MyEclipse6.0的jdk版本是5.0 的, 所以会出现这个异常。

具体解决办法:

--> Preferences -->Java --> compiler中右边的compiler

compliance level对应的下拉菜单中选择JDK版本(一般选6.0).

--> Preferences -->MyEclipse -->Application Servers

-->Tomcat --> Tomcat 6.x(选最下面的那一个) -->JDK中右边的Tomcat JDK name下的对应的下拉菜单中选择自身电脑上安装的JDK版本,如果下拉菜单中没有,则通过Add-->在JRE home directory:的右边单击Browse添加(找到安装在本台电脑中的jdk,必需与步骤1中的JDK版本一致,一般路径如下:C:Program

FilesJavajdk1.7.0,如果没有安装jdk,则下载一个jdk7.0以上版本安装即可),添加进去后,在JDK中右边的Tomcat JDK name下的对应的下拉菜单中就可以选择刚才添加进去的JDK版本了,然后点击右下角的Apply即可.

如果还报异常,是因为有些MyEclipse版本自带有JDK版本,所以也要将它改过来.

--> Preferences -->Java -->Installed JRES,然后在右边选择与步骤1和步骤2版本一致(jdk1.7.0)的JDK版,即在jdk1.7.0的前面单选框上打勾即可,如果没有jkd1.7.0,只有MyEclipse 6.0,那么需要通过右边的Add添加,添加方式跟步骤2一样,然后jdk1.7.0就显示出来了,再选中即可,到此,应该没有问题了!

四、JSP

四(1)

报错:eption: Invalid argument

looking up property: "ame" of

bean: "dto";查询时或者添加时都可能报该错,可能是因为"org"为空。添加时,"ame"传入到action中,org不会为null,但如果在action中进行处理时,把它变为了null,那么就会报次错误。反正要注意""中是否有对象为null

四(2)

Exception: Unable to compile class

for JSP:

An error occurred at line: 12 in the generated java file

Only a type can be imported. resolves to

a package翻译为:一个发生错误行:12生成的Java文件

只有一种类型可以被导入。 会被解析为一个包 错误的原因可能就是:在某个jsp页面中有"<%@page

import=""%>"这么一句,而项目中找不到""这个包,可将此句删除即可。 还有就是项目中很多jsp文件,要找到有错的jsp页面,只需在控制台中报的异常中的自己的类(可点击进去的类(点击直接跳到那一行))中找到对应的jsp文件。

四(3)

严重: e() for servlet jsp threw exception

: Unresolved compilation problem:

The method setCharacterEncoding(String) is undefined for

the type ServletResponse 该异常跟上一个异常类似,像报这种错的差不多都是因为jsp页面有错。这是报的ServletResponse中没有setCharacterEncoding这个方法,找到jsp页面调用该方法的地方删掉即可。要找到jsp页面,跟上一个异常相似。

四(4)

启动报错:严重: Exception sending context initialized event to

listener instance of class

DataSourceTableListener

: Unresolved compilation problem:

The method contextInitialized(ServletContextEvent) of type

CreateDataSourceTableListener must override a superclass

method 其实不用启动,项目上会画小红色叉叉(编译不通过),去掉方法上的"@override"即可。

四(5)

严重: e() for servlet jsp threw exception

: Unresolved compilation problem:

The method setCharacterEncoding(String) is undefined for

the type ServletResponse

问题是因为:同时导入了 和 javaEE 5.. 在他们中都有rvletRequest,而项目中默认是导入的 中的该类,而可能该jar版本过低,反正在该jar包的HttpServletRequest类中就是没得setCharacterEncoding方法。移除即可。

五、SSH整合

五(1)

otFoundException:

tLoaderListener 报此异常时,其实ContextLoaderListener类所在jar包是导入了的,还报次错,是因为tomcat的原因(猜想:可能是因为发布在上面的项目过多)。

五(2)

严重: Exception starting filter struts2

Class: ObjectFactory

File:

Method: getClassInstance

Line: 209 -

com/opensymphony/xwork2/spring/:209:-1这是因为整合SSH2时没有在文件中配置监听器

五(3)

eationException:

Error creating bean with name 'biz' defined in class path

resource []: Cannot resolve

reference to bean 'dao' while setting bean property 'dao';

nested exception is

eationException:

Error creating bean with name 'dao' defined in class path

resource []: Invocation of init

method failed; nested exception is

lArgumentException: 'sessionFactory' or

'hibernateTemplate' is required因为"dao"对应的类继承自HibernateDaoSupport,而在spring中配置该类时,未配置

name="sessionFactory" ref="sessionFactory" />

五(4)

严重: Exception starting filter struts2

The action name cannot be the same as the action suffix [Action]

- Class: ionNameBuilder

File:

Method: build

Line: 64 -

org/apache/struts2/convention/:64:-1

Caused by: lStateException: The action name

cannot be the same as the action suffix [Action]有可能是因为

action类继承自ActionSupport,而在为ActionSupport导入包时,导成了spring的包,应该导入的是Support。

五(5)

eErrorException: Error thrown in

preDeregister method

Caused by: sDefFoundError:

org/apache/struts2/util/ObjectFactoryDestroyable 明明导入了jar包,在jar包结果中可以找到该类,但还报此错,不知为什么,重新启动就好了。可能是因为我用的MyEclipse8.5自动引入的SSH,其中某些jar包可能有冲突。

五(6)

Unable to load configuration. - bean -

jar:file:/D:/Tomcat/tomcat/apache-tomcat-6.0.30/webapps/test/WEB-INF/lib/!/:29:72

Caused by: Unable to load bean: type:

class:Factory - bean -

jar:file:/D:/Tomcat/tomcat/apache-tomcat-6.0.30/webapps/test/WEB-INF/lib/!/:29:72

Caused by: Bean type class

Factory with the name xwork has

already been loaded by bean -

jar:file:/D:/Tomcat/tomcat/apache-tomcat-6.0.30/webapps/test/WEB-INF/lib/!/:29:72 - bean -

jar:file:/D:/Tomcat/tomcat/apache-tomcat-6.0.30/webapps/test/WEB-INF/lib/!/:

29:72 这个异常,从最后一个Caused by:...就可以看出是因为jar包冲突所致,但我的项目结构中只有,并没有!原因是因为之前项目已经加载有那个包,tomcat中存在,只需停掉tomcat然后删除多余的jar包,重启tomcat即可(我是直接将tomcat-webapps中我的项目中的lib文件夹下的所有jar包全删了,然后将我示例项目中的所有jar包放入其中,重启tomcat即可)。

五(7)

eationException:

Error creating bean with name 'biz' defined in class path

resource []: Initialization of bean

failed; nested exception is sDefFoundError:

Could not initialize class er

Caused by: sDefFoundError: Could not

initialize class er这个可能是因为在文件中配置事物时,“

id="myPointcut" expression="execution(* .*(..))"

/>”中表达式对应的biz中是纯类,没有接口。将结构改为实现类继承接口的形式,即可解决。(不知为什么配置事物必须是要面向接口编程?)————

a.如何强制使用CGLIB实现AOP?:

*添加CGLIB库:spring_home/cglib/*.jar.

*在spring配置文件()中加入:

3)、如果目标对象没有实现接口,必须使用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换。

总结:JDK动态代理和CGLIB字节码生成的区别:

*JDK动态代理只能对实现了接口的类生成代理,而不能针对类;

*CGLIB是针对类实现代理,主要是对指定的类生成一个子类作为代理,覆盖其中的 方法, 因为是继承,所以该类或方法最好不要声明成final

出现上面错误的根本原因是包冲突,以上给的添加接口的方法并不完美,其实上边也说了可以不用基于接口。完美解决方法如下:

归根结底还是导入包引起的冲突:

Spring Aop Libraries中的 与 Hibernate core

Libraries 中的冲突

解决方法一:删去Spring Aop Libraries中的 ,其它不变,

解决方法二:删去Hibernate core Libraries 中的和 (这里又因为Hibernate core Libraries中的又与Spring Aop Libraries中的冲突)

其中,我亲自试过解决方法一,能够解决问题。记住不光要将该jar包从工程中导出,还必须把它从WebRoot下WEB-INF下的lib文件夹中删掉才行。

还记住:在未面向接口编程时,一定要在""节点中配置" proxy-target-class="true""事物才会有效?(此不确定,我将该属性删掉,运行删除功能还是很正常)。 而且很神奇的是:我的项目中,Student与Klass是多对一关系,如果我没有在文件中配置不延迟加载(lazy="false")的话,则会报以下异常:interException

Proxy(:372)

Proxy(:3121)。同样只需将删掉即可正常。有时还需同时删掉。

五(8)

eationException:

Error creating bean with name 'dao' defined in class path

resource []: Cannot resolve

reference to bean 'sessionFactory' while setting bean property

'sessionFactory'; nested exception is

eationException:

Error creating bean with name 'sessionFactory' defined in class

path resource []: Cannot resolve

reference to bean 'dataSource' while setting bean property

'dataSource'; nested exception is

eationException:

Error creating bean with name 'dataSource' defined in class

path resource []: Initialization of

bean failed; nested exception is

lArgumentException: warning no match for this

type name: [Xlint:invalidAbsoluteTypeName]

Caused by:

eationException:

Error creating bean with name 'sessionFactory' defined in class

path resource []: Cannot resolve

reference to bean 'dataSource' while setting bean property

'dataSource'; nested exception is

eationException:

Error creating bean with name 'dataSource' defined in class

path resource []: Initialization of

bean failed; nested exception is

lArgumentException: warning no match for this

type name: [Xlint:invalidAbsoluteTypeName]

Caused by:

eationException:

Error creating bean with name 'dataSource' defined in class

path resource []: Initialization of

bean failed; nested exception is

lArgumentException: warning no match for this

type name: [Xlint:invalidAbsoluteTypeName]

Caused by: lArgumentException: warning no

match for this type name:

[Xlint:invalidAbsoluteTypeName]这个有可能是因为在文件中配置事物时,“

id="myPointcut" expression="execution(* .*.*(..))"

/>”中配成了expression="execution(* .*(..))"所以报错说无法将强制转换为一个类。

六、Struts

六(1)

MethodException:

Student()该异常可能是因为:配置action时使用了通配符,最后调用action时地址写错了,写的地址对应的方法不存在。

六(2)

有时struts2项目运行时会出现下面的错误:

Caused by: astException:

11Configuration cannot be cast

to serConfiguration

11Configuration

不能转换为

serConfiguration

出现这个异常,首先看看,tomcat中是否已经存在与当前发布的项目同名的项目,将其删除即可。若不是这个错,则用以下方式:

这两个类均位于一个名叫的架包里,删除这个架包可以解决这个问题

六(3)

当你在用struts2开发时,如果,从页面跳入action时,页面报No

result defined for action and result时,大部分的原因有以下几种:

1、validate方法没有通过;

2、页面元素中有重命名时,但后台action类的对应的接收此同名参数的是变量而没有写成数组

要检查这种错误时,可以

1,在后台action类中重写ActionSupport中的

void addActionError(String anErrorMessage)

void addActionMessage(String aMessage)

void addFieldError(String fieldName, String errorMessage)

这三个方法,在并在其实现代码中设置断点,监控传入的参数,并可获知页面的相关报错具体原因.

2,在页面中加入以下标签,将错误显示出来:

还有:

value="%{#}" list="#ist"

listKey="id" listValue="kname"

name="students[%{#}].">我使用这个标签时,headerKey=" "这个属性值为空格时,也出这个错。

还有一种情况(应该是常犯的):jsp页面中的参数与对应action中接收该参数的字段的类型不一致。如:我用struts2的optiontransferselect标签的时候,该标签的name和doubleName属性(分别是左边和右边下拉框的name属性)我设的是student 和teacher,而在对应Action中为定义的用于接收该两个参数的字段分别是 List student 和 List teacher,这样就报此错了,因为此时下拉框的name属性为student和teacher,那么传入到action中的是String[]类型,所以出错。这里要么将action中的两个字段类型改为String[]类型,要么将jsp中optiontransferselect标签的name和doubleName属性值改为"" 和 ""。

七、Android

七(1)

在Run Configurations面板中提示There is no android project

named 'cm-android',并且运行时控制台提示 not

find *.apk:先看看有没有包,没有即右击项目--Properties--android--选择相应的android版本,如果项目正常的话,那么这样就应该将导入到项目中了;如果没有反映,则右击项目--Properties--java build path--Add

Labraries--Android Classpath Container--选择自己的项目,有可能这样才将导入,但很可能还是报上面的错误。我最终是在项目文件夹中,将红感叹号(改动过的)文件删掉,重新更新(SVN),然后重新导入项目,没,就右击项目--Properties--android--选择相应的android版本即可。