org.hibernate.util.ReflectHelper.getConstructor 空指针异常

先给你们写我的解决方式,造福伸手党:

多半问题是出现在了构造里 你一定写了

select new (xxx,xxx,xxx) from xxx...

1.检查你的构造是不是类型和实际类型不符

2.java.util.Date这个类比较个性.

3.保持你的构造参数名和类的成员变量名相同.

我是用3解决的,你们可以试试,虽然我还没找到依据.

重点说2和3.

假设我们有一个构造:

public XXX(String a,Date b,BigDecimal Cc){
  this.a = a;
  this.b = b;
  this.Cc = Cc;
}

而你有这样一个属性列表:

private String a;
private Date b;
private BigDecimal Cc;

这基本包含了你可能遇见的错误.

关于第2点,你的构造里不能直接写Date b.你要写成

public XXX(String a,String b,BigDecimal Cc){
  this.a = a;
  this.b = DateHelper.parseString(insertTime,null);
  this.Cc = Cc;
}
public static Date parseString(String s,String format) throws ParseException{
<span style="white-space:pre">		</span>if(format==null) format = "yyyy-MM-dd HH:mm:ss";
<span style="white-space:pre">		</span>SimpleDateFormat df = new SimpleDateFormat(format);
<span style="white-space:pre">		</span>return df.parse(s);
}

能看懂,是吧?看不懂google一下.

关于第三点,如果你写成了

public XXX(String a,String b,BigDecimal cc){
  this.a = a;
  this.b = DateHelper.parseString(insertTime,null);
  this.Cc = cc;
}

你觉得是一样的是吧,我也这么觉得...可就不行...必须和属性名相同.

我找到了抛出异常的部分:

	public static Constructor More ...getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
336		final Constructor[] candidates = clazz.getConstructors();
337		for ( int i = 0; i < candidates.length; i++ ) {
338			final Constructor constructor = candidates[i];
339			final Class[] params = constructor.getParameterTypes();
340			if ( params.length == types.length ) {
341				boolean found = true;
342				for ( int j = 0; j < params.length; j++ ) {
343					final boolean ok = params[j].isAssignableFrom( types[j].getReturnedClass() ) || (
344							types[j] instanceof PrimitiveType &&
345									params[j] == ( ( PrimitiveType ) types[j] ).getPrimitiveClass()
346					);
347					if ( !ok ) {
348						found = false;
349						break;
350					}
351				}
352				if ( found ) {
353					if ( !isPublic( clazz, constructor ) ) {
354						constructor.setAccessible( true );
355					}
356					return constructor;
357				}
358			}
359		}
360		throw new PropertyNotFoundException( "no appropriate constructor in class: " + clazz.getName() );
361	}

注意看343行,有一个 `isAssignableFrom` 方法.

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
Parameters:
cls the Class object to be checked
Returns:
the boolean value indicating whether objects of the type cls can be assigned to objects of this class
Throws:
NullPointerException if the specified Class parameter is null.
Since:
JDK1.1
439
440     public native boolean More ...isAssignableFrom(Class<?> cls);

我没有证据是不是来自于这,堆栈信息其实并没有打印它,只打印到了`getConstructor`方法343行的异常.

所以,如果你有时间,并且感兴趣,找出原因,请回复我.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 03:09:48

org.hibernate.util.ReflectHelper.getConstructor 空指针异常的相关文章

hibernate HQL —— ReflectHelper.java:343

最近碰到个郁闷的问题嵌套查询时 UserAddress userAddress = (UserAddress) this.findOne("select new UserAddress(uuid,regionByDistrict,regionByCountry,regionByProvince,regionByCity,addressName,consignee,email,address,zipcode,tel,mobile,signBuilding,bestTime) from UserAd

org.hibernate.MappingException: class com.itheima.domain.Customer.java not found while looking for property: cust_id at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.

我这次异常的出现时,没有配置逐渐生成策略. 原文地址:https://www.cnblogs.com/chenyanlong/p/9737702.html

Could not parse mapping document from input stream hibernate配置异常

十二月 18, 2016 4:24:29 下午 org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.B

[Hibernate开发之路](2)Hibernate问题

(1)数据库驱动问题 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: com.m

Hibernate学习笔记(2)Hibernate问题

(1)数据库驱动问题 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: com.m

Could not parse mapping document from file

java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccesso

java反射(Field的应用)

//$Id: DirectPropertyAccessor.java 11405 2007-04-15 12:50:34Z [email protected] $ package org.hibernate.property; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Map; import org.hibernate.HibernateException; import o

Error creating bean with name &#39;sessionFactory&#39; defined in class path resource [applicationContext.xm

报错信息如下: <span style="font-size:24px;">17:18:32,245 ERROR ContextLoader:307 - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resour

(异常)Hibernate的动态模型

解决办法 不用自带的Hibernate包 自己下载Hibernate包导入 最近出现这样的错误 2015-5-10 12:24:16 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 2015-5-10 12:24:16 org.hibernate.Version logVersion INFO: HHH000412: