0

I am parsing certain elements from a html file from a website, converting them to objects and adding them into my database, I have no issue when parsing and creating objects of type game, I can also add team objects accordingly to each game (one to one relation so one game has one home team and one away team which are both of type team). When I try to add these game/team objects to an already existing league in the database, that has a one to many relation with game and team, I get the following error: (Note i am using springs mvc framework)

java.lang.NullPointerException: null
    at com.FYP.Club.controller.HomeController.addNewPost(HomeController.java:98) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_60]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_60]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_60]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_60]

I have also researched this exception on other threads on stack overflow (such as What is a NullPointerException, and how do I fix it?)

I understand the concept behind the error but cant figure out the correct way to initialise the league object. Thanks!

Here is my controller:

@Controller
public class HomeController {


  @Autowired
    UserLoginRepository userRepository;

  @Autowired
        TeamRepository teamRepository;

  @Autowired
    GameRepository gameRepository;

  @Autowired
        LeagueRepository leagueRepository;



  @RequestMapping(value="/parsegames", method=RequestMethod.GET)
    public String index(Game game) {
        return "parseGame";

    }

  @RequestMapping(value = "/parsegames", method = RequestMethod.POST)
    public String addNewPost(@Valid Model model) throws IOException {

      Document doc = Jsoup.connect("http://www.irishrugby.ie/club/ulsterbankleagueandcup/fixtures.php").get();


      Elements kelime = doc.select("tr[id^=fixturerow]");
        for(Element sectd:kelime){
            Elements tds = sectd.select("td"); 

                  String result = tds.get(0).text();
                   String result1 = tds.get(1).text();
                   String result2 = tds.get(2).text();
                   String result3 = tds.get(3).text();
                   String result4 = tds.get(4).text();
                   String result5 = tds.get(5).text();
                   String result6 = tds.get(6).text();
                   String result7 = tds.get(7).text();


                   //creates a new game object
                   Game game = new Game();


                   game.setDatePlayed(result);
                   game.setFinalScore(result4);

                   //searches db for team with the name then adds the team 
 to the game homeside
                   Team team = teamRepository.findByTeamName(result3);
                   game.setHomeSide(team);


                   Team team2 = teamRepository.findByTeamName(result5);
                   game.setAwaySide(team2);

                   //saves full game info to db
                   gameRepository.save(game);

            League league = leagueRepository.findByLeagueName(result2);
                 league.setLeagueName("Still");
                 league.setDivision(result2);
                 league.setSeason("17/18");
                 league.addTeam(team);
                 league.addTeam(team2);
                 league.addGame(game);
                 leagueRepository.save(league); 






        }

        return "parseresult";

    }

Here is the leagueRepository:

@Repository
public interface LeagueRepository extends JpaRepository<League, Long> {

    League findByLeagueName(String leaguename);


}

Here is my league class:

@Entity
public class League {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int leagueId;
    private String season;
    private String leagueName;
    private String division;

    // league has many matches and is one to many with teams

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
     public Set<Game> games;


    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
     public Set<Team> teams;

    public League()
    {

    }

    public League(String leagueName, String division, String season) {
        super();
        this.season = season;
        this.leagueName = leagueName;
        this.division =  division;
    }





    public League(int leagueId, String season, String leagueName,
            String division, Set<Game> games, Set<Team> teams) {
        super();
        this.leagueId = leagueId;
        this.season = season;
        this.leagueName = leagueName;
        this.division = division;
        this.games = games;
        this.teams = teams;
    }

    public String getDivision() {
        return division;
    }

    public void setDivision(String division) {
        this.division = division;
    }

    public int getLeagueId() {
        return leagueId;
    }

    public void setLeagueId(int leagueId) {
        this.leagueId = leagueId;
    }

    public String getSeason() {
        return season;
    }

    public void setSeason(String season) {
        this.season = season;
    }

    public String getLeagueName() {
        return leagueName;
    }

    public void setLeagueName(String leagueName) {
        this.leagueName = leagueName;
    }

    public Set<Game> getGames() {
        return games;
    }

    public void setGames(Set<Game> games) {
        this.games = games;
    }

    public Set<Team> getTeams() {
        return teams;
    }

    public void setTeams(Set<Team> teams) {
        this.teams = teams;
    }

       public void addTeam(Team team){
            teams.add(team);
        }

       public void addGame(Game game){
            games.add(game);
        }




}
  • Which exactly is line 98 in `HomeController`? – lzagkaretos Dec 13 '17 at 15:47
  • 1
    Hi lzagkaretos, it is this line: league.setLeagueName("Still"); whenever i try set anything of "league" it happens – Gavin James Beere Dec 13 '17 at 15:49
  • The problem is that `leagueRepository.findByLeagueName(result2)` returns a `null` object. I see that you search for League with league name `result2` and that afterwards you set `result2` in division field. Is it possible you have an error in the select argument? Have you executed expected sql query in the database? – lzagkaretos Dec 13 '17 at 16:02
  • 1
    lzagkaretos - it is returning null as i have mistakenly used findByLeagueName when it should have been findByDivision. Now im getting "java.net.SocketTimeoutException: Read timed out" But thats a different story.. I cant nominate this at the correct answer, maybe if you post it below? – Gavin James Beere Dec 13 '17 at 16:14
  • Current issue probably has to do with the external call you do in order to fetch the data. You can try again, the site seems to be online and working. – lzagkaretos Dec 13 '17 at 16:19
  • 1
    Thanks lzagkaretos for all your help, i needed to populate the db with team objects which stopped the "read time out" error. Thanks again! – Gavin James Beere Dec 13 '17 at 16:46

2 Answers2

1

In your League.class add following:

@OneToMany(cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
public Set<Game> games = new HashSet();

instead of

@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
public Set<Game> games;
mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • 1
    Thanks mrkernelpanic for the response, I made the change you suggested, recreated my database, restarted the application and I still receive the same error – Gavin James Beere Dec 13 '17 at 15:57
0

You have used findByDivision instead of findByLeagueName in order to retrieve League object from the database.
The above result came from discussion in the comments section and after observation the usages of result2 variable.

lzagkaretos
  • 2,842
  • 2
  • 16
  • 26