title: Android TextView next to ImageView set text programmatically causing a null when referencing
body: <p>Hey all I am trying to figure out how to put text into a <strong>TextView</strong> that's beside a <strong>ImageView</strong> so that I can label each image as this section of code does:</p>
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
      GlobalCls.hideSystemUI(getWindow());
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_main);
      GlobalCls._loadGlobalCls(getApplicationContext(), MainActivity.this);
      GlobalCls.database = openOrCreateDatabase(GlobalCls.dbName, Context.MODE_PRIVATE, null);
      GlobalCls.createSQLTbl();
  
      startLoading();
  }
  
  private void startLoading() {
    tabLayout = findViewById(R.id.mTabLayout);
    viewPager = findViewById(R.id.viewPager);
    //adapter setup
    pagerAdapter = new com.telluridetainment.PagerAdapter(getSupportFragmentManager());
    pagerAdapter.addFragment(new com.telluridetainment.FragMovies(), &quot;Movies&quot;);
    pagerAdapter.addFragment(new com.telluridetainment.FragShows(), &quot;Shows&quot;);
    pagerAdapter.addFragment(new com.telluridetainment.FragMusic(), &quot;Music&quot;);
    pagerAdapter.addFragment(new com.telluridetainment.FragAndroid(), &quot;Android&quot;)
    pagerAdapter.addFragment(new com.telluridetainment.FragGame2(), &quot;game1&quot;);
    pagerAdapter.addFragment(new com.telluridetainment.FragGame1(), &quot;game2&quot;);
  
    if (GlobalCls.theUser.equals(&quot;j&quot;)) {
      pagerAdapter.addFragment(new com.telluridetainment.FragUser(), &quot;j&quot;);
    } else {
      pagerAdapter.addFragment(new com.telluridetainment.FragUser(), &quot;t&quot;);
    }
  
    viewPager.setOffscreenPageLimit(7);
    viewPager.setAdapter(pagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
  
    for (int i = 0; i &lt; tabLayout.getTabCount(); i++) {
      TabLayout.Tab tab = tabLayout.getTabAt(i);
      //TextView t = findViewById(R.id.tabText);
  
      switch (i) {
      case 0:
        if (tab != null) {
          tab.setIcon(R.drawable.movies);
          //t.setText(&quot;Movies&quot;);
        }
        break;
      case 1:
        if (tab != null) {
          tab.setIcon(R.drawable.shows);
        }
        break;
      case 2:
        if (tab != null) {
          tab.setIcon(R.drawable.music);
        }
        break;
      case 3:
        if (tab != null) {
          tab.setIcon(R.drawable.android);
        }
        break;
      case 4:
        if (tab != null) {
          tab.setIcon(R.drawable.game1);
        }
        break;
      case 5:
        if (tab != null) {
          tab.setIcon(R.drawable.game2);
        }
        break;
      case 6:
        if (tab != null) {
          tab.setIcon(R.drawable.j);
        }
        break;
      }
  
      if (tab != null) tab.setCustomView(R.layout.my_custom_tab);
    }
  }
  </code></pre>
  <p>And the XML layout:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;ImageView
          android:id=&quot;@android:id/icon&quot;
          android:layout_width=&quot;50dp&quot;
          android:layout_height=&quot;50dp&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_centerVertical=&quot;true&quot;
          android:layout_marginStart=&quot;13dp&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/tabText&quot;
          style=&quot;@style/MyCustomTabText&quot;
          android:layout_width=&quot;92dp&quot;
          android:layout_height=&quot;50dp&quot;
          android:text=&quot;hi&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_centerVertical=&quot;true&quot;
          android:layout_marginStart=&quot;72dp&quot;
          android:maxHeight=&quot;20dp&quot;
          android:textColor=&quot;@color/white&quot; /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>The code works just fine. It places the image(s) where I need them with the text (currently only static text) beside it.</p>
  <p><a href="https://i.stack.imgur.com/ZfxGI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZfxGI.png" alt="enter image description here" /></a></p>
  <p>However, when I try the code</p>
  <pre><code>t.setText(&quot;Movies&quot;);
  </code></pre>
  <p>I know it finds the TextView because it has it's number when I hover over it:</p>
  <p><a href="https://i.stack.imgur.com/wPLse.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wPLse.png" alt="enter image description here" /></a></p>
  <p>But like I said above once it gets to setting the text it's 'null':</p>
  <p><a href="https://i.stack.imgur.com/SKulH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SKulH.png" alt="enter image description here" /></a></p>
  <p>Not sure what it is I'm doing incorrectly to cause it to be <code>null</code>?</p>
  <p><strong>UPDATE 1</strong></p>
  <p>Here is the error log that was requested:</p>
  <pre><code>D/AndroidRuntime: Shutting down VM
  E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.tridetainment, PID: 15100
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tridetainment/com.tridetainment.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
          at android.os.Handler.dispatchMessage(Handler.java:107)
          at android.os.Looper.loop(Looper.java:214)
          at android.app.ActivityThread.main(ActivityThread.java:7356)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
          at com.tridetainment.MainActivity.lambda$startLoading$3$com-tridetainment-MainActivity(MainActivity.java:213)
          at com.tridetainment.MainActivity$$ExternalSyntheticLambda3.run(Unknown Source:2)
          at android.app.Activity.runOnUiThread(Activity.java:6904)
          at com.tridetainment.MainActivity.startLoading(MainActivity.java:178)
          at com.tridetainment.MainActivity.onCreate(MainActivity.java:310)
          at android.app.Activity.performCreate(Activity.java:7802)
          at android.app.Activity.performCreate(Activity.java:7791)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
          at android.os.Handler.dispatchMessage(Handler.java:107) 
          at android.os.Looper.loop(Looper.java:214) 
          at android.app.ActivityThread.main(ActivityThread.java:7356) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
  I/Process: Sending signal. PID: 15100 SIG: 9
  Disconnected from the target VM, address: 'localhost:41902', transport: 'socket'
  </code></pre>
  <p><strong>UPDATE 2</strong></p>
  <p>I have added more code to the above to show the relationship(s).</p>
  <p><strong>UPDATE 3</strong></p>
  <p>Updated the code to the full call.</p>
  <p><strong>UPDATE 4</strong></p>
  <p>This is the <strong>main_activty</strong> XML:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:layout_x=&quot;10dp&quot;
      android:layout_y=&quot;100dp&quot;
      android:id=&quot;@+id/mainPage&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;com.tridetainment.noTabSwiping
          android:id=&quot;@+id/viewPager&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          android:tag=&quot;VP&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/mTabLayout&quot;
          tools:ignore=&quot;SpeakableTextPresentCheck&quot; /&gt;
  
      &lt;com.google.android.material.tabs.TabLayout
          android:id=&quot;@+id/mTabLayout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;70dp&quot;
          android:background=&quot;#F44336&quot;
          android:tag=&quot;TL&quot;
          android:elevation=&quot;0dp&quot;
          app:tabTextAppearance=&quot;@style/MyCustomTabText&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:tabGravity=&quot;fill&quot;
          app:tabIndicatorColor=&quot;@color/white&quot;
          app:tabMode=&quot;fixed&quot;
          app:tabSelectedTextColor=&quot;@color/white&quot;
          app:tabIndicatorHeight=&quot;10dp&quot;
          app:tabTextColor=&quot;@color/grey&quot;
          app:tabInlineLabel=&quot;true&quot;
          tools:ignore=&quot;SpeakableTextPresentCheck&quot; /&gt;
  
      &lt;View
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;15dp&quot;
          android:layout_below=&quot;@+id/mTabLayout&quot;
          android:layout_x=&quot;10dp&quot;
          android:layout_y=&quot;10dp&quot;
          android:background=&quot;@drawable/gui_tabshadow&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/viewPager&quot; /&gt;
  
      &lt;FrameLayout
          android:id=&quot;@+id/videoFrame&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:background=&quot;#122AFF&quot;
          android:tag=&quot;VF&quot;
          android:visibility=&quot;invisible&quot;&gt;
  
      &lt;/FrameLayout&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>