Android Studio: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference [TextView]
<p>I have searched for an answer that works for me but have not come across anything that helped.</p>

<p>My problem is that I am linking a TextView to another activity, however when clicked on throws a NullPointerException</p>

<p><strong>This is the error:</strong></p>

<pre><code>01-18 07:03:41.882 14021-14035/com.j2fx.msc_driverlogin E/Surface:     getSlotFromBufferLocked: unknown buffer: 0xab7d7650
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: FATAL EXCEPTION: main
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: Process: com.j2fx.msc_driverlogin, PID: 14021
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.j2fx.msc_driverlogin/com.j2fx.msc_driverlogin.RegisterNew}:    java.lang.NullPointerException: Attempt to invoke virtual method 'void  android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a  null object reference
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
(quite a lot more stack trace but probably not relevant)
</code></pre>

<p>Here is the <strong>Login.class</strong></p>

<pre><code>package com.j2fx.msc_driverlogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends AppCompatActivity implements View.OnClickListener {

    Button bLogin;
    EditText etUsername, etPassword;
    TextView tvRegisterLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        bLogin = (Button) findViewById(R.id.bLogin);
        tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);

        bLogin.setOnClickListener(this);
        tvRegisterLink.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bLogin:
                // will add login code here
                break;
            case R.id.tvRegisterLink:
                startActivity(new Intent(this, Register.class));
                break;
        }
    }
}
</code></pre>

<p>Here is the <strong>Activity_Login.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:layout_height="match_parent" &gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Driver ID"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/etUsername"/&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:layout_marginBottom="10dp"
        android:id="@+id/etPassword"/&gt;
    &lt;Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/bLogin"/&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:text="Register new account"
        android:id="@+id/tvRegisterLink"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>Here is the <strong>Register.class</strong></p>

<pre><code>package com.j2fx.msc_driverlogin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Register extends AppCompatActivity implements View.OnClickListener {

    Button bRegister;
    EditText etName, etAddress, etDateOfBirth, etVehicleReg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etName = (EditText) findViewById(R.id.etName);
        etAddress = (EditText) findViewById(R.id.etAddress);
        etDateOfBirth = (EditText) findViewById(R.id.etDateOfBirth);
        etVehicleReg = (EditText) findViewById(R.id.etVehicleReg);

        bRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bRegister:


                break;
        }
    }
}
</code></pre>

<p>And finaly the <strong>Activity_Register.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:layout_height="match_parent" &gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/etName"/&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Address"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/etAddress"/&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date of Birth"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:layout_marginBottom="10dp"
        android:id="@+id/etDateOfBirth"/&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vehicle Reg"/&gt;

    &lt;EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_marginBottom="10dp"
        android:id="@+id/etVehicleReg"/&gt;

    &lt;Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/bRegister"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>I have checked thoroughly and cannot see where I have gone wrong, although I am new to this so possibly missing something trivial.</p>

<p>Some points:</p>

<ul>
<li><p>The id tvRegisterLink is within the same view</p></li>
<li><p>I have initialised tvRegisterLink before setOnClickListener</p></li>
<li><p>I have declared tvRegisterLink before the method onCreate</p></li>
<li><p>There is no duplication of the tvRegisterLink ID</p></li>
</ul>

<p>Any ideas or pointers in the right direction would be great !</p>

<p>Thanks.</p>