【问题标题】:Null Pointer Exception Amazon S3 Bucket空指针异常 Amazon S3 存储桶
【发布时间】:2020-01-29 14:19:25
【问题描述】:

我正在尝试使用 AWS S3 开发工具包。我的项目结构是划分模块以获得更好的模块化。

我有 4 个文件

+-- S3Application.java
+-- S3InterfaceImpl.java
+-- S3Config.java
+-- S3CreateBucketClient.java\

以下是上述每个文件的代码:

public class S3Application{

private static S3InterfaceImpl S3InterfaceImpl;

public static void main(String[] args) throws IOException {

        try {
            System.out.println("Reached");
            S3InterfaceImpl.createBucket("hello123");
        } catch (Exception e) {
            System.out.println("Exception");
        }

    }
} 

我有 S3InterfaceImplcreateBucket 方法,然后调用 s3CreateBucketClient.createBucket

@Slf4j
@Component
@RequiredArgsConstructor
public class S3InterfaceImpl implements S3Interface {

    private Logger log;

    private S3CreateBucketClient s3CreateBucketClient;


    @Override
    public void createBucket(String bucketName) {
        // TODO Auto-generated method stub
        try {
            System.out.println("Reached");
            s3CreateBucketClient.createBucket(bucketName);
        } catch (Exception e) {
            log.error("Exception while creating bucket {}", bucketName, e);
            //throw new TechnicalException(ErrorCode.S3_PROCESSING_FAILED, e);
        }

    }
} 

这里有S3Config 来配置我的访问密钥

public class S3Config {

    private static final AWSCredentials credentials;
    static {
        //put your accesskey and secretkey here
        credentials = new BasicAWSCredentials(
                "<access-key>",
                "<secret-key>");
    };
    public AmazonS3 createS3Client() {

            AmazonS3 s3Client = AmazonS3ClientBuilder
                    .standard()
                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://192.168.44.225:9000","us-east-1"))
                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .withPathStyleAccessEnabled(false)
                    .build();

        return s3Client;
    }

} 

这里我有 S3CreateBucketClient 和 AWS 开发工具包方法。

@Component
@RequiredArgsConstructor
@Slf4j
public class S3CreateBucketClient {

    private Logger log;

    private S3Config S3Config = new S3Config();
    private final AmazonS3 s3Client = S3Config.createS3Client();

    public void createBucket(String bucketName) {
        boolean bucketExist = s3Client.doesBucketExistV2(bucketName);
        log.info("Bucket with name {} exist {}", bucketName, bucketExist);
        if(!bucketExist) {
            Bucket bucket = s3Client.createBucket(bucketName);
            if(bucket == null) {
                throw new RuntimeException("Failed to create bucket with name " + bucketName);
            }
        }
    }
} 

我在S3InterfaceImpl.createBucket("hello123"); 收到Exception in thread "main" java.lang.NullPointerException S3Application

【问题讨论】:

  • 您确定new BasicAWSCredentials() 在该行执行时已被调用?
  • 我要对代码进行哪些更改以反映这一点?

标签: amazon-web-services amazon-s3 nullpointerexception


【解决方案1】:

这似乎与 AWS 或 S3 无关,是您的 OOP 代码的问题。在尝试访问该变量/对象的成员之前,您必须将对象的实例分配给使用类类型声明的变量。

在 S3Application 中,您永远不会在调用 create 函数之前使用 S3InterfaceImpl 的实例实例化您的类中的 S3InterfaceImpl 属性。因此,如果您调试失败的代码行并检查 S3InterfaceImpl 变量,您应该会看到它有一个 NULL 值。当您尝试访问值为 NULL 的变量上的任何成员时,您会收到 NullPointerException。

你需要 S3InterfaceImpl = new S3InterfaceImpl() 或者你需要使用 DI 注入一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    相关资源
    最近更新 更多