1

I was trying to solve "Sonar Violation: Security - Array is stored directly", and i've been searching and i found THIS. I've tried it but it simply wont work, giving java.lang.reflect.InvocationTargetException

The method is very simple

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

and i tried:

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

and also

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

Could someone please explain to me why this is happening? Thank you.

EDIT

I tried

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

}

and it worked! I am really confused since this method will only be called when i have something to write, so i not possible for it to be null.

Stack Trace:

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)
Community
  • 1
  • 1
Snox
  • 580
  • 1
  • 10
  • 24

1 Answers1

2

Doesn't the error pretty much explain it? NullPointerException. Your method is being called with a null argument. Your method calls clone() on null. You can't do that. You have to check for null as you do in the version that works, so, do that. Your method still isn't correct though since it fails to set the field to null when the argument is null.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • The thing is, this error happens when i launch the application, so this method is not called unless there is something in the database, and if there is its not null! Thats why i don't understand the error... – Snox Jul 28 '14 at 15:30
  • Hibernate may be initializing the state of the object. Or maybe you do have an empty field somewhere. – Sean Owen Jul 28 '14 at 16:29