0

So I'm trying to build a login app in Android Studio and when I test it the app crashes for some reason. I'm only decent at looking through the log to figure out the issue but I cannot do it for this exception error. Pasted below is my log.

02-09 16:18:09.482 2467-2467/? I/art: Not late-enabling -Xcheck:jni         (already on)
02-09 16:18:09.585 2467-2467/edu.dtcc.bwharto9.loginform W/System:  ClassLoader referenced unknown         path: /data/app/edu.dtcc.bwharto9.loginform-2/lib/x86
02-09 16:18:09.590 2467-2467/edu.dtcc.bwharto9.loginform D/AndroidRuntime: Shutting down VM
02-09 16:18:09.590 2467-2467/edu.dtcc.bwharto9.loginform E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.dtcc.bwharto9.loginform, PID: 2467
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.dtcc.bwharto9.loginform/edu.dtcc.bwharto9.loginform.MainActivity}:                
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at edu.dtcc.bwharto9.loginform.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) `

Here is also my Java

    package edu.dtcc.bwharto9.loginform;

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

    public class MainActivity extends AppCompatActivity {'

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

    EditText userName = (EditText)findViewById(R.id.userName);
    EditText password = (EditText)findViewById(R.id.password);
    public void login() {
        if (userName.getText().toString().equals("student") && password.getText().toString().equals("password"))
        {
            Toast.makeText(getApplicationContext(), "You have sucessfully logged in!", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Login error! Please check your username or password!", Toast.LENGTH_LONG).show();
        }
    }

}

XML

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.dtcc.bwharto9.loginform.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Please enter your username and Password "
        android:id="@+id/instructionsText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/userName"
        android:layout_below="@+id/instructionsText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:hint="Username" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/password"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:hint="Password"
        android:password="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log In"
        android:id="@+id/btnLogIn"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:onClick="login" />
</RelativeLayout>

Thanks to everyone who helps me out, I'm in a complete fix!

Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • You have a NULL Pointer, where you have not "findViewById" before referencing an item. – Booger Feb 09 '16 at 21:33
  • [See this answer](http://stackoverflow.com/questions/18880962/i-would-like-to-set-my-variables-at-the-top-of-my-class-instead-of-in-the-method/18881067#18881067) – codeMagic Feb 09 '16 at 21:39

3 Answers3

5
EditText userName = (EditText)findViewById(R.id.userName);
EditText password = (EditText)findViewById(R.id.password);

This will not work. You are calling findViewById() from a field initializer. You cannot reliably call methods inherited from Activity here in general. And, for findViewById(), you cannot get results until after setContentView() has been called.

Replace those lines with:

EditText userName;
EditText password;

Then, replace your onCreate() with:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userName = (EditText)findViewById(R.id.userName);
    password = (EditText)findViewById(R.id.password);
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

Move these lines inside the onCreate method

    EditText userName = (EditText)findViewById(R.id.userName);
    EditText password = (EditText)findViewById(R.id.password);

Somenthing like:

 public class MainActivity extends AppCompatActivity {'

    EditText userName;
    EditText password

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


    userName = (EditText)findViewById(R.id.userName);
    password = (EditText)findViewById(R.id.password);
   }

You are calling findViewById() from a field initializer and you can't do it.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Move the findViewById method calls in the onCreate after the setContentView method.

EditText userName;
EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userName = (EditText)findViewById(R.id.userName);
    password = (EditText)findViewById(R.id.password);
}
chris
  • 4,332
  • 5
  • 41
  • 61