Luciano Stupenengo
Luciano Stupenengo

Reputation: 183

File to inputStream for FTP upload returns NullPointerException

I'm trying to upload a file to FTP but have trouble with the inputStream. It returns java.lang.NullPointerException.

How do I load my file into the inputSteam?

Also I get no progress reports. The onProgressUpdate is never executed, why?

My Code

private class AsyncFTP extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {

            publishProgress("Entro");

            //Creo FTPClient
            FTPClient FTP = new FTPClient();
            //Defino datos del servidor
            String FTP_HOST = "NOT REAL IP";
            String FTP_USER = "NOT REAL USER";
            String FTP_PASS = "NOT REAL PASS";

            //Cargo el archivo
            File file = new File("/data/data/com.lstupenengo.mysql/files/xx.xx");
            FileInputStream FIS = null;
            try {
                FIS = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            publishProgress("File ");
            //Conectar
            try{
                publishProgress("Conectando");
                FTP.connect(FTP_HOST, 21);
                FTP.login(FTP_USER, FTP_PASS);
                publishProgress("Conecto " + String.valueOf(FTP.getReplyCode()));
                FTP.type(FTPClient.BINARY_FILE_TYPE);


                //Directorio
                FTP.changeWorkingDirectory("/public_html/");

                publishProgress("Directorio"+String.valueOf(FTP.getReplyCode()));
                FTP.enterLocalPassiveMode();
                FTP.storeFile("xx.xx",FIS);
                publishProgress("Subio "+String.valueOf(FTP.getReplyCode()));



            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(String txt) {
            TV.setText(TV.getText()+"\n"+txt);
        }

        @Override
        protected void onPostExecute(String result){
            TV.setText(TV.getText()+"\n Termino FTP");
        }
    }

Logcat

 OpenGL ES Shader Compiler Version: 17.01.12.SPL
    Build Date: 07/01/14 Tue
    Local Branch:
    Remote Branch: quic/kk_2.7_rb1.41
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.04.007.064 +  NOTHING
12-16 08:12:46.964    9262-9262/com.lstupenengo.mysql D/OpenGLRenderer﹕ Enabling debug mode 0
12-16 08:12:47.084    9262-9262/com.lstupenengo.mysql I/ActivityManager﹕ Timeline: Activity_idle id: android.os.BinderProxy@44bd9f08 time:324929151
12-16 08:12:55.142    9262-9262/com.lstupenengo.mysql W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
12-16 08:12:57.344    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND rw
12-16 08:12:57.534    9262-9262/com.lstupenengo.mysql I/ActivityManager﹕ Timeline: Activity_idle id: android.os.BinderProxy@44bd9f08 time:324939608
12-16 08:12:57.885    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND ro
12-16 08:12:57.995    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND rw
12-16 08:12:58.115    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, relatime] AND rw
12-16 08:12:58.215    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [ro, relatime] AND ro
12-16 08:12:58.756    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND ro
12-16 08:12:58.776    9262-9400/com.lstupenengo.mysql W/System.err﹕ java.io.FileNotFoundException: /data/data/com.lstupenengo.mysql/files/wa.db: open failed: EACCES (Permission denied)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:418)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:97)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:76)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.Posix.open(Native Method)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:402)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ ... 9 more
12-16 08:13:00.577    9262-9400/com.lstupenengo.mysql W/dalvikvm﹕ threadid=15: thread exiting with uncaught exception (group=0x41a32d88)
12-16 08:13:00.587    9262-9400/com.lstupenengo.mysql E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
    Process: com.lstupenengo.mysql, PID: 9262
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at org.apache.commons.net.io.Util.copyStream(Util.java:100)
            at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
            at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
            at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
            at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:117)
            at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:76)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

Upvotes: 0

Views: 719

Answers (1)

Jrm
Jrm

Reputation: 83

Your onProgressUpdate doesn't override, it needs as parameter (Params... param) and not a single String.

This is the definition.

protected void onProgressUpdate (Progress... values)

so you need

protected void onProgressUpdate (String... values)

and get your string with values[0] if you only put one string parameter in your publishProgress.

By the other hand, about your exception, it's hard to say without the stacktrace but check if your FIS is null before storing file. NullPointers are usually easy to fix. Anyway you have a small tutorial here

Upvotes: 1

Related Questions