使用clone和Arrays.copyOf的byte []出错

时间:2014-07-28 14:48:52

标签: java spring bytearray sonarqube

我试图解决“声纳违规:安全 - 阵列直接存储”,我一直在搜索,我发现THIS。我已经尝试了但它根本无法工作,给予java.lang.reflect.InvocationTargetException

方法非常简单

public void setData( byte[] data) {
    this.data= data;
}

我试过了:

public void setData( byte[] data) {
    this.data= Arrays.copyOf(data, data.length);
}

以及

public void setData( byte[] data) {
    this.data= data.clone();
}

有人可以向我解释为什么会这样吗? 谢谢。

修改

我试过

public void setData( byte[] data) {
    if(data!= null){
        this.data= data.clone();
    }

}

它有效!我真的很困惑,因为这个方法只有在我有东西要写的时候才会被调用,所以我不可能将它作为空。

堆栈追踪:

Exception in thread "Timer-1" org.hibernate.PropertyAccessException: Exception occurred inside setter of com.project.hibernate.Clients.data
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:88)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:710)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:371)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4509)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:186)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1107)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:963)
    at org.hibernate.loader.Loader.doQuery(Loader.java:910)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2522)
    at org.hibernate.loader.Loader.doList(Loader.java:2508)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338)
    at org.hibernate.loader.Loader.list(Loader.java:2333)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at com.project.hibernate.dao.impl.ClientsDaoImpl.findAllClients(ClientsDaoImpl.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy38.findAllClients(Unknown Source)
    at com.project.bsl.client.InMemoryClientInfo.initOperate(InMemoryClientInfo.java:104)
    at com.project.bsl.client.InMemoryClientInfo$1.run(InMemoryClientInfo.java:117)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
    ... 36 more
Caused by: java.lang.NullPointerException
    at com.project.hibernate.Clients.setData(Clients.java:156)

1 个答案:

答案 0 :(得分:2)

这个错误几乎不解释它吗? NullPointerException。使用null参数调用您的方法。您的方法会在clone()上调用null。你不能这样做。你必须检查null和你工作的版本,所以,做到这一点。您的方法仍然不正确,因为当参数为null时,它无法将字段设置为null