-4

I am a newbie and trying to make an android application for Online Voting. I have just designed the layouts so far but, unfortunately, I am facing rendering problems and the null pointer exception.

When I open an activity from MainActivity, I can only see a white screen! The sequence of activities is as follows:

MainActivity --> Instructions or AdminLogin --> UserLogin or RegisterCandidate

Even after hours of googling, I am not able to figure out the exact problem. I have checked several answers on StackOverflow itself but failed to figure out what's wrong.

The error trace is as follows:

E/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.palakjain.onlinevotingschool/com.example.palakjain.onlinevotingschool.AdminLogin}: java.lang.NullPointerException
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2360)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412)
      at android.app.ActivityThread.access$600(ActivityThread.java:162)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      at android.os.Looper.loop(Looper.java:194)
      at android.app.ActivityThread.main(ActivityThread.java:5388)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:525)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
      at com.example.suniljain.onlinevotingschool.AdminLogin.onCreate(AdminLogin.java:39)
      at android.app.Activity.performCreate(Activity.java:5141)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412) 
      at android.app.ActivityThread.access$600(ActivityThread.java:162) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
      at android.os.Handler.dispatchMessage(Handler.java:107) 
      at android.os.Looper.loop(Looper.java:194) 
      at android.app.ActivityThread.main(ActivityThread.java:5388) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
      at dalvik.system.NativeStart.main(Native Method)

MainActivity code:

public class MainActivity extends AppCompatActivity {

Button btnConductVoting, btnInstructions, btnSkipInstructions;

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

    btnConductVoting = (Button) findViewById(R.id.btnConductVoting);
    btnInstructions = (Button) findViewById(R.id.btnInstructions);
    btnSkipInstructions = (Button) findViewById(R.id.btnSkipInstructions);

    btnConductVoting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, AdminLogin.class);
            startActivity(i);
        }
    });

    btnInstructions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, Instructions.class);
            startActivity(i);
        }
    });

    btnSkipInstructions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, UserLogin.class);
            startActivity(i);
        }
    });

}
}

Instructions code:

public class Instructions extends AppCompatActivity{
Button btnStartVoting;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnStartVoting = (Button) findViewById(R.id.btnStartVoting);

    btnStartVoting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Instructions.this, UserLogin.class);
            startActivity(i);
        }
    });
}
}

AdminLogin code:

public class AdminLogin extends AppCompatActivity{

Button btnAdminLogin;
EditText etUsername;
EditText etPassword;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnAdminLogin = (Button) findViewById(R.id.btnAdminLogin);
    etUsername = (EditText) findViewById(R.id.etAdminPassword);
    etPassword = (EditText) findViewById(R.id.etAdminPassword);

    btnAdminLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(etUsername.getText().toString().equals("administrator") && etPassword.getText().toString().equals("password")){
                Intent i = new Intent(AdminLogin.this, RegisterCandidate.class);
                startActivity(i);
            }
        }
    });

}
}

UserLogin code:

public class UserLogin extends AppCompatActivity{

//TextView tvUserForgotPassword;
TextView tvNewUserSignUp;
//Button btnUserLogIn;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    tvNewUserSignUp = (TextView) findViewById(R.id.tvNewUserSignUp);        

    tvNewUserSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(UserLogin.this, NewUser.class);
            startActivity(i);
        }
    });

}
}

RegisterCandidate code:

public class RegisterCandidate extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}

NewUser code:

public class NewUser extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}

Update1: I have made all the changes that are suggested in the answers, still my application is crashing whenever I click "SKIP INSTRUCTIONS" button in the MainActivity. Also, app crashes when I click "Log-in" button from AdminLogin Activity. What should I do?

Update2: i referred What is a NullPointerException, and how do I fix it? yet couldn't fix the error!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Palak Jain
  • 17
  • 6

4 Answers4

1

You didn't setContentView in AdminLogin Activity.

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   // set contentview here before finding views otherwise they will be null
    btnAdminLogin = (Button) findViewById(R.id.btnAdminLogin);
   }

Just a note, same issue is with your other activities, cross check that.

If you want to know what is setContentView then in a short term it associate View/UI which will display when Activity launch. For an example - If you want to show EditText and Button on UI then they must be present in layout file which have .xml extension. (You can also create View through java code)

Rahul
  • 10,457
  • 4
  • 35
  • 55
1

You cannot compare Strings with == in your AdminLogin activity etUsername.getText().toString() == "administrator"

use

etUsername.getText().toString().equals("administrator")

also you didn't setContentView as stated in above answer

Manohar
  • 22,116
  • 9
  • 108
  • 144
1

as far as you are newbie on android you must read this documents to set setContentView

What is setContentView(R.layout.main)?

https://developer.android.com/training/basics/firstapp/building-ui.html

DolDurma
  • 15,753
  • 51
  • 198
  • 377
0

There is no back end and front end language for Android. Both are done using Java. The XML file is just a layout or skeleton for the activity. To use the activity one must use

setContentView(R.id.layout_id)
Praveen Thirumurugan
  • 977
  • 1
  • 12
  • 27