简体   繁体   English

使用总和时发生空指针异常

[英]Null Pointer Exception while using sum agregation

While developing an ERP using Spring MVC, JPA/Hibernate i need summing the cr fields of the entity opération but when executing query with createquery 使用Spring MVC开发ERP时,JPA / Hibernate我需要对实体操作的cr字段求和,但是在使用createquery执行查询时

I'm attempting to sum the CR fields of the entity operation in a MVC ERP I'm building but when executing the query with createQuery I get an null pointer exception. 我正在尝试对正在构建的MVC ERP中的实体操作的CR字段求和,但是当使用createQuery执行查询时,出现了空指针异常。

I tested selectQuery without the sum aggregation it worked well. 我在没有总和汇总的情况下测试了selectQuery,它运行良好。 I need clarification. 我需要澄清。

com.ensi.erp.dao.operationManagerImpl.sommesComptes 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 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;}
}

stackTrace 堆栈跟踪

 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

Reading the stack trace, it should be obvious to you. 读取堆栈跟踪,对您来说应该很明显。 The stack trace tells you exactly where it happens: 堆栈跟踪告诉您确切的位置:

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

So the error happens when calling sommeComptes() , which happens when constructing an instance of construireBilanImpl . 所以,当调用错误发生sommeComptes()构建的一个实例时发生construireBilanImpl

Let's look at the code: 让我们看一下代码:

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?
}

So, this has absolutely no relation with JPQL queries. 因此,这与JPQL查询绝对无关。 Stack traces provide extremely valuable information. 堆栈跟踪提供了非常有价值的信息。 Learn to read and understand them. 学习阅读和理解它们。

Side note: please don't use accents in your variable and method names. 旁注:请不要在变量和方法名称中使用重音符号。 Stick to ASCII characters. 坚持使用ASCII字符。 And respect the Java naming conventions. 并遵守Java命名约定。 Classes start with an uppercase letter. 类以大写字母开头。 And don't name doubles what is actually an array of integers. 并且不要将实际上是整数数组的名称doubles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM