在java中将文件上载到amazon s3时出现空指针异常
当我在FileXfer类中调用函数fileUpload时(从main或从其他地方),它可以正常工作。但是,当我从外部调用此函数时,它会在upload=tx.upload(请求)以及从何处调用该函数时,给我一个空指针异常。 请告诉我解决这个问题的方法。我尝试在这个函数中使用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());
}
}}
# 1 楼答案
如果我不得不猜测,(您可能应该通过调试器运行代码),但是在获得NullPointerException的情况下,
tx
是null。这是因为tx
是一个在main中初始化的私有静态变量,但似乎没有在其他任何地方设置它。(这门课以外的任何地方都不能……这是私人的)我的建议是,将tx设为私有final(instance)变量,在构造时提供给
FileXfer
对象。(您也可以对许多其他变量执行相同的操作…遵循更多的Dependency Injection pattern而不是使用静态字段进行传递。您会发现您的代码更容易用这种方式进行推理)缩写代码: