在java中将文件上载到amazon s3时出现空指针异常
当我在FileXfer类中调用函数fileUpload时(从main或从其他地方),它可以正常工作。但是,当我从外部调用此函数时,它会在upload=tx.upload(请求)以及从何处调用该函数时,给我一个空指针异常。 请告诉我解决这个问题的方法。我尝试在这个函数中使用tx定义,但它给出了一些其他错误`在java中将文件上载到amazon s3时出现空指针异常,java,amazon-s3,Java,Amazon S3,当我在FileXfer类中调用函数fileUpload时(从main或从其他地方),它可以正常工作。但是,当我从外部调用此函数时,它会在upload=tx.upload(请求)以及从何处调用该函数时,给我一个空指针异常。 请告诉我解决这个问题的方法。我尝试在这个函数中使用tx定义,但它给出了一些其他错误` import java.awt.BorderLayout; import java.io.File; import javax.swing.BorderFactory; import j
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());
}
}}
如果我不得不猜测,(您可能应该通过调试器运行代码),但是在获得NullPointerException的情况下,tx
为null。这是因为tx
是一个在main中初始化的私有静态变量,但它似乎不在其他任何地方设置。(这门课以外的任何地方都不能……这是私人的)
我的建议是将tx设置为私有的final(instance)变量,在构建时提供给FileXfer
对象。(您也可以对许多其他变量执行相同的操作…遵循更多的a,而不是使用静态字段进行传递。您会发现,通过这种方式,您的代码更容易推理)
缩写代码:
// 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);
...
}
}
我不是落选者,但我明白他们的观点;StackOverflow不是一个调试工具。在发布之前,你应该尽量缩小问题的范围。通常,如果你把问题写得足够好,你会自己找到答案。所以你知道tx为null,你希望用户也能解决“其他一些错误”。这是一个问题,只需付出很少的努力,对依赖注入问题的理解就最终解决了。酷东西