0
votes

please I need assistance with this issue. I am trying to upload files to remote server which is currently working fine. I am also able to save the users email to shared preference. But on trying to access the email in an async task. it keeps giving error of:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference $1.doInBackground(PASSMobileAudioService.java:165) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  at java.lang.Thread.run(Thread.java:818) 

Here is my code:

       public class PASSMobileAudioService extends Service implements MediaRecorder.OnInfoListener {

private static Context mContext;
public PASSMobileAudioService(Context mContext) {
    this.mContext = mContext;

}

        public void uploadAudio(final String existingFileName)  {
    this.mFileName = existingFileName;

    new AsyncTask<String, Void, Void>() {

        @Override
        protected Void doInBackground(String... params) {
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            String responseFromServer = "";
            String urlString = Constant.UPLOAD_AUDIO;
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            String email = prefs.getString(Constant.USER_EMAIL, "");

            try {
                //------------------ CLIENT REQUEST
                FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
                // open a URL connection to the Servlet
                URL url = new URL(urlString);
                // Open a HTTP connection to the URL
                conn = (HttpURLConnection) url.openConnection();
                // Allow Inputs
                conn.setDoInput(true);
                // Allow Outputs
                conn.setDoOutput(true);
                // Don't use a cached copy.
                conn.setUseCaches(false);
                // Use a post method.
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"userid=\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=US-ASCII" + lineEnd);
                dos.writeBytes("Content-Transfer-Encoding: 8bit" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(email + lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"sessionid=\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=US-ASCII" + lineEnd);
                dos.writeBytes("Content-Transfer-Encoding: 8bit" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(sessionid + lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"upload\";filename=\"" + existingFileName + "\"" + 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);
                // close streams
                Log.e("Debug", "File is written");
                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (MalformedURLException ex) {
                Log.e("Debug", "error: " + ex.getMessage(), ex);
            } catch (IOException ioe) {
                Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            }
            //------------------ read the SERVER RESPONSE
            try {
                inStream = new DataInputStream(conn.getInputStream());
                String str;
                while ((str = inStream.readLine()) != null) {
                    Log.e("Debug", "Server Response " + str);
                }
                inStream.close();
            } catch (IOException ioex) {
                Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }
            return null;
        }
    }.execute();
}
1

1 Answers

1
votes

Your root error message is Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference in PASSMobileAudioService.java:165

I am assuming that this line is:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);

Which, in this case, mContext is null.

You do not have any other context for this class, so that is all I can give you. Make sure your context is not null.