I had already tried solutions mentioned in Why is my Spring @Autowired field null? yet the problem persists. I have tried annotating the class DevicePojo(code below) with @Configurable @Service.
Here are my beans DistributionConfig.java
@Component
@Configuration
public class DistributionConfig {
@Qualifier("exponentialDistribution")
@Bean
@Scope("prototype")
public DistributionService exponentialDistribution() {
return new ExponentiallyDistribute();
}
@Qualifier("normalDistribution")
@Bean
@Scope("prototype")
public DistributionService normalDistribution() {
return new NormallyDistribute();
}
@Qualifier("uniformDistribution")
@Bean
@Scope("prototype")
public DistributionService uniformDistribution() {
return new UniformlyDistribute();
}
}
JsonFileConfig.java
@Configuration
public class JsonFileConfig {
private static ObjectMapper mapper=new ObjectMapper();
@Qualifier("devicesPojo")
@Bean
public DevicesPojo[] devicesPojo() throws Exception {
DevicesPojo[] devicePojo=mapper.readValue(new File(getClass().getClassLoader().getResource("Topo/esnet-devices.json").getFile()),DevicesPojo[].class);
return devicePojo;
}
@Qualifier("linksPojo")
@Bean
public LinksPojo[] linksPojo() throws Exception {
LinksPojo[] linksPojo=mapper.readValue(new File(getClass().getClassLoader().getResource("Topo/esnet-adjcies.json").getFile()),LinksPojo[].class);
return linksPojo;
}
}
Here is my DevicePojo where i get the null pointer exception.
@JsonDeserialize(using = DeviceDeserializer.class)
@Component
public class DevicesPojo {
private String device;
private List<String> ports;
private List<Integer> bandwidth;
@Autowired
@Qualifier("uniformDistribution")
private DistributionService uniformDistribution; // Here uniformDistribution is null
public DevicesPojo(String device, List<String> port, List<Integer> bandwidth) {
this.device = device;
this.ports= port;
this.bandwidth=bandwidth;
this.uniformDistribution.createUniformDistribution(1000,0,ports.size());
}
public String getDevice(){
return device;
}
public String getRandomPortForDevice()
{
return ports.get((int)uniformDistribution.getSample());
}
public List<String> getAllPorts(){
return ports;
}
public int getBandwidthForPort(String port){
return bandwidth.get(ports.indexOf(port));
}
}
However, if i replace private DistributionService uniformDistribution;
with private DistributionService uniformDistribution=new UniformDistribution()
the code is working fine.