2

I am trying to save data on gemfire using crud repository. I have created one operation class to call save method of repository but at autowired instance I am getting null pointer exception. Below is my code:

public interface GeodeRepository extends CrudRepository<KeyValueBean, String> {

    @Override
    public KeyValueBean findOne(String name);

    @Override
    public <S extends KeyValueBean> Iterable<S> save(Iterable<S> entities);
}

@EnableGemfireRepositories(basePackageClasses = GeodeRepository.class)
@EnableAutoConfiguration
@Configuration
public class Operations {

    @Autowired
    private GeodeRepository repository;

    public void saveKeyValueData(KeyValueBean keyValueBean) {
        System.out.println("Repository is : " + repository);
        repository.save(Arrays.asList(keyValueBean)); // <--- i am getting
                                                        // repository as null so
                                                        // getting null pointer
                                                        // exception
    }
}
kryger
  • 12,906
  • 8
  • 44
  • 65

2 Answers2

3

When we @Autowired any class make sure, you have declared that class as a @Component.

for example:

@Component
public class Operations {

    @Autowired
    private GeodeRepository repository;

    public void saveKeyValueData(KeyValueBean keyValueBean) {
        System.out.println("Repository is : " + repository);
        repository.save(Arrays.asList(keyValueBean)); 
    }
}

and try using @Autowired to Operation class to your class in which class your are calling your saveKeyValueData() method.

ddb
  • 2,423
  • 7
  • 28
  • 38
Abhijeet Behare
  • 597
  • 1
  • 7
  • 21
2

So, what is not apparent from your example is how you "bootstrap" your application and it's features (e.g. Repositories) into action.

It is not simply enough to add the Spring @Configuration, Spring Boot's @EnableAutoConfiguration and SD GemFire's @EnableGemfireRepositories annotations and expect everything to be auto-configured and wired up successfully. I.e. you need a bootstrapping mechanism, like Spring Boot, especially if you are using the @EnableAutoConfiguration annotation.

For example...

import org.springframework.boot.SpringApplication;
...

class MyApplication {

  public static void main(String[] args) {
    SpringApplication.run(Operations.class, args);
  }
}

Now, you could remove the @EnableAutoConfiguration from your Operations class and add the @SpringBootApplication to the MyApplication class, like so...

@SpringBootApplication
class MyApplication {
...
}

@SpringBootAppliation combines together Spring's @Configuration with Spring Boot's @EnableAutoConfiguration, along with many other useful meta-annotations, like Spring's @ComponentScan, enabling all sorts of magic to happen.

But, if you are not using Spring Boot, you can always bootstrap you application with the AnnotationConfigApplicationContext, like so..

class MyApplication

  public static void main(String[] args) {
    AnnotationConfigApplicationContext applicationContext = 
      new AnnotationConfigApplicationContext(Operations.class);

    applicationContext.registerShutdownHook();
  }
}

This is essentially what the Spring Boot, SpringApplication class does for you anyway.

If you are developing a Web application, then of course you can specify the type of ApplicationContext created since you are using Java config, for instance. See here for more details.

Hope this helps!

Cheers, John

John Blum
  • 7,381
  • 1
  • 20
  • 30