Question

Spring throws a NPE while calling the @PostCOnstruct method. Apparently it does not initialize ONLY ONE variable which is already specified inside a bean at the XML file.

The XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

    <bean id="blueprint" class="com.rfslabs.TheDAO">

        <property name="SLEEP_TIME" value="1"/>
        <property name="LIFESPAN_BIG" value="60"/>
        <property name="LIFESPAN_SMALL" value="20"/>
        <property name="ALERT_SIZE" value="80"/>
        <property name="MAX_SIZE" value="3072"/>
        <property name="BUILD_FOLDER" value="/var/www/clients/client2/web6/web/builds/"/>
        <property name="EXISTING" value="4"/>

        <property name="ALERT_L1" value="none"/>
        <property name="ALERT_L2" value="none"/> 

    </bean>

</beans>

The variable that is null after this:

ApplicationContext con = new FileSystemXmlApplicationContext("res/config.xml");

dao = (TheDAO) con.getBean("blueprint");

is the String

ALERT_L2 

I have provided setters and getters for all the variables in TheDAO class, see it for yourself:

    public class TheDAO {

public int SLEEP_TIME;//SECONDS
public int LIFESPAN_BIG;//SECONDS
public int LIFESPAN_SMALL;//SECONDS
public int ALERT_SIZE;//IN %
public int MAX_SIZE;//IN MB
public int EXISTING;
public File BUILD_FOLDER;
public String ALERT_L1;
public String ALERT_L2;

public String getALERT_L1() {
    return ALERT_L1;
}
public void setALERT_L1(String aLERT_L1) {
    ALERT_L1 = aLERT_L1;
}

public String getALERT_L2() {
    return ALERT_L2;
}
public void setALERT_L2(String aLERT_L2) {
    ALERT_L1 = aLERT_L2;
}

public int getEXISTING() {
    return EXISTING;
}
public void setEXISTING(int eXISTING) {
    EXISTING = eXISTING;
}

    public int getSLEEP_TIME() {
    return SLEEP_TIME;
}
public void setSLEEP_TIME(int sLEEP_TIME) {
    SLEEP_TIME = sLEEP_TIME;
}

public int getLIFESPAN_BIG() {
    return LIFESPAN_BIG;
}
public void setLIFESPAN_BIG(int lIFESPAN_BIG) {
    LIFESPAN_BIG = lIFESPAN_BIG;
}

public int getLIFESPAN_SMALL() {
    return LIFESPAN_SMALL;
}
public void setLIFESPAN_SMALL(int lIFESPAN_SMALL) {
    LIFESPAN_SMALL = lIFESPAN_SMALL;
}

public int getALERT_SIZE() {
    return ALERT_SIZE;
}
public void setALERT_SIZE(int aLERT_SIZE) {
    ALERT_SIZE = aLERT_SIZE;
}

public int getMAX_SIZE() {
    return MAX_SIZE;
}
public void setMAX_SIZE(int mAX_SIZE) {
    MAX_SIZE = mAX_SIZE;
}

public File getBUILD_FOLDER() {
    return BUILD_FOLDER;
}
public void setBUILD_FOLDER(File bUILD_FOLDER) {
    BUILD_FOLDER = bUILD_FOLDER;
}

@PostConstruct
public void init(){

    ALERT_SIZE = ( ALERT_SIZE * MAX_SIZE ) / 100;    

    SLEEP_TIME *= 1000;

    LIFESPAN_BIG *= 1000;
    LIFESPAN_SMALL *= 1000;

    if(!ALERT_L1.equals("none")){
    ALERT_L1 = "sudo php " + ALERT_L1;
    }

    if(!ALERT_L2.equals("none")){
        ALERT_L2 = "sudo php " + ALERT_L2;
        }

}


}

The program crashes when it comes to this point:

if(!ALERT_L2.equals("none")){
        ALERT_L2 = "sudo php " + ALERT_L2;
        }
}

...because ALERT_L2 is null . I think this is spring's fault.

Was it helpful?

Solution

public void setALERT_L2(String aLERT_L2) {
    ALERT_L1 = aLERT_L2;
}

You are not setting ALERT_L2 but ALERT_L1. Correct it and it should work.

OTHER TIPS

Incorrect value assignment in setter

public void setALERT_L2(String aLERT_L2) {
    //ALERT_L1 = aLERT_L2;
    this.ALERT_L2 = aLERT_L2; //should be
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top