Auro
Auro

Reputation: 1628

Using service inside SpringBootApplication class throws NullPointerException

I'm quite new to SpringBoot. I have created a sample application with a service class.

Below is my SpringBootApplication class

@SpringBootApplication
public class SampleApplication {

    @Autowired
    static AWSService awsService;

    public static void main(String[] args) {

        SpringApplication.run(SampleApplication.class, args);
        awsService.getCertificate();  // Getting an NPE at this point

    }

}

The AWSService class

@Service
public class AWSService {

    public AWSService() {

    }

    private final Log log = new Log(getClass().getSimpleName());

    public void getCertificate() {
        String accessKey="";
        String secretKey="";
        try {
            Scanner awsCredentials = new Scanner(new File(Constants.AWS_CREDENTIALS));
            accessKey=awsCredentials.next();
            secretKey=awsCredentials.next();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey,secretKey);
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(
                new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
        S3Object s3object = s3Client.getObject(
                new GetObjectRequest(Constants.S3_BUCKET_NAME, Constants.S3_KEY_NAME));
        String temporaryCertificatePath = storeCertificate(s3object);
        Constants.setKeyStoreFile(temporaryCertificatePath);
    }

    private String storeCertificate(S3Object s3Object) {
        try {
            File certificate = File.createTempFile("signingKey",".p12");
            OutputStream outputStream = new FileOutputStream(certificate);
            byte buffer [] = IOUtils.toByteArray(s3Object.getObjectContent());
            outputStream.write(buffer);
            certificate.deleteOnExit();
            return certificate.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }
}

Following is the error I'm getting

Exception in thread "main" java.lang.NullPointerException
2017-02-27 13:24:04.023 at in.juspay.SampleApplication.main(SampleApplication.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 INFO   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
798 at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

I have Autowired the service in my application class, yet I'm getting a NullPointerException. If I understand Spring's @Service properly, the @Autowire should take care of the initialization of the object. Why then, am I getting an NPE at that point?

Upvotes: 0

Views: 521

Answers (2)

Emil Hotkowski
Emil Hotkowski

Reputation: 2343

You cannot autowire static fields. Use application context to reach it.

Can you use @Autowired with static fields?

Upvotes: 2

Bhavesh
Bhavesh

Reputation: 940

Very basic thing, static fields gets priority even before bean creation. That's why you can use static keyword to get a access of an instance.

refer this also Getting java.lang.NullPointerException when calling Method.invoke

Upvotes: 1

Related Questions