0

I'm doing a cron job that uses spring-solr to get data from a core, process the data and then save it to the same core. The connection to solr and the execution of the cron job aren't an issue, (i mean i tested the cron job with a simple message 'Hello world', and the solr connection with an independent REST service). my problem is that a get a null pointer exception error when this line executes:

    List<Usuario> turistas = viajeroRepository.findAllCities(); 

This is the full code from that class:

@Service
public class NormalizeCities extends QuartzJobBean {

    private static final Logger logger = Logger.getLogger(NormalizeCities.class);

    @Autowired
    private ViajeroRepository viajeroRepository;

    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {

        List<Usuario> turistas = viajeroRepository.findAllCities();     
        Set<String> cities = new HashSet<String>();

        for (Usuario turista : turistas)
            cities.add(turista.getReviewer_city());


        int counter = 0;
           Iterator<String> iterator = cities.iterator();
            while(iterator.hasNext()) {
                String city = iterator.next();

                JSONObject json = new JSONObject(Util.readUrl(Util.getGoogleURL(Util.normalize(city), counter)));

                String status = json.getString("status");

                if (status.equals("OK")) {

                     processJson(json,city);


                } else if (status.equals("OVER_QUERY_LIMIT")) {
                    counter++;

                    if (counter == 4) {
                        counter = 0;
                    }
                    json = new JSONObject(Util.readUrl(Util.getGoogleURL(
                            Util.normalize(city), counter)));
                    processJson(json,city);

                }

            }

            logger.info("HECHO:Normalizacion de ciudades");


    }



    private void processJson(JSONObject json,String city) {

        json = json.getJSONArray("results").getJSONObject(0);

        String cityName = json.getString("formatted_address");

        json = json.getJSONObject("geometry").getJSONObject("location");

        Double lat = json.getDouble("lat");
        Double lng = json.getDouble("lng");

        List<Usuario> viajeros = viajeroRepository.findAllCitiesByLocation(city);

        for(int index=0;index<viajeros.size();index++){

            viajeros.get(index).setReviewer_city(cityName);
            viajeros.get(index).setLat(lat);
            viajeros.get(index).setLng(lng);
        }

        viajeroRepository.save(viajeros);
    }

}

the solr repository code:

public interface ViajeroRepository extends SolrCrudRepository<Usuario, String> {

    @Query(value="reviewer_city:* AND -lat:* AND -lng:*", fields = {"reviewer_city"})
    List<Usuario> findAllCities();


    @Query(value="reviewer_city:?0 AND -lat:* AND -lng:*")
    List<Usuario> findAllCitiesByLocation(String reviewer_city);


    @Query(value="id_destino:?0 AND reviewer_city:* AND lat:* AND lng:*", fields = {"reviewer_city","lat","lng"})
    List<Usuario> findOriginCityCoordinates(String id_destino);



}

the quartz.xml

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

    <bean id="job" class="mx.sectur.job.NormalizeCities" />


    <!-- Quartz Job -->
    <bean name="Job" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="mx.sectur.job.NormalizeCities" />
    </bean>




    <!-- Cron Trigger -->
    <bean id="cronTriggerJob"
                class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="Job" />
        <property name="cronExpression" value="0 05 14 ? * *" />
    </bean>



    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerJob" />

            </list>
        </property>
    </bean>

</beans>

the stacktrace:

2017-05-26 14:05:00 ERROR JobRunShell:211 - Job DEFAULT.Job threw an unhandled E
xception:
java.lang.NullPointerException
        at mx.sectur.job.NormalizeCities.executeInternal(NormalizeCities.java:32
)
        at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJob
Bean.java:75)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.j
ava:573)
2017-05-26 14:05:00 ERROR ErrorLogger:2425 - Job (DEFAULT.Job threw an exception
.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exc
eption: java.lang.NullPointerException]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.j
ava:573)
Caused by: java.lang.NullPointerException
        at mx.sectur.job.NormalizeCities.executeInternal(NormalizeCities.java:32
)
        at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJob
Bean.java:75)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)

Did i missed something to configure in the cron job?

Progs
  • 1,059
  • 7
  • 27
  • 63
  • Please tell us which line is this `mx.sectur.job.NormalizeCities.executeInternal(NormalizeCities.java:32) `. – walen May 29 '17 at 07:17

1 Answers1

0

You should know what exactly is the line of

java.lang.NullPointerException
    mx.sectur.job.NormalizeCities.executeInternal(NormalizeCities.java:32)

But looking at your code, I assume dependency injection is not working for viajeroRepository.

Have a look at this post: How to use @Autowired in a Quartz Job?

freedev
  • 25,946
  • 8
  • 108
  • 125