1

I am trying to cause a new activity to start when a user clicks the logout button. However whenever I click on the button the app crashes and gives the calling setOnClickListener on a null object reference. The Java code is below

package hanyang.hyu_se;

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.ButtonBarLayout;
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 bNewGroup, bLogout;
EditText etSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    bNewGroup = (Button) findViewById(R.id.bNewGroup);
    bLogout = (Button) findViewById(R.id.bLogout);
    etSearch = (EditText) findViewById(R.id.etSearch);

    bLogout.setOnClickListener(this);
    bNewGroup.setOnClickListener(this);
}

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

            break;
        case R.id.bNewGroup:

            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);
}
}

At first my problem was that when I had manually typed in Button it didn't called the widget button but after addressing that issue I am now stuck on this null object reference.

I have checked my XML and the important part is...

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Logout"
    android:id="@+id/bLogout"
    android:layout_above="@+id/relativeLayout"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

Is this error occurring because the main activity is unable to locate the "bLogout" id? I have checked the syntax numerous times and I don't think that is the cause of the problem.

EDIT: Here is the error output. Sorry about the formatting... I tried to put it in a code section but it wouldn't let me add spaces to the front of the text for some reason...

--------- beginning of crash 06-03 14:44:55.744 2400-2400/hanyang.hyu_se E/AndroidRuntime: FATAL EXCEPTION: main Process: hanyang.hyu_se, PID: 2400 java.lang.RuntimeException: Unable to start activity
ComponentInfo{hanyang.hyu_se/hanyang.hyu_se.login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at hanyang.hyu_se.login.onCreate(login.java:31) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Wiguna R
  • 157
  • 5
  • 19
Josh L
  • 105
  • 1
  • 2
  • 10

0 Answers0