title: How do you call a class (that's not an activity) in an activity class in Android Studio?
body: <p>I'm learning Android Studio and I decided to create a Java class and then call it in MainActivity. However, the app crashes on startup - see below. I just don't understand what the error means. Any thoughts?</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>   package com.example.daniel.hamblaster;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          generateText obj = new generateText();
          obj.generate();
  
      }
  }
  </code></pre>
  
  <p>Java class:</p>
  
  <pre><code>package com.example.daniel.hamblaster;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class generateText extends AppCompatActivity {
  
      Button myButton = (Button) findViewById(R.id.myButton);
  
      public void generate() {
          myButton.setOnClickListener(
              new Button.OnClickListener() {
                  public void onClick(View v) {
                      TextView myText = (TextView) findViewById(R.id.myText);
                      myText.setText("blablaba");
                  }
              }
          );
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.example.daniel.hamblaster, PID: 5560
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.daniel.hamblaster/com.example.daniel.hamblaster.MainActivity}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'android.view.Window$Callback android.view.Window.getCallback()' on a
    null object reference
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6119)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback
    android.view.Window.getCallback()' on a null object reference
                          at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:120)
                          at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:151)
                          at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:31)
                          at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:55)
                          at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:33)
                          at android.support.v7.app.AppCompatDelegateImplN.(AppCompatDelegateImplN.java:33)
                          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
                          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                          at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
                          at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
                          at com.example.daniel.hamblaster.generateText.(generateText.java:9)
                          at com.example.daniel.hamblaster.MainActivity.onCreate(MainActivity.java:14)
                          at android.app.Activity.performCreate(Activity.java:6679)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6119) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
    Application terminated.</p>
  </blockquote>