1

I have a context config file like so :

applicationContect.xml:

   <bean id="CalcValidate" class="com.fmr.pmsvcs.orders.fixture.utility.CalcValidate" >
    <property name="orderRestServiceL" >
       <ref local="OrderRestService" />
    </property>
  </bean>

And my class looks like bellow :

CalcValidate.java

public class CalcValidate {
 public static OrderRestService orderRestServiceL;

    public static OrderRestService getOrderRestServiceL() {
        return orderRestServiceL;
    }

    public static void setOrderRestServiceL(OrderRestService orderRestServiceL)      {
        CalcValidate.orderRestServiceL = orderRestServiceL;
    }
  public static String getNetAssets(String user, BigInteger fundid,
            Order CalcOrder) throws Exception {
        PortfolioReferenceParameter par =
                orderRestServiceL.netAssets(user, fundid);

        if (par.getPortfolios().get(0) == null
                && CalcOrder.getPortfolioTna() == null
                && CalcOrder.getPortfolioTnaUsd() == null) {
            System.out
                    .println("    ##### PASS Portfolio Net Asset are null in service and DB");
            return OrderFixtureConstants.TRUE;
        }

        // *** Validate against Net Asset in Fund Base Currency
        if (!par.getPortfolios().get(0).getTotalNetAssets()
                .equals(CalcOrder.getPortfolioTna())) {
            return ("FAIL  net Asset in response ["
                    + CalcOrder.getPortfolioTna()
                    + " ] doesn't match net Asset in DB ["
                    + par.getPortfolios().get(0).getTotalNetAssets() + " ]");
        }

        System.out.println("    ##### PASS   net Asset in response ["
                + CalcOrder.getPortfolioTna()
                + " ] does match net Asset in DB ["
                + par.getPortfolios().get(0).getTotalNetAssets() + " ]");

        // *** Validate against Net Asset in Fund Base Currency
        if (!par.getPortfolios().get(0).getTotalNetAssetsUSD()
                .equals(CalcOrder.getPortfolioTnaUsd())) {
            return ("FAIL  net Asset USD in response ["
                    + CalcOrder.getPortfolioTnaUsd()
                    + " ] doesn't match net Asset in DB ["
                    + par.getPortfolios().get(0).getTotalNetAssetsUSD() + " ]");
        }

        System.out.println("    ##### PASS   net Asset in response ["
                + CalcOrder.getPortfolioTnaUsd()
                + " ] does match net Asset in DB ["
                + par.getPortfolios().get(0).getTotalNetAssetsUSD() + " ]");

        return OrderFixtureConstants.TRUE;

    }
}

In later part of my class I am calling a method like orderRestServiceL.getMethod(); Here "orderRestServiceL" coming as null. Anybody has any Idea how to resolve this?

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39
Raj
  • 29
  • 7
  • Could you please show a bit more of your classes? – dunni Apr 16 '15 at 20:36
  • @dunni I edited the code. But for some reason it is not showing some part of the class as code. – Raj Apr 16 '15 at 20:42
  • What is the reason behind the static field and method? Spring components are singleton by default, being said that why are you declaring static fields? It makes unit testing complicated and non final static fields are code smell. – minion Apr 16 '15 at 23:46

1 Answers1

2

Your issue seems to be related to the Java class field. it is declared as static while it should be non-static:

public class CalcValidate {

  private OrderRestService orderRestServiceL;

  public OrderRestService getOrderRestServiceL() {
    return orderRestServiceL;
  }

  public void setOrderRestServiceL(OrderRestService orderRestServiceL) {
    this.orderRestServiceL = orderRestServiceL;
  }
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • :in my case I need to declare it as a public static. as my method is public static – Raj Apr 16 '15 at 21:01
  • I am not sure why do you need it as static? This notation is injecting a value to an object so it mustn't be static. – MaxZoom Apr 16 '15 at 21:05
  • You can try this [hack](http://stackoverflow.com/a/7253751/4454454) for static field that I do not recommend. – MaxZoom Apr 16 '15 at 21:10
  • @Raj: Then make the field *and* the method non-static. Is there really a good reason, why you need the static method? – dunni Apr 17 '15 at 08:17