title: how to call an intent from a different class
body: <p>I am trying to call an intent from a handler and I don't know how to do it. I have tried many different ways but it does't seem to be working. I want to start the <code>PrimeNumbersActivity</code> when the progress dialog reaches its max.</p>
  
  <pre><code>public class MyHandler extends Handler {
      ProgressDialog progressDialog;
      Context context;
  
      public MyHandler(ProgressDialog progressDialog) {
          this.progressDialog = progressDialog;
      }
  
      public MyHandler(Context context){
          this.context = context;
      }
  
      @Override
      public void handleMessage(Message msg) {
          findPrimeNumber(Integer.parseInt(msg.obj.toString()));
      }
  
      public void findPrimeNumber(int number){
  
          ArrayList&lt;Integer&gt; primeNumbers = new ArrayList&lt;&gt;();
          boolean isPrimeNumber;
          PrimeNumbers primeNumbers1 = new PrimeNumbers();
  
  
  
  
          for(int i = 2; i &lt;= number; i++){
              isPrimeNumber = true;
              for(int j = 1; j &lt;= i; j++){
                  try {
                      sleep(50);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  if(j != 1 &amp;&amp; j != i){
                      if(i%j == 0){
                          isPrimeNumber = false;
                          break;
                      }
                  }
              }
              if(isPrimeNumber){
                  primeNumbers.add(i);
                  primeNumbers1.primeNumbers.add(i);
              }
              progressDialog.setProgress(i*100/number);
              if(progressDialog.getProgress() == progressDialog.getMax()){
                  progressDialog.dismiss();
                  Intent intent = new Intent(context.getApplicationContext(),PrimeNumbersActivity.class);
                  context.startActivity(intent);
              }
          }
  
          for (int i :
                  primeNumbers) {
              Log.d(TAG, i + "");
          }
      }
  }
  </code></pre>
  
  <p>the PrimeNumbersActivity</p>
  
  <pre><code>public class PrimeNumbersActivity extends AppCompatActivity {
  
      private PrimeNumbers primeNumbers = new PrimeNumbers();
      private ListView listView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_prime_numbers);
  
          listView = findViewById(R.id.listView);
  
          if(primeNumbers.primeNumbers.isEmpty()){
              Toast.makeText(getApplicationContext(),"Pas de nombres premiers",Toast.LENGTH_SHORT).show();
          }else{
              ArrayAdapter&lt;Integer&gt; arrayAdapter = new ArrayAdapter&lt;Integer&gt;(getApplicationContext(),android.R.layout.simple_list_item_1,primeNumbers.primeNumbers);
              listView.setAdapter(arrayAdapter);
          }
      }
  }
  </code></pre>
  
  <p>MainActivity</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      public static final int number = 20;
      public Button button;
      public EditText editText;
      public ProgressDialog progressDialog;
      public AlertDialog.Builder alertDialog;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          progressDialog = new ProgressDialog(MainActivity.this);
          progressDialog.setMax(100);
          progressDialog.setMessage("Please wait...");
          progressDialog.setTitle("Finding prime numbers");
          progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  
          final MyThread thread = new MyThread(progressDialog);
          thread.setName("MyThread");
          thread.start();
  
          button = (Button) findViewById(R.id.afficherNombrePremier);
          editText = (EditText) findViewById(R.id.editText);
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(editText.getText().toString().isEmpty()){
                      alertDialog = new AlertDialog.Builder(MainActivity.this);
                      alertDialog.setTitle("Warning");
                      alertDialog.setMessage("Veuillez saisir un nombre entier dans la zone de texte");
                      alertDialog.show();
                  }
                  else{
                      progressDialog.show();
                      Message message = Message.obtain();
                      message.obj = editText.getText().toString();
                      thread.mHandler.sendMessage(message);
                  }
  
              }
          });
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>04-24 00:45:15.457 4625-4712/com.example.mohammed.tdservicesex1 E/AndroidRuntime: FATAL EXCEPTION: MyThread
                                                                                Process: com.example.mohammed.tdservicesex1, PID: 4625
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                                    at com.example.mohammed.tdservicesex1.MyHandler.findPrimeNumber(MyHandler.java:65)
                                                                                    at com.example.mohammed.tdservicesex1.MyHandler.handleMessage(MyHandler.java:31)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at com.example.mohammed.tdservicesex1.MyThread.run(MyThread.java:22)
  </code></pre>
  
  <p>Thread:</p>
  
  <pre><code>public class MyThread extends Thread {
      private static final String TAG = MyThread.class.getSimpleName();
      public MyHandler mHandler;
      ProgressDialog progressDialog;
  
      public MyThread(ProgressDialog progressDialog) {
          this.progressDialog = progressDialog;
      }
  
      @Override
      public void run() {
          Looper.prepare();
          mHandler = new MyHandler(progressDialog);
          Looper.loop();
      }
  }
  </code></pre>