Hier ist mein Java-Code auszuführen:eine java.lang.NullPointerException bekommen beim Versuch, Datei
public class SimpleIntentService extends AsyncTask<String, Void, String> {
String upload;
public String uploadFiles(String sourceFileUri) {
upload=sourceFileUri;
return upload;
}
@Override
protected String doInBackground(String... params) {
try {
String sourceFileUri = upload;
int serverResponseCode;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (sourceFile.isFile()) {
try {
String upLoadServerUri = "http://raymartsison.x10host.com/uploadToServer.php";
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE",
"multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("bill", sourceFileUri);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
+ sourceFileUri + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math
.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,
bufferSize);
}
// send multipart form data necesssary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn
.getResponseMessage();
if (serverResponseCode == 200) {
// messageText.setText(msg);
//Toast.makeText(ctx, "File Upload Complete.",
// Toast.LENGTH_SHORT).show();
// recursiveDelete(mDirectory1);
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
}
// dialog.dismiss();
} // End else block
} catch (Exception ex) {
// dialog.dismiss();
ex.printStackTrace();
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
und ich halte diesen Fehler:
11-30 01:43:08.613 19483-19870/com.practice.raymart.practiceapplication
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual
method 'char[] java.lang.String.toCharArray()' on a null object reference
Bitte kann mir jemand helfen, im a Anfänger zu Java und Programmierung und ich habe versucht zu googeln und der Fehler zeigt immer noch. Ich erhalte Ausnahme bei dieser Zeile:
File sourceFile = new File(sourceFileUri);
Und es scheint, dass es nicht den Weg richtig ist immer das ist, warum ich diese Ausnahme erhalte. Jede Hilfe wird sehr geschätzt. Vielen Dank!
'sourceFileUri' ist anscheinend' null'. Rufen Sie 'uploadFiles()' auf? Übergeben Sie einen Nicht-'null'-Wert an' uploadFiles() '? – CommonsWare
von wo Sie sourceFileUri speichern? – Munir