Java函数返回null

时间:2016-02-23 09:07:25

标签: java ejb oracle-adf

我正在尝试开发一个函数,如果结果成功,则返回一个字符串

这是我的功能:

 public String addAssistanceRecord(Date dateCreation, BigDecimal dureeAssistance, String etatAssistance,
                  String idAssistance, String ingAff, String ouvertPar, String sujetAss, String dep,String client,String idContrat) {


        Assistance assistance = new Assistance();
        assistance.setClient(client);
        assistance.setDateCreation(dateCreation);
        assistance.setDep(dep);
        assistance.setDureeAssistance(dureeAssistance);
        assistance.setIngAff(ingAff);
        assistance.setOuvertPar(ouvertPar);
        assistance.setSujetAss(sujetAss);
        assistance.setIdAssistance(idAssistance);
        assistance.setEtatAssistance(etatAssistance);


        //setting the contratid explicitly
          Contrat contrat = new Contrat();
             contrat.setIdContrat(idContrat);
                 assistance.setContrat(contrat);

       //persist the assistance object data to ASS table


                      Contrat con1 =assistance.getContrat();
                      String idCon= con1.getIdContrat();
                    //  System.out.println("id contrat "+ idCon);


     Query query =
         em.createQuery("SELECT a from Contrat a where a.idContrat =:id");

     query.setParameter("id", idCon);
     Contrat con2 = (Contrat) query.getSingleResult();
     em.persist(assistance);

    // System.out.println("id contrat 2 "+ con2.getIdContrat());
     //System.out.println("nb jr contrat 2 "+ con2.getNbJours());

     BigDecimal nbJours = con2.getNbJours();
     BigDecimal sldj =con2.getSoldeJours();
     int monSolde;
     if(sldj==null){

         monSolde =0;
     }
     else {
              monSolde=(con2.getSoldeJours()).intValueExact(); 
         }


       int monAssistance= (assistance.getDureeAssistance()).intValueExact();
       System.out.println("mon Solde est "+monSolde);
     System.out.println("ma durée est "+monAssistance);  

     if((monSolde - monAssistance > 0) ){
             if(monSolde==0){
         nbJours = nbJours.subtract(assistance.getDureeAssistance());

          con2.setSoldeJours(nbJours);
                 em.merge(con2);
               return "success";

     }
             else{
             nbJours = sldj.subtract(assistance.getDureeAssistance());

              con2.setSoldeJours(nbJours);
                     em.merge(con2);
               return "success";

     }



     }
     else {
             return "daddaaa";
         }

 }

这里我比较的条件((monSolde - monAssistance> 0))

这里是我的托管bean中的函数

public String Verification() {
        oracle.binding.BindingContainer bindings = getBindings();
        OperationBinding operationBinding = bindings.getOperationBinding("addAssistanceRecord");
       Object result = operationBinding.getResult();
      System.out.println("## Result = " + result);
         if (result == "success") {
            String messageText = "Votre Consommation du contrat est ajouté avec succès";
            FacesMessage fm = new FacesMessage(messageText);


            fm.setSeverity(FacesMessage.SEVERITY_FATAL);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, fm);
            return null;

        } 
        else 
        {
            String messageText = "Impossible d'ajouter cette consommation: la durée d'assistance est inférieure  a celle du contrat choisi";
            FacesMessage fm = new FacesMessage(messageText);


            fm.setSeverity(FacesMessage.SEVERITY_FATAL);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, fm);
            return null;

        }


    }

当我执行我的函数时,它正确完成所有工作,但函数(Verification)始终返回null

2 个答案:

答案 0 :(得分:1)

您始终null因为您始终return null;。请尝试使用:

   `retrun result;` instead of `return null;` .

尝试替换它:

  if (result == "success") {

使用equals代替==

 if ("success".equals(result)) {

答案 1 :(得分:0)

比较两个字符串时,您需要使用equals()函数而不是==运算符,因为==检查两个对象是否指向内存中的相同位置, equals()函数可以覆盖,当比较两个字符串时,检查值是否相同。