title: SharedPreferences Java Android Studio
body: <p>I am trying to create a user session when they login/register so that they automatically go into the app when after they close it.</p>
  <p>I keep on getting this error:</p>
  <pre><code>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context 
  android.content.Context.getApplicationContext()' on a null object reference
      at com.example.myapplication.BackgroundWorker.&lt;init&gt;(BackgroundWorker.java:32)
      at com.example.myapplication.LoginActivity.OnLogin(LoginActivity.java:73)
      at java.lang.reflect.Method.invoke(Native Method) 
      at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener
  </code></pre>
  <p>I'm following this answer <a href="https://stackoverflow.com/a/11027631/14270746">https://stackoverflow.com/a/11027631/14270746</a></p>
  <p>I already created the login/registration script in php and Java so I'm trying to implement this stuff inside of the code I already created. I'm doing this inside of my BackgroundWorker.Java class. I tried to do it inside of my LoginActivity but I was getting the same error. Can someone help me ?</p>
  <pre><code>public class BackgroundWorker extends AsyncTask&lt;String, Void, String&gt; {
  
      Context context;
      AlertDialog alertDialog;
      BackgroundWorker(Context ctx) {
  
          context = ctx;
      }
  
      private SharedPreferences sharedpreferences;
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
      SharedPreferences.Editor editor = preferences.edit();
  
      @Override
      protected String doInBackground(String... params) {
          String type = params [0];
          String login_url = &quot;http://192.168.1.37/byebye/login_handler.php&quot;;
          String register_url = &quot;http://192.168.1.37/byebye/register_handler.php&quot;;
          if (type.equals(&quot;login&quot;)) {
  
              try {
                  String username = params [1];
                  String pw = params [2];
  
                  editor.putString(&quot;username&quot;, String.valueOf(username));
                  editor.apply();
  
                  URL url = new URL(login_url);
                  HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                  httpURLConnection.setRequestMethod(&quot;POST&quot;);
                  httpURLConnection.setDoOutput(true);
                  httpURLConnection.setDoInput(true);
                  OutputStream outputStream = httpURLConnection.getOutputStream();
                  BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, &quot;UTF-8&quot;));
                  String post_data = URLEncoder.encode(&quot;username&quot;, &quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(username, &quot;UTF-8&quot;)+&quot;&amp;&quot;
                          +URLEncoder.encode(&quot;pw&quot;, &quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(pw, &quot;UTF-8&quot;);
                  bufferedWriter.write(post_data);
                  bufferedWriter.flush();
                  bufferedWriter.close();
                  outputStream.close();
                  InputStream inputStream = httpURLConnection.getInputStream();
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, &quot;iso-8859-1&quot;));
                  String result = &quot;&quot;;
                  String line = &quot;&quot;;
  
                  while ((line = bufferedReader.readLine()) != null){
  
                      result += line;
                  }
  
                  bufferedReader.close();
                  inputStream.close();
                  httpURLConnection.disconnect();
                  return result;
  
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
          } else if (type.equals(&quot;register&quot;)) {
  
              try {
                  String name = params [1];
                  String username = params [2];
                  String email = params [3];
                  String pw = params [4];
  
                  URL url = new URL(register_url);
                  HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                  httpURLConnection.setRequestMethod(&quot;POST&quot;);
                  httpURLConnection.setDoOutput(true);
                  httpURLConnection.setDoInput(true);
                  OutputStream outputStream = httpURLConnection.getOutputStream();
                  BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, &quot;UTF-8&quot;));
  
                  String post_data = URLEncoder.encode(&quot;name&quot;, &quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(name, &quot;UTF-8&quot;)+&quot;&amp;&quot;
                          +URLEncoder.encode(&quot;username&quot;, &quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(username, &quot;UTF-8&quot;)+&quot;&amp;&quot;
                          + URLEncoder.encode(&quot;email&quot;, &quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(email, &quot;UTF-8&quot;)+&quot;&amp;&quot;
                          + URLEncoder.encode(&quot;pw&quot;, &quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(pw, &quot;UTF-8&quot;);
  
                  bufferedWriter.write(post_data);
                  bufferedWriter.flush();
                  bufferedWriter.close();
                  outputStream.close();
                  InputStream inputStream = httpURLConnection.getInputStream();
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, &quot;iso-8859-1&quot;));
                  String result = &quot;&quot;;
                  String line = &quot;&quot;;
  
                  while ((line = bufferedReader.readLine()) != null){
  
                      result += line;
                  }
  
                  bufferedReader.close();
                  inputStream.close();
                  httpURLConnection.disconnect();
                  return result;
  
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return null;
      }
  
      @Override
      protected void onPreExecute() {
          alertDialog = new AlertDialog.Builder(context).create();
          alertDialog.setTitle(&quot;Status&quot;);
      }
  
      @Override
      protected void onPostExecute(String result) {
          alertDialog.setMessage(result);
          alertDialog.show();
  
              Intent intent = new Intent(context.getApplicationContext(), ProfileActivity.class);
              context.startActivity(intent);
      }
  
      @Override
      protected void onProgressUpdate(Void... values) {
          super.onProgressUpdate(values);
      }
  
  }
  </code></pre>
  <p>This is the line of the first error(BackgroundWorker.java:32):</p>
  <pre><code>SharedPreferences preferences = 
  PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
  </code></pre>
  <p>And this is the second one:</p>
  <pre><code>BackgroundWorker backgroundWorker = new BackgroundWorker(this);
  </code></pre>
  <p>My guess is that the variables isn't saving to the sharedpref.</p>