-2

当我从 FileXfer 类(从主要或其他地方)调用函数 fileUpload 时,它工作正常。但是,当我从外部调用此函数时,它会在 upload=tx.upload(request) 以及调用该函数的任何位置给我一个空指针异常。请建议我解决这个问题的方法。我尝试在这个函数中使用 tx 定义,但它给出了一些其他错误。

import java.awt.BorderLayout;


import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
  import com.amazonaws.auth.AWSCredentials;
 import com.amazonaws.auth.PropertiesCredentials;
 import com.amazonaws.services.s3.model.GetObjectRequest;
   import com.amazonaws.services.s3.model.ProgressEvent;
     import com.amazonaws.services.s3.model.ProgressListener;
   import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
 import com.amazonaws.services.s3.transfer.Download;


public class FileXfer {


private static AWSCredentials credentials;
private static TransferManager tx;
private static String bucket;
private static String key;


private JProgressBar pb;
private JFrame frame;
private Upload upload;
private Download download;

public static void main(String[] args) throws Exception {
credentials = new PropertiesCredentials(FileXfer.class.getResourceAsStream("AwsCredentials.properties"));

    //transfer manager
 tx = new TransferManager(credentials);

 FileXfer upld = new FileXfer();

 // for testing
    //upld.createAmazonS3Bucket("aabrak12ujjia");
 upld.FileUpload("riti","D:/DM1/ec2trp.pem");
// upld.FileDownload("riti","ec2trp.pem","D:/DM1/"); 

  }

//for file download  
public void FileDownload( String Bname,String getkey, String destination) throws Exception {


    try{
        bucket=Bname; // bucket name

    key=getkey;  // file name to be downloaded

    frame = new JFrame("Saving a File");
    ProgressBar();
    ProgressTrackDownload progress = new ProgressTrackDownload();

    File fileToSave = new File(destination+key);

    GetObjectRequest request1 = new GetObjectRequest(bucket,key)
    .withProgressListener(progress.progressListener);
    download = tx.download(request1,fileToSave);

    System.out.println("Save as file: " + fileToSave.getAbsolutePath());

}catch (AmazonServiceException ase) {
    System.out.println("Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.");
    System.out.println("Error Message:    " + ase.getMessage());
    System.out.println("HTTP Status Code: " + ase.getStatusCode());
    System.out.println("AWS Error Code:   " + ase.getErrorCode());
    System.out.println("Error Type:       " + ase.getErrorType());
    System.out.println("Request ID:       " + ase.getRequestId());
} catch (AmazonClientException ace) {
    System.out.println("Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.");
    System.out.println("Error Message: " + ace.getMessage());
 }
}


 // for file upload
public void FileUpload(String bucket, String source) throws Exception {


    try{
         frame = new JFrame("Amazon S3 File Upload");

    File fileToUpload= new File(source);

    ProgressBar();
    ProgressTrackUpload pro = new ProgressTrackUpload(); 

    PutObjectRequest request = new PutObjectRequest(
            bucket, fileToUpload.getName(), fileToUpload).withProgressListener(pro.progressListener);

    upload = tx.upload(request);



}catch (AmazonServiceException ase) {
    System.out.println("Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.");
    System.out.println("Error Message:    " + ase.getMessage());
    System.out.println("HTTP Status Code: " + ase.getStatusCode());
    System.out.println("AWS Error Code:   " + ase.getErrorCode());
    System.out.println("Error Type:       " + ase.getErrorType());
    System.out.println("Request ID:       " + ase.getRequestId());
} catch (AmazonClientException ace) {
    System.out.println("Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.");
    System.out.println("Error Message: " + ace.getMessage());
  }
    }}
4

1 回答 1

0

如果我不得不猜测,(你可能应该通过调试器运行你的代码),但在你得到 NullPointerException 的情况下,tx它是空的。那是因为tx是一个在 main 中初始化的私有静态变量,但它似乎没有在其他任何地方设置。(这个班级以外的任何地方都不能......它是私人的)。

我的建议是使 tx 成为私有最终(实例)变量,FileXfer在构造时提供给对象。(您也可以对许多其他变量执行相同的操作......遵循更多的依赖注入模式,而不是使用静态字段传递。您会发现您的代码更容易以这种方式进行推理)。

在缩写代码中:

// or elsewhere of course...
public static void main(String... args) {
  TransferManager manager = new TransferManager(credentials);
  FileXfer upld = new FileXfer(manager);
  upld.fileUpload....
}

public class FileXfer {
  private final TransferManager tx;
  ...

  public FileXfer(TransferManager tx) {
    //ensure that tx is not null.
    if(tx == null) { throw new NullPointerException("tx"); }
    this.tx = tx;
  }

  ...
  public void FileUpload(...) {
    ...
    upload=tx.upload(request);
    ...
  }
}
于 2013-03-16T20:04:57.800 回答