使用总和聚合时出现空指针异常

时间:2014-06-22 18:54:25

标签: java spring hibernate jpa

在使用Spring MVC开发ERP时,JPA / Hibernate我需要总结实体操作的cr字段,但在使用createquery执行查询时

我正在尝试在我正在构建的MVC ERP中对实体操作的CR字段求和,但在使用createQuery执行查询时,我得到一个空指针异常。

我测试了selectQuery而没有总和聚合它运行良好。我需要澄清。

com.ensi.erp.dao.operationManagerImpl.sommesComptes

`public float sommeComptes(int... doubles) {
        String liste= new String();
        String x;
        for (int d : doubles) {
            x=Integer.toString(d);
            x=x.concat(",");
            liste=liste.concat(x);
        }
    liste=liste.substring(0, liste.length()-1);
    String cc =" select sum(op.cr) from opération op ";
     System.out.println(cc);
        Query req= em.createQuery(cc);
        Number somme=(Number)req.getSingleResult();
        return somme.floatValue();  
    }
    `

com.ensi.entitis.operation

`@Entity
@Table(name="opérations")
public class opération implements Serializable {
@Id 
@Column(name="id")
    private int idop;
@Column(name="journal")
    private char journal;
@Column(name="compte")
    private int compte;
@Column(name="libellé")
    private String libellé;
@Column(name="dateop")
    private Date dateop;
@Column(name="dr")
    private float dr;
@Column(name="cr")
    private float cr;

//getters
public int getIdop(){return idop;}
public char getJournal(){return journal;}
public int getCompte(){return compte;}
public String getLibellé(){return libellé;}
public Date getDateop(){return  dateop;}
public float getDr(){return dr;}
public float getCr(){return cr;}

//setters
public  opération(){};
public  opération(int id,char j,int com,String lib, Date da ,float drr,float crr){idop=id;journal=j;compte=com;libellé=lib;dateop=da;dr=drr;cr=crr;};
public void setIdop(int op){idop=op;}
public void setJournal(char op){journal=op;}
public void setCompte(int op){compte=op;}
public void setLibellé(String op){libellé=op;}
public void setDateop(Date op){dateop=op;}
public void setDr(float op){ dr=op;}
public void setCr(float op){ cr=op;}


}`

com.ensi.metier.construireBilanImpl

package com.ensi.erp.metier;

import org.springframework.transaction.annotation.Transactional;

import com.ensi.erp.dao.OperationManager;
@Transactional
public class construireBilanImpl implements construireBilan {

    private  OperationManager opmanimpl2 ;

    private float immobilisations_corporelles=sommeComptes(1266,1322);
    private float immobilisation_corporelles;
    private float immobilisations_financieres;
    private float actifs_immobilisés;
    private float autres_actifs_nc;
    private float stocks;
    private float clients_et_comptes_rattachés;
    private float autres_actifs_courants;
    private float placements_et_autres;
    private float liquidités_et_equivalents;
    private float Total_actifs_courants;

    private float Capitaux_propres;
    private float Capital_social;
    private float Réserves;
    private float Autres_capitaux_propres;
    private float Résultat_Reporté;
    private float Total_avant_résultat;
    private float résultat_exercice;
    private float Total_capitaux_propres;


    private float Emprunts;
    private float Autres_passifs_financiers;
    private float Provisions;
    private float Total_passifs_nc;

    private float Fournisseurs_et_comptes_rattachés;
    private float Autres_passifs_courants;
    private float Concours_bancaires_et_autres;
    private float passifs_financiers;


    public construireBilanImpl(){};
    public void setOpmanimpl2(OperationManager x){opmanimpl2=x;}

    public float sommeComptes(int... doubles){
        return opmanimpl2.sommeComptes(doubles);


    }

    public float getImmobilisations_corporelles(){return immobilisations_corporelles;}
    public void setImmobilisations_corporelles(float x){immobilisations_corporelles=x;}
}

栈跟踪

 INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Jun 22 20:55:39 CEST 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/C:/Users/housseminfo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ERP_PCD/WEB-INF/classes/root-context.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1888c363: defining beans [datasource,persistenceUnitManager,entityManagerFactory,transactionManager,operationmanager,opser,operationmanager2,bilan,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,operationController,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1888c363: defining beans [datasource,persistenceUnitManager,entityManagerFactory,transactionManager,operationmanager,opser,operationmanager2,bilan,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,operationController,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bilan' defined in URL [file:/C:/Users/housseminfo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ERP_PCD/WEB-INF/classes/root-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.construireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1011)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:957)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.construireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1004)
    ... 23 more
Caused by: java.lang.NullPointerException
    at com.ensi.erp.metier.construireBilanImpl.sommeComptes(construireBilanImpl.java:48)
    at com.ensi.erp.metier.construireBilanImpl.<init>(construireBilanImpl.java:11)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 25 more
juin 22, 2014 8:55:40 PM org.apache.catalina.core.StandardContext listenerStart
Grave: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bilan' defined in URL [file:/C:/Users/housseminfo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ERP_PCD/WEB-INF/classes/root-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.construireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1011)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:957)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.construireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1004)
    ... 23 more
Caused by: java.lang.NullPointerException
    at com.ensi.erp.metier.construireBilanImpl.sommeComptes(construireBilanImpl.java:48)
    at com.ensi.erp.metier.construireBilanImpl.<init>(construireBilanImpl.java:11)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 25 more

1 个答案:

答案 0 :(得分:2)

读取堆栈跟踪,对您来说应该是显而易见的。堆栈跟踪会告诉您确切的位置:

java.lang.NullPointerException
at com.ensi.erp.metier.construireBilanImpl.sommeComptes(construireBilanImpl.java:48)
at com.ensi.erp.metier.construireBilanImpl.<init>(construireBilanImpl.java:11)

因此,在调用sommeComptes()时会发生错误,这在构造construireBilanImpl的实例时会发生。

让我们看一下代码:

private  OperationManager opmanimpl2 ;
// so opmanimpl2 is null.

private float immobilisations_corporelles = sommeComptes(1266,1322)

public float sommeComptes(int... doubles){
    return opmanimpl2.sommeComptes(doubles);
    //       ^-- what's the value of opmanimpl2 again?
}

因此,这与JPQL查询完全没有关系。堆栈跟踪提供极有价值的信息。学会阅读和理解它们。

附注:请不要在变量和方法名称中使用重音符号。坚持使用ASCII字符。并尊重Java命名约定。类以大写字母开头。并且不要将doubles命名为实际上是一个整数数组。