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行的异常.

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值