java常见错误集锦
java常见错误以及可能原因集锦
0、需要标识符
a) 不在函数内
1、非法表达式开始
b) 可能:丢失括号 .
2. no data found
a) 可能:setInt(1,100)中,没有100这个值
3. 找不到符号
a) 可能:没导入包
4. 指定了无效URL
a) 可能:数据库名或IP错误,即连接出错
5. 类路径没有找到
a) 可能: ClassNotFoundException: oracle.jdbc.driver.OracleDriver
b) 原因: 一般是指包名写错,或者没有import包,或者没有在类路径中找到jar文件
c) 解决: 没有加载Oracle驱动jar,在.bash_profile中把ojdbc14.jar加进来
6. 空指针异常
a) 可能: 数据源错误比如数据库名或IP错误
7. 不能执行查询
a) 可能: 数据库中表的问题,比如列名不存在
8. invalid identity
a) 可能: 列名出错
9. 若在数据库中创建了两个sequence ,运行时出现异常可能是先后执行了多次select 语句,导致与原有的序列号产生冲突
10. 表名或列名不存在
a) 可能:表不存在或者没有插入数据到表中
11. 不支持的类,类的版本错误
a) 可能:没有导入jdk5.0,或者编译器仍为 1.4
12. MappingNotFoundException
a) Maybe: In the Eclipse Not refersh , or not exist in the dirctory
13. HibernateException: /hibernate.cfg.xml not found
a) Maybe1: hibernate.cfg.xml not in the root directory
b) Maybe2: Could not parse configuration .
c) resolve: database not connect or use another database
14. ConstraintViolationException
a) Maybe: used a not true database
15. 驱动没有找到或者 JDBC Driver not found
可能:连接数据库的驱动jar包不存在或者版本不一致,比如将旧的版本换成新的会造
成该类错误
16. 空指针异常, https://www.360docs.net/doc/d67746197.html,ng.NullPointerException
a) 可能1:数据库连接出错,比如在hibernate.cfg.xml中的数据错误会导致异常。
17. 数据插入异常,GenericJDBCException: could not insert
a) 可能1:没有建立表或者表中没有任何数据
b) 可能2:插入数据后没有执行提交语句:commit
18. LazyInitializationException 或者延迟加载异常
a) 可能1:没有在查询语句中加fetch
19. IdentifierGenerationException
a) 可能1:高位表没有初始化(比如hi_value中没有记录)
20. could not initialize a collection: [hibernate.entity.Role.modules#32768]
Syntax error: Encountered "-" at line 1, column 132.
a) 错误原因:
21. could not insert collection rows: [hibernate.entity.Module.roles#1]
在Module.hbm.xml文件的如下配置中加入inverse=”true”
22.在部署Struts时,出现如下错误信息:
HTTP Status 404 - Servlet action is not available
type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not
available.
问题原因:
1.、web.xml文件中未配置ActionServlet。
2、struts-config.xml文件未配置你要访问的Action。
3、你的jsp文件form标记中action属性的路径名称错误。
4、非以上三种情况。
针对以上4种情况相应的解决方案如下:
1、在web.xml文件中加上ActionServlet的配置信息
?????? /WEB-INF/struts-config.xml?
?2、在struts-config.xml文件检查你要访问的Action配置文件。
3、检查jsp文件form标记中action属性的路径名称是否与struts-config.xml文件中action标记的path属性的路径名称一致。
4、非以上情况的解决办法就是检查web容器的log日志,如果时tomcat则检查下logs 目录下的localhost_log文件,看里边是否记录有错误信息,然后根据错误信息提示将
其纠正。
23.https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError: org/apache/commons/beanutils/Converter
缺少spring-framework-2.0.3\lib\jakarta-commons\commons-beanutils.jar
24. java 代码Caused by: https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
org/objectweb/asm/Type
缺少spring-framework-2.0.3\lib\asm\asm-2.2.2.jar包,版本不同,该包的名字有相
应的区别
java 代码Caused by: https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
org/dom4j/DocumentException
缺少spring-framework-2.0.3\lib\dom4j\dom4j-1.6.1.jar包,版本不同,该包的名字有相应的区别
把这个包进去就可以了:\Spring206\lib\dom4j、dom4j-1.6.1.jar
java 代码Caused by: https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
org/apache/commons/collections/SequencedHashMap
缺少spring-framework-2.0.3\lib\jakarta-commons\commons-collections.jar包,版本不同,该包的名字有相应的区别
java 代码Caused by: https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
net/sf/cglib/proxy/CallbackFilter
缺少spring-framework-2.0.3\lib\cglib\cglib-nodep-2.1_3.jar包,版本不同,该包的名字有相应的区别
java 代码Caused by: https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
org/objectweb/asm/CodeVisitor
缺少hibernate-3.2\lib\asm.jar包,版本不同,该包的名字有相应的区别
java 代码
org.hibernate.exception.SQLGrammarException: could not load an entity:
[https://www.360docs.net/doc/d67746197.html,er#1]
JAVA工程与WEB工程包的区别:一个是asm.jar,一个是jta.jar
java 代码Caused by: https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
javax/transaction/TransactionManager
缺少spring-framework-2.0.3\lib\j2ee\jta.jar包,版本不同,该包的名字有相应的区
别此种错误请检查相应的***.hbm.xml配置文件的配置
25, Exception in thread "main"
org.springframework.dao.InvalidDataAccessResourceUsageException:could not
get next sequence value; nested exception is
org.hibernate.exception.SQLGrammarException: could not get next sequence value Caused by: org.hibernate.exception.SQLGrammarException: could not get next
sequence value
原因:没有加Sequence或者数据库的方言写错了
26,我的配置文件
然后我运行保存一条数据进去
The database returned no natively generated identity value
就会报这个错误
因为你native是根据看底层数据库的能力选择identity, sequence 或者hilo中的一个
而我建表的时候id没有指定
alter table `student` change `id` `id` int auto_increment
我们把我们的表的结构小小的改动一下就没问题了
27,
UpdateManager无法启动2007-08-09 15:21如果在应用程序安装过程中,暴力中断安装
程序,会出现如下状况: 1.apt-get remove 和dpkg --remove 无法删除软件
2.UpdateManager无法启动
3.新立得软件包管理程序无法启动
以上可能是deb损坏之类造成的
sudo dpkg -r sqldveloper
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
E: 软件包 sqldeveloper 需要重新安装,但是我无法找到相应的安装文件。
解决方法: 1.从 /var/lib/dpkg/status 中把对应的段删掉
重要:修改之前请先备份
在status中找到你对应的包删除就OK了...
28,
Exception in thread "main" https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError:
antlr/ANTLRException
少了antlr-2.7.5H3.jar包,从
D:\eclipse\eclipse\plugins\https://www.360docs.net/doc/d67746197.html,.hibernate.eclipse_4.1.1\myeclips
e-data\3.0\lib
29,
Caused by:
https://www.360docs.net/doc/d67746197.html,ng.NoClassDefFoundError: javax/transaction/Synchronization
缺少Spring206\lib\j2ee\jta.jar
30,
Caused by:
https://www.360docs.net/doc/d67746197.html,ng.IllegalArgumentException: Cannot convert value of type [$Proxy3] to
required type [business.impl.PriceBiz] for property 'priceBiz': no matching
editors or conversion strategy found
在java类中设置priceBiz时应该设置的是接口PriceBizIf,而非实现类
31, 数组越界
可能1: 在命令行后面需要加入参数.
可能2:加入的参数错误
32, car is not mapped
可能1: 在cfg.xml中没有增加映射的hbm.xml文件在
33, 属性没有找到
可能1: hbm.xml文件中的
34,SQLException: 无当前连接可能1:在比如创建帐户时没有对相应的方法添加到
中去,比如这个没有加入:
Java代码
class="https://www.360docs.net/doc/d67746197.html,MatchMethodPointcutAdvisor">
class="https://www.360docs.net/doc/d67746197.html,MatchMethodPointcutAdvisor">
35,SQLException: Syntax error: Encountered "table" at line 1, column 8.
可能1;执行SQL时出现冲突,可能是SQL语句中使用了关键字作为变量来用,比如
update order set balance=1000000;
其中order被用作表名来使用,这是错误的,因为order是个关键字,用在order by中
36, Servlet /Spring-WebMvc threw load() exception
org.xml.sax.SAXParseException: Document root element "beans", must match
DOCTYPE root "null".
jar包的冲突,spring-1.2.6.jar和spring.jar出现在一个war的目录
中:JBoss/server/all/deploy/Spring-WebMvc.war/WEB-INF/lib
37, java.sql.SQLException: 当事务仍处于活动状态时,无法关闭连接。
38 ,
validateJarFile(/home/soft01/Tomcat/webapps/sms-token-struts/WEB-INF/lib/ser
vlet-api.jar)
Servlet /sms-struts-tiles threw load() exception
https://www.360docs.net/doc/d67746197.html,ng.ClassNotFoundException: org.apache.struts.action.ActionServlet
原因:在WEB-INF/lib/ 目录下有servlet-api.jar与/Tomcat/common/lib目录下的servlet-api.jar冲突了,把WEB-INF/lib/下的servlet-api.jar删除就可以了
39,https://www.360docs.net/doc/d67746197.html,ng.ClassNotFoundException:
org.springframework.web.struts.ContextLoaderPlugIn
Marking servlet action as unavailable
10:30:47,088 ERROR [/NetCT_OSS]:3953 - Servlet /NetCT_OSS threw load() exception javax.servlet.UnavailableException
40,org.xml.sax.SAXParseException: Document root element "beans", must match
DOCTYPE root "null".
缺少
MyEclipse/eclipse/plugins/com.genuitec.eclipse.springframework_5.5.0/data/2.
0/dist/modules/spring-struts.jar
41,Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint
(SD0702.FK82D343CF49A4B831) violated - parent key not found
Caused by: org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
有可能是ID生成策略的问题,我将它XML映射文件改成
42,
2007-08-19 16:21:43,290 ERROR
[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/OSS].[actio
n]] - Servlet.service() for servlet action threw exception
org.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the session: [entity.Module#2]
java.util.ConcurrentModificationException
原因:已经有对象拥有了所选择的module,用clear(),而不是remove();
43,
ERROR [org.apache.catalina.session.ManagerBase] - IOException while loading
persisted sessions: java.io.InvalidClassException: entity.Module; local class
incompatible: stream classdesc serialVersionUID = -5988350031935445758, local
class serialVersionUID = -297934247726484429
java.io.InvalidClassException: entity.Module; local class incompatible: stream classdesc serialVersionUID = -5988350031935445758, local class serialVersionUID
= -297934247726484429
在Module中加上一个关键字:transient
44,
Caused by: java.sql.BatchUpdateException: ORA-02292: integrity constraint
(SD0702.SYS_C00132664) violated - child record found
原因:有其他的表引用了该表的外建,所以报这个异常,如果没有引用就不会出现
45,
出现数组越界的时候,看看是不是循环时没有加=,比如:
for(int i = 1; i for(int i = 1; i<=arr.length;i++) 46.如果使用Ant和Junit是被报找不到test,检查一下是否误用了private 出错信息: type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedE rror: No tests found in test.AllTest 该错误有个很特别的特点,当你不通过ant来运行测试,而是通过AllTest类来运行的话,可以正常运行。小心哦 47.如果遇到报:表名无效,且jvm报严重错误,看看是不是用了数据库的保留关键字来 做表名了,如User。 48.使用ant时必须小心ant的classpath它用的不是IDE的classpath,小心! 49.小心下边的异常, exception setting property value with CGLIB (set https://www.360docs.net/doc/d67746197.html,e_reflection_optimizer=false for more info) setter of https://www.360docs.net/doc/d67746197.html,ers.?" type="net.sf.hibernate.PropertyAccessException">https://www.360docs.net/doc/d67746197.html,ng.ClassCastException at https://www.360docs.net/doc/d67746197.html,ersMetaClass1.setPropertyValues( net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues 上次出现该错错误的原因是: 源文件里我的class类型为一个类Contact contact 而跑到mapping里却成了Set,呵呵厉害。 50.突然间冒出大量的NullPointException 重新build一下。 51.="Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade" 52.Tapestry的出错信息: Class com.bookshop.Hello does not implement the IPage interface. location: context:/WEB-INF/Home.page, line 6 原因:与显示有关的哪个java类没有从IPage家族继承。 53.使用ant时给出的路径好象不允许出现空格。 54.由于使用ant时用junit做测试的话,classpath中出现j2ee.jar的话,问题多多。例如xml格式的log文件生成不了,莫名其妙的NullPointException等。所以我设置了两个包个包含j2ee.jar,为编译用;一个没有,为junit用 55.进行单元测试时,在查询返回后应马上assertNotNull(),这样可以更快速的定位NullPointException 56.要使用ResourceBundle的话,要千万小心。必须用日志记录下它的状态。还有该属 性文件应该放在classes下面。 57.当要显示任何页面时,都被提示无效,那么应该检查一下lib目录了,还有一些很奇怪的异常,例如你明明可以找到一个类但是服务器却提示ClassNotDefException,那么估计是缺少了该类所必须的包了。或者多了不兼容的包,如, xdoclet系列包不被struts 的lib目录所兼容 58.编写clone时从Java编程思想(2nd)上学到的(732): .引数传递过程中会自动产生别名(alias)。 .没有局域对象(local objects),只有局域性的(local)references。 .reference受范围(scope)的限制,对象则否。 .对象的寿命从来不是Java的讨论议题(因为有垃圾回收机制) 59.try catch finally的域居然是分离的。 60.jsp乱码的其中一个原因:charset="gb2312" 等号"="的两边不允许有空格。 61.我的基于Displaytag的简单报表解决方案。 https://www.360docs.net/doc/d67746197.html,/ 下载displaytag.jar和displaytag.tld displaytag.jar放在lib目录,而displaytag.tld放在WEB-INF目录,在web.xml中为displaytag.tld声明一下。 在jsp里使用前,加上 <%@ taglib uri="https://www.360docs.net/doc/d67746197.html," prefix="display" %> 注意该软件有个bug,他要用的一个包common-lang.jar版本必须在 2.0以上。 如果碰到下面异常,则应坚持一下是否该包的版本问题。 https://www.360docs.net/doc/d67746197.html,ng.NoSuchMethodError: https://www.360docs.net/doc/d67746197.html,ng.StringUtils.capitalize(Ljava/lang/String;)Ljava/lang /String; 然后就可放心使用了 定义表格的样子,用css定义。如 TABLE.its THEAD TR { BACKGROUND-COLOR: #69c } TABLE.its TR.even { BACKGROUND-COLOR: #def } 在使用分页时,可能会出现这种情况,点击其他页时,弹出下载窗口,让你下载当前jsp 页面,这是因为你在当前页面读取了数据的缘故。解决办法为在action里读取数据而不是在jsp里。可以参考 https://www.360docs.net/doc/d67746197.html,/example-paging.jsp?d-26189-p=2 62.使用displaytag时,在一列中放入多个元素 必须在display:table中定义一个id "/Bookshop/admin/bookManagement.do?action=view&id=<%=((Book)item).getId()%>" target = "_blank" >查看 "/Bookshop/admin/bookManagement.do?action=edit&id=<%=((Book)item).getId()%>"> 编辑 "/Bookshop/admin/bookManagement.do?action=delete&id=<%=((Book)item).getId()% >">删除 要在displaytag中使用链接,必须具备paramId,否则不显示为链接 "书名" paramId="item" paramProperty="product.id"/> 可以这样使用display 其中product为对象 63. session.getAttribute()取到的东西。 64.实验struts-upload例子时要注意的地方: 1.If you would rather write this file to another file, please check here: 这一行要打钩 2.If you checked the box to write to a file, please specify the file path here: 在这里要重命名如:c:\b.jpg 上传成功的话,会出现提示 The file has been written to "c:\b.jpg" 65.Hibernate的like可以这么用: Query query = session.createQuery("from src.persistent.Book as book where upper(https://www.360docs.net/doc/d67746197.html,) like :name "); query.setString("name", "%"); result = query.list(); 66.Hibernate出现 duplicate import : className 异常也可能是因为忘了为持久类在configuration中addClass了 67. name所引用的是session里的attribute。 68.如果发现要出现询问下载的情况,有可能是因为要跳转的页面出现了问题。试试在要跳转到的页删掉 <%@ page contentType="text/html; charset=gb2312"%> 69.如果JSP页面跳转时出现下边的错误信息: The request sent by the client was syntactically incorrect (Invalid path /web/shoppingCart was requested). 原因是struts-config的action = "x" 写成了 action = "x.do" 70.在struts中,strut-config.xml中,forward时使用redirect = "true"可以将.do 重定向为.jsp 71.以后在判断相等性之前先用logger把两个值显示出来。 72.从session里getAttribute后,修改并不需要重新setAttribute一次。 73.在hibernate中使用subclass是一棵继承树共用一个表,仅生成个mapping。 每个类中必须有discrimator-value。在最上层的类中必须声明: @hibernate.discriminator column = "class"。 不可以将子类添加到configuration里去。(即不可addClass(子类)) 使用Xdoclet的建立subclass的例子(该类是父类) /** * @hibernate.class discriminator-value = "customer" * @hibernate.discriminator column = "class" */ 而使用joined-subclass则是一类一表,也不许将子类添加进configuration里去。 /** * @hibernate.joined-subclass * @hibernate.joined-subclass-key * column="customer_id" */ 如果发现生成的mapping文件中joined-subclass的key column为空,那么可能是 @hibernate.joined-subclass-key这句没有写对。 用joined-subclass生成的表,仔细看。Member extends Customer create table Customer ( id VARCHAR2(255) not null, name VARCHAR2(255), description VARCHAR2(255), primary key (id) ) create table Member ( customer_id VARCHAR2(255) not null, password VARCHAR2(255), primary key (customer_id) ) 74.在junit中尽量使用assertEquals代替assertTrue; 75.Hibernate 如果Child extends Parent 那么from Parent as parent 也将会将Child选出来,而from Child as child 则不会选出Parent 76.Hibernate 使用hibernate的one-to-one时,应该两方向都set,否则会报save NullPointException parent.setChild(child); child.setParent(parent); 遇到下边的异常,估计是与因为外键出现了问题: https://www.360docs.net/doc/d67746197.html,ng.NullPointerException at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValue(Abstract EntityPersister.java:675) at net.sf.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:33) 如: * @hibernate.id generator-class = "foreign" * @hibernate.generator-param name = "property" value = "customer" * @hibernate.one-to-one name = "custmoer" class = "src.persistent.Customer" value的值和one-to-one 中name的值不符,则会出现上边的异常。如果不显示指定name 则默认取成员变量名 如 Customer a 则name = "a" 需要小心的是不是类名。所以,以后最好显示指定名字为好。 78.出现异常: Exceptionobject references an unsaved transient instance - save the transient instance before flushing: src.persistent.Product 原因没有为某对象进行set设置,如上边的这个就是某对象没有调用setProduct 79. "xxx action = "/a" 不用.do和根目录名