I switched from getting values by loading up the properties file and retrieve them to using @Value annotations. Right now I am able to print the values that I set. When I run my application I see the value, But when I open up localhost:8080 I receive a null exception.
IndexController
@Controller
public class IndexController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView getdata() throws IOException {
AppPortList apL = new AppPortList();
List<AppPortModel> apList = apL.getAppPortList();
Collections.sort(apList);
ModelAndView model = new ModelAndView("index");
model.addObject("showap", apList);
return model;
}
Properties file
APP_SERVERS=Server1@!Server1212@!Server12daa21@!Server21334
APP_SERVER_List=1020@!3011@!8080@!2020
//much more servers than this.
Class that is having errors
@Component
public class AppPortList {
@Value("#{'${APP_SERVERS}'.split('@!')}")
private String[] apServerArray;
@Value("#{'${APP_SERVER_List}'.split('@!')}")
private String[] appServerPortsList;
@PostConstruct
public List<AppPortModel> getAppPortList() {
try {
System.out.println(apServerArray.length + "@@@@");
for (int z = 0; z < apServerArray.length; z++) {
String apServer = apServerArray[z];
String[] portListArray=appServerPortsList;
}catch {//stuff}
Console Output
16@@@@@
When I open local host I receive a null exception on Line 80. Which is my for Statement. "for (int z = 0; z < apServerArray.length; z++)" is coming up null.
Running local Host Error
java.lang.NullPointerException: null
at com.spring.web.util.AppPortList.getAppPortList(AppPortList.java:82) ~[classes/:na]
at com.spring.web.controller.IndexController.getdata(IndexController.java:61) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91]
Attempt 2
So I commented my for statment out and used System.out.println(apServerArray + "@@@@");
Output 2
[Ljava.lang.String;@328b8745@@@@@
and when I ran my localhost I get null. So why does my console reads it when I run it then when I open localhost it becomes null?
null@@@@@
${APP_SERVERS:}(add:before the closing accolade). Does it changes the behavior ?