title: Class.forName() keeps showing error: java.lang.NullPointerException
body: <p>I want to create a variable to contain an Activity.class so that I can dynamically change the Activity to be called with intent. I want the startBtn to start the intent to the last level played Activity which I will store in json format internally in the device. However, I keep showing up error when I try to use the forName() function to call the Activity class.</p>
  
  <p>Main Activity.class</p>
  
  <pre><code> public class MainActivity extends Activity {
  
       Button startBtn, chooseLevelBtn;
       Class classVariable;
  
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
  
           startBtn = (Button) findViewById(R.id.startBtn);
           chooseLevelBtn = (Button) findViewById(R.id.chooseLevelBtn);
  
           try {
               /*
                * Set class Variable so that I can dynamically change the Activity
                * to be called with intent.
                * Testing the variable with Level001Activity, but app keep crashing
                * when startBtn is pressed with stack error I post below
                */
               classVariable = Class.forName("com.ed.pieces.Level001Activity");
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
  
           startBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent i = new Intent(getApplicationContext(),
                      classVariable);
                   startActivity(i);
               }
           });
      }
  }
  </code></pre>
  
  <p>Error Stack</p>
  
  <blockquote>
    <blockquote>
      <p>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Class.getName()' on a null object reference
                  at android.content.ComponentName.(ComponentName.java:129)
                  at android.content.Intent.(Intent.java:4449)
                  at com.example.ed.pieces.MainActivity$1.onClick(MainActivity.java:35)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)</p>
    </blockquote>
  </blockquote>
  
  <p>P.S I will store the last level played information in json format as a string which is such as "com.ed.pieces.Level001Activity", and when the user start the app, the class classVariable should contain the Activity class of the last played level.</p>
  
  <p>All suggestions are greatly welcomed. Thank you in advance.</p>