簡體   English   中英

使用總和時發生空指針異常

[英]Null Pointer Exception while using sum agregation

使用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();  
    }
    `

腸胃炎手術

`@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

讀取堆棧跟蹤,對您來說應該很明顯。 堆棧跟蹤告訴您確切的位置:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM