0

I am trying to create a login/register app, following the steps in this youtube tutorial https://www.youtube.com/watch?v=x0I5vJfaRIU The app should allow me to switch between Login page, Register page and the Main Activity which will allow me to log out. When I press the logout button it takes me to the login page as expected, when I click the TextView "tvRegisterHere" I should be taken to the register page, however the app simply stops running and this error message appears in logcat "Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference". I have declared both Login and Register in the Android Manifest. I have searched around for examples of the same problem but the solution to fix the others doesn't fix my issue. If anybody could please help me with my problem I would very much appreciate it.

Here are my different .java files

MainActivity.java

package com.example.john_000.ppmon;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button bLogout;
EditText etDisplayUsername, etDisplayCounty;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    bLogout = (Button)findViewById(R.id.bLogout);
    etDisplayCounty = (EditText)findViewById(R.id.etDisplayCounty);
    etDisplayUsername = (EditText)findViewById(R.id.etDisplayUsername);

    bLogout.setOnClickListener(this);
}

@Override
public void onClick(View v){
    switch(v.getId()){
        case R.id.bLogout:
            startActivity(new Intent(this, Login.class));
            break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Login.java

package com.example.john_000.ppmon;

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

/**
 * Created by jjohn_000 on 03/02/2016.
 */
public class Login extends ActionBarActivity implements View.OnClickListener{          

Button bLogin;
EditText etUsername, etPassword;
TextView tvRegisterHere;

@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);
    tvRegisterHere = (TextView)findViewById(R.id.tvRegisterHere);

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

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


            break;

        case R.id.tvRegisterHere:
            startActivity(new Intent(this, Register.class));
            break;
    }
}

Register.java

package com.example.john_000.ppmon;

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

/**
 * Created by jjohn_000 on 03/02/2016.
 */
public class Register extends ActionBarActivity implements         View.OnClickListener{

Button bRegister;
EditText etRegisterUsername, etRegisterPassword,etConfirmPassword, etCounty;

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

    etRegisterUsername = (EditText)findViewById(R.id.etUsername);
    etRegisterPassword = (EditText)findViewById(R.id.etRegisterPassword);
    etConfirmPassword = (EditText)findViewById(R.id.etConfirmPassword);
    etCounty = (EditText)findViewById(R.id.etCounty);
    bRegister = (Button)findViewById(R.id.bRegister);

    bRegister.setOnClickListener(this);
}

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


            break;
    }
}
}

logcat

02-03 15:22:47.821 6849-6849/? I/art: Late-enabling -Xcheck:jni
02-03 15:22:48.299 6849-6873/com.example.john_000.ppmon D/OpenGLRenderer:             Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-03 15:22:48.303 6849-6849/com.example.john_000.ppmon D/:     HostConnection::get() New Host Connection established 0xb42d77a0, tid 6849
02-03 15:22:48.319 6849-6849/com.example.john_000.ppmon D/Atlas: Validating     map...
02-03 15:22:48.439 6849-6873/com.example.john_000.ppmon D/libEGL: loaded     /system/lib/egl/libEGL_emulation.so
02-03 15:22:48.439 6849-6873/com.example.john_000.ppmon D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-03 15:22:48.448 6849-6873/com.example.john_000.ppmon D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
02-03 15:22:48.468 6849-6873/com.example.john_000.ppmon D/: HostConnection::get() New Host Connection established 0xaf039510, tid 6873
02-03 15:22:48.503 6849-6873/com.example.john_000.ppmon I/OpenGLRenderer:     Initialized EGL, version 1.4
02-03 15:22:48.591 6849-6873/com.example.john_000.ppmon D/OpenGLRenderer:     Enabling debug mode 0
02-03 15:22:48.631 6849-6873/com.example.john_000.ppmon W/EGL_emulation:     eglSurfaceAttrib not implemented
02-03 15:22:48.631 6849-6873/com.example.john_000.ppmon W/OpenGLRenderer:     Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035860, error=EGL_SUCCESS
02-03 15:22:53.308 6849-6873/com.example.john_000.ppmon W/EGL_emulation: eglSurfaceAttrib not implemented
02-03 15:22:53.308 6849-6873/com.example.john_000.ppmon W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb427f7a0, error=EGL_SUCCESS
02-03 15:22:53.582 6849-6873/com.example.john_000.ppmon D/OpenGLRenderer: endAllStagingAnimators on 0xb4198580 (RippleDrawable) with handle 0xb42d7ba0
02-03 15:22:54.462 6849-6849/com.example.john_000.ppmon D/AndroidRuntime: Shutting down VM
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: FATAL EXCEPTION: main
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: Process: com.example.john_000.ppmon, PID: 6849
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     java.lang.RuntimeException: Unable to start activity         ComponentInfo{com.example.john_000.ppmon/com.example.john_000.ppmon.Register}:     java.lang.NullPointerException: Attempt to invoke virtual method 'void     android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on     a null object reference
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:         at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method     'void     android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on     a null object reference
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at com.example.john_000.ppmon.Register.onCreate(Register.java:28)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

1 Answers1

0

I assume you are using wrong xml in Register activity. That's why you are not able to get R.id.bRegister which is causing NullPointerException.

setContentView(R.layout.activity_login); should be something like setContentView(R.layout.activity_register); in Register activity.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57