title: resolving null pointer exceptions in list in spring mvc and hibernate jpa
body: <p>i am using spring and hibernate jpa. i retrieved a list of records from the database. when in the controller i could see the content of the list by using System.out.println(). but when i try passing this list to another method it says a null pointer exception. below is the error</p>
  
  <pre><code>18-Nov-2015 18:50:47.092 INFO [http-apr-8080-exec-117]org.apache.catalina.core.ApplicationContext.log Destroying Spring    FrameworkServlet 'dispatcher'
  18-Nov-2015 18:50:51.724 INFO [http-apr-8080-exec-117]  org.apache.catalina.core.ApplicationContext.log Spring WebApplicationInitializers detected on classpath:  [com.domkat.springmvcjpa.config.AppInitializer@18a550e]
  18-Nov-2015 18:50:52.168 INFO [http-apr-8080-exec-117] org.apache.catalina.core.ApplicationContext.log Initializing Spring FrameworkServlet 'dispatcher'
  18-Nov-2015 18:51:14.761 SEVERE [http-apr-8080-exec-116] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/mysurvey] threw exception [Request processing failed; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0] with root cause
  java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
  at java.util.ArrayList.rangeCheck(ArrayList.java:638)
  at java.util.ArrayList.get(ArrayList.java:414)
  at  org.hibernate.collection.internal.PersistentBag.get(PersistentBag.java:440)
  at com.domkat.springmvcjpa.processor.Processor.traverseLegsBearings(Processor.java:271)
  at com.domkat.springmvcjpa.controller.StationController.analyseSurvey(StationController.java:132)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>please help.
  below is the controller method </p>
  
  <pre><code> @RequestMapping(value="sanalyse", method=RequestMethod.POST)
  public String analyseSurvey(Model model){
      List&lt;Fromtost&gt;ftcList=ftss.getFromtostList();
      model.addAttribute("fromtostations", ftcList);
      System.out.println(ftcList.size());
      Processor processor=new Processor();
      processor.traverseLegsBearings(ftss.getFromtostList());
  
  return "tables";
  }
  </code></pre>
  
  <p>below is the method i tried passing it to</p>
  
  <pre><code>public void traverseLegsBearings(List&lt;Fromtost&gt; ftcList) {
  
  
      double backwardBearingOfLegs = 0.0;
      double cpBB = ftcList.get(0).getSurveyId().getFromtocpsList().get(0).getBb();
      double firstOAdecDeg = ftcList.get(0).getDegdec();
      double numberOfStations = ftcList.size();
      double forwardBearingOfLegs = 0.0;
      List&lt;ArrayList&gt; bearings = new ArrayList&lt;&gt;();
  
      for (Fromtost s : ftcList) {
  
          forwardBearingOfLegs = (double) cpBB + s.getDegdec();
          if (forwardBearingOfLegs &gt;= 360) {
              forwardBearingOfLegs = forwardBearingOfLegs - 360;
              //forwardBearings.add(forwardBearingOfLegs);
          }
  
          if (forwardBearingOfLegs &gt;= 180) {
              backwardBearingOfLegs = forwardBearingOfLegs - 180;
  
          } else {
              backwardBearingOfLegs = forwardBearingOfLegs + 180;
  
  
          }
  
          s.setFb(forwardBearingOfLegs);
          s.setBb(backwardBearingOfLegs);
  
          forwardBearings.add(forwardBearingOfLegs);
          backwardBearings.add(backwardBearingOfLegs);
          cpBB = backwardBearingOfLegs;
  
      }
  
      /*bearings.add(forwardBearings);
      bearings.add(backwardBearings);*/
  
  
  
  }
  </code></pre>
  
  <p>thank you.</p>
  
  <p>the controller</p>
  
  <pre><code>@Controller
  public class StationController {
  @Autowired
  private FromtocpsService ftcs;
  @Autowired
  private FromtostService ftss;
  @Autowired
  private SurveysService ss;
  
  
  @RequestMapping(value="sanalyse", method=RequestMethod.POST)
  public String analyseSurvey(Model model){
      List&lt;Fromtost&gt;ftcList=ftss.getFromtostList();
      model.addAttribute("fromtostations", ftcList);
      System.out.println(ftcList.size());
      Processor processor=new Processor();
      processor.traverseLegsBearings(ftss.getFromtostList());
  
  return "tables";
  }
  </code></pre>
  
  <p>the entities involved are 
  Surveys:</p>
  
  <pre><code>public class Surveys implements Serializable {
  private static final long serialVersionUID = 1L;
  @Column(name = "surveytitle")
  private String surveytitle;
  @Column(name = "numofstations")
  private Integer numofstations;
  // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
  @Column(name = "g")
  private Double g;
  @Column(name = "n")
  private Double n;
  @Column(name = "misclosureineastings")
  private Double misclosureineastings;
  @Column(name = "misclosureinnorthings")
  private Double misclosureinnorthings;
  @Column(name = "bearingmisclosure")
  private Double bearingmisclosure;
  @Column(name = "allowablemisclosure")
  private Double allowablemisclosure;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "survey_id")
  private Integer surveyId;
  @Column(name = "matNumber")
  private String matNumber;
  @OneToMany(cascade=CascadeType.ALL,mappedBy = "surveyId",fetch=FetchType.EAGER)
  @Fetch(value = FetchMode.SUBSELECT)
  private List&lt;Fromtocps&gt; fromtocpsList;
  @OneToMany(cascade=CascadeType.ALL,mappedBy = "surveyId",fetch=FetchType.EAGER)
  private List&lt;Fromtost&gt; fromtostList;
  </code></pre>
  
  <p>2)Fromtocps</p>
  
  <pre><code>public class Fromtocps implements Serializable {
  private static final long serialVersionUID = 1L;
  // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
  @Column(name = "distance")
  private Double distance;
  @Column(name = "fl1")
  private String fl1;
  @Column(name = "fl2")
  private String fl2;
  @Column(name = "fr1")
  private String fr1;
  @Column(name = "fr2")
  private String fr2;
  @Column(name = "diffleft")
  private String diffleft;
  @Column(name = "diffright")
  private String diffright;
  @Column(name = "meandiff")
  private String meandiff;
  @Column(name = "decdeg")
  private Double decdeg;
  @Column(name = "eastingschange")
  private Double eastingschange;
  @Column(name = "northingschange")
  private Double northingschange;
  @Column(name = "fb")
  private Double fb;
  @Column(name = "bb")
  private Double bb;
  @Column(name = "fromtolabel")
  private String fromtolabel;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "fromtocps_id")
  private Integer fromtocpsId;
  @JoinColumn(name = "surveyId", referencedColumnName = "survey_id")
  @ManyToOne
  private Surveys surveyId;
  @JoinColumn(name = "fromtost_id", referencedColumnName = "fromtost_id")
  @ManyToOne
  private Fromtost fromtostId;
  @OneToMany(cascade=CascadeType.ALL,mappedBy = "fromtocpsId")
  private List&lt;Tocp&gt; tocpList;
  @OneToMany(cascade=CascadeType.ALL,mappedBy = "fromtocpsId")
  private List&lt;Fromcp&gt; fromcpList;
  </code></pre>