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!