I am trying to create a session in an android app I am developing but the app crashes when it create creates the session.
Code:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                  Process: com.example.system2.tranxav, PID: 2460
                  java.lang.RuntimeException: An error occured while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:304)
                      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:818)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                      at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
                      at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
                      at com.example.system2.tranxav.Session.<init>(Session.java:16)
                      at com.example.system2.tranxav.SignUpUpload.doInBackground(SignUpUpload.java:105)
                      at com.example.system2.tranxav.SignUpUpload.doInBackground(SignUpUpload.java:28)
                      at android.os.AsyncTask$2.call(AsyncTask.java:292)
                      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:818) 
Code:
package com.example.system2.tranxav;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Created by SYSTEM2 on 30/11/2017.
 */

public class Session {
    private SharedPreferences prefs;
    SharedPreferences.Editor  editor;
    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
        editor = prefs.edit();
    }

    public void setusename(String email) {
        editor.putString("email", email).commit();
        editor.commit();
    }

    public String getusename() {
        String email = prefs.getString("email","");

        return email;
    }
}
package com.example.system2.tranxav;

/**
* Created by SYSTEM2 on 28/11/2017.
*/
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Random;

public class SignUpUpload extends AsyncTask<String, String, String>{
String firstname, lastname, email, phone, pswd;
Context context;
private Session session;//global variable


SignUpUpload(Context cxt){

}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String signup_url = "http://10.0.2.2:85/tranxav/signup.php";
if (type.equals("signup")){
try{
firstname = params[1];
lastname = params[2];
email = params[3];
phone = params[4];
pswd = params[5];

int min = 1;
int max = 9;

Random r = new Random();
int firstNum = r.nextInt(max - min + 1) + min;
int secondNum = r.nextInt(max - min + 1) + min;
int thirdNum = r.nextInt(max - min + 1) + min;
int fourthNum = r.nextInt(max - min + 1) + min;

StringBuilder sbs = new StringBuilder();
sbs.append("");
sbs.append(firstNum);
sbs.append(secondNum);
sbs.append(thirdNum);
sbs.append(fourthNum);
String vCode = sbs.toString();


URL url = new URL(signup_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String postData = URLEncoder.encode("firstname", "UTF-8")+"="+URLEncoder.encode(firstname, "UTF-8")+"&"+
URLEncoder.encode("lastname", "UTF-8")+"="+URLEncoder.encode(lastname, "UTF-8")+"&"+
URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "UTF-8")+"&"+
URLEncoder.encode("pswd", "UTF-8")+"="+URLEncoder.encode(pswd, "UTF-8")+"&"+
URLEncoder.encode("phone", "UTF-8")+"="+URLEncoder.encode(phone, "UTF-8")+"&"+
URLEncoder.encode("vCode", "UTF-8")+"="+URLEncoder.encode(vCode, "UTF-8")+"&"+
URLEncoder.encode("vCode", "UTF-8")+"="+URLEncoder.encode(vCode, "UTF-8");
bufferedWriter.write(postData);
bufferedWriter.flush();



String result = "";
String line = "";
int statusCode = httpURLConnection.getResponseCode();
if (statusCode == 200) {
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null){
sb.append(line).append("\n");
result = sb.toString();
}
if(bufferedWriter!=null)
{
bufferedWriter.close();
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
}
session = new Session(context); //in oncreate and now we set sharedpreference then use this like
session.setusename(email);

return result;
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}


}
return null;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}

@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}

}
[/CODE]
Code:
package com.example.system2.tranxav;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Random;

/**
 * Created by SYSTEM2 on 30/11/2017.
 */

public class VerifyCode extends AsyncTask<String, String, String> {
    AlertDialog alertDialog;
    ProgressDialog pd;
    Context context;
    private Session session;//global variable
    String email;
    VerifyCode(Context cxt){
        context = cxt;
    }

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection conn;
        String vCode1, vCode2, vCode3, vCode4;
        String type = params[0];
        String signup_url = "http://10.0.2.2:85/tranxav/VerifyCode.php";
        if (type.equals("verify")){
            try{

                int min = 1;
                int max = 9;

                vCode1 = params[1];
                vCode2 = params[2];
                vCode3 = params[3];
                vCode4 = params[4];

                Random r = new Random();
                int firstNum = r.nextInt(max - min + 1) + min;
                int secondNum = r.nextInt(max - min + 1) + min;
                int thirdNum = r.nextInt(max - min + 1) + min;
                int fourthNum = r.nextInt(max - min + 1) + min;

                session = new Session(context);
                email = session.getusename();
                URL url = new URL(signup_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String postData = URLEncoder.encode("vCode1", "UTF-8")+"="+URLEncoder.encode(vCode1,  "UTF-8")+"&"+
                        URLEncoder.encode("vCode2", "UTF-8")+"="+URLEncoder.encode(vCode2,  "UTF-8")+"&"+
                        URLEncoder.encode("vCode3", "UTF-8")+"="+URLEncoder.encode(vCode3,  "UTF-8")+"&"+
                        URLEncoder.encode("vCode4", "UTF-8")+"="+URLEncoder.encode(vCode4,  "UTF-8")+"&"+
                        URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email,  "UTF-8");
                bufferedWriter.write(postData);
                bufferedWriter.flush();


                String result = "";
                String line = "";
                int statusCode = httpURLConnection.getResponseCode();
                if (statusCode == 200) {
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                    StringBuilder sb = new StringBuilder();
                    while ((line = bufferedReader.readLine()) != null){
                        sb.append(line).append("\n");
                        result = sb.toString();
                    }
                    if(bufferedWriter!=null)
                    {
                        bufferedWriter.close();
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                }else{
                    System.out.print("Unsucessful");
                }
                return result;
            }catch (MalformedURLException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog(context);
        pd.setMessage("\tPlease Wait...");
        pd.setCancelable(false);
        pd.show();
    }
    @Override
    protected void onPostExecute(String result) {
        if(result.equalsIgnoreCase("true"))
        {

            Log.i("info", "This is what we like to show");
            Intent intent = new Intent(context,DashboardActivity.class);
            context.startActivity(intent);
            ((Activity)context).finish();

        }else if (result.equalsIgnoreCase("false")){
            Log.i("info", "This is false");

            Toast.makeText(context, "Invalid email or password", Toast.LENGTH_LONG).show();

        } else {
            Log.i("info", "exception");
            Toast.makeText(context, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();

        }
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }
}