-3

I am trying to create an app that requires a login. Just to test to see how it works, I made a login button to go to a separate layout where you press a button to randomize numbers. The login button works fine and it brings me to another layout, but it is when I click the button on the separate layout that my app crashes.

here is my code:

private TextView R1;
private TextView R2;
private TextView R3;
private TextView R4;
private TextView R5;
private TextView R6;
private Animation R1_roll_rotate;
private Animation R2_roll_rotate;
private Animation R3_roll_rotate;
private Animation R4_roll_rotate;
private Animation R5_roll_rotate;
private Animation R6_roll_rotate;
private Button Randomize_Button;
private Animation Button_Rotate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    Randomize_Button= (Button) findViewById(R.id.Randomize);
    R1= (TextView) findViewById(R.id.RandomOne);
    R2= (TextView) findViewById(R.id.RandomTwo);
    R3= (TextView) findViewById(R.id.RandomThree);
    R4= (TextView) findViewById(R.id.RandomFour);
    R5= (TextView) findViewById(R.id.RandomFive);
    R6= (TextView) findViewById(R.id.RandomSix);
    Button_Rotate= AnimationUtils.loadAnimation(Main_Activity.this, R.anim.Button_Rotate);
    R1_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomOne_roll_rotate);
    R2_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomTwo_roll_rotate);
    R3_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomThree_roll_rotate);
    R4_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFour_roll_rotate);
    R5_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFive_roll_rotate);
    R6_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomSix_roll_rotate);

public void onClick(View view) {
    int randomnum1 = r.nextInt(70);
    int randomnum2 = r.nextInt(70);
    int randomnum3 = r.nextInt(70);
    int randomnum4 = r.nextInt(70);
    int randomnum5 = r.nextInt(70);
    int randomnum6 = r.nextInt(27);
    int num1 = randomnum1;
    int num2 = randomnum2;
    int num3 = randomnum3;
    int num4 = randomnum4;
    int num5 = randomnum5;
    int num6 = randomnum6;
    String randomnum7 = String.valueOf(randomnum1);
    String randomnum8 = String.valueOf(randomnum2);
    String randomnum9 = String.valueOf(randomnum3);
    String randomnum10 = String.valueOf(randomnum4);
    String randomnum11 = String.valueOf(randomnum5);
    String randomnum12 = String.valueOf(randomnum6);
    try {
        Randomize_Button.startAnimation(Button_Rotate);
        R1.startAnimation(R1_roll_rotate);
        R2.startAnimation(R2_roll_rotate);
        R3.startAnimation(R3_roll_rotate);
        R4.startAnimation(R4_roll_rotate);
        R5.startAnimation(R5_roll_rotate);
        R6.startAnimation(R6_roll_rotate);*/


        for (int i = num1; i > 0 && i == num1; i++) {
            String ii = Integer.toString(i);
            R1.setText(ii);
        }
        for (int j = num2; j > 0 && j == num2; j++) {
            String jj = Integer.toString(j);
            R2.setText(jj);
        }
        for (int k = num3; k > 0 && k == num3; k++) {
            String kk = Integer.toString(k);
            R3.setText(kk);
        }
        for (int d = num4; d > 0 && d == num4; d++) {
            String dd = Integer.toString(d);
            R4.setText(dd);
        }
        for (int s = num5; s > 0 && s == num5; s++) {
            String ss = Integer.toString(s);
            R5.setText(ss);
        }
        for (int g = num6; g > 0 && g == num6; g++) {
            String gg = Integer.toString(g);
            R6.setText(gg);
        }
        for (int g = num1; g == num2; g++) {
            g = r.nextInt(70);
            String gg = Integer.toString(g);
            R1.setText(gg);
        }
        for (int d = num1; d == num3; d++) {
            d = r.nextInt(70);
            String dd = Integer.toString(d);
            R1.setText(dd);
        }
        for (int a = num1; a == num3; a++) {
            a = r.nextInt(70);
            String aa = Integer.toString(a);
            R1.setText(aa);
        }
        for (int w = num1; w == num4; w++) {
            w = r.nextInt(70);
            String ww = Integer.toString(w);
            R1.setText(ww);
        }
        for (int q = num1; q == num5; q++) {
            q = r.nextInt(70);
            String qq = Integer.toString(q);
            R1.setText(qq);
        }
        for (int y = num2; y == num3; y++) {
            y = r.nextInt(70);
            String yy = Integer.toString(y);
            R2.setText(yy);
        }
        for (int o = num2; o == num4; o++) {
            o = r.nextInt(70);
            String oo = Integer.toString(o);
            R2.setText(oo);
        }
        for (int p = num2; p == num5; p++) {
            p = r.nextInt(70);
            String pp = Integer.toString(p);
            R2.setText(pp);
        }
        for (int x = num3; x == num4; x++) {
            x = r.nextInt(70);
            String xx = Integer.toString(x);
            R3.setText(xx);
        }
        for (int c = num3; c == num5; c++) {
            c = r.nextInt(70);
            String cc = Integer.toString(c);
            R3.setText(cc);
        }
        for (int b = num4; b == num5; b++) {
            b = r.nextInt(70);
            String bb = Integer.toString(b);
            R4.setText(bb);
        }
    } catch (Exception e){

        Toast.makeText(Main_Activity.this,e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

Does anyone know why? If so, your help would be deeply appreciated! Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jonathania
  • 27
  • 7

2 Answers2

0

The possible reason what I can figure out reading your question is most probably you would have forgot the add the button in your another layout.

Since, the Button View would be null in this case so once you click, it would throw NullPointerException like this

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setEnabled(boolean)' on a null object reference
  1. First of all check in your another layout whether the button view is added or not.
  2. Then in Activity class check whether button view that you have initialized using the correct id which should match against the xml one.

These could be the reason for the crash you encountered.

Hope this helps.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
0

Add a try-catch block in button click like..

try{
//Your Code for randomize number.
}
catch(Exception e)
{
 Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
}

This will show exception in toast and your app won't crash.

Sachin Soma
  • 3,432
  • 2
  • 10
  • 18
  • the "context" is throwing an error "cannot resolve symbol 'context'" – Jonathania Jul 07 '18 at 06:36
  • Instead of context write YourActivityName.this – Sachin Soma Jul 07 '18 at 06:39
  • Okay. That worked now I have 2 errors one for all of my animations and one for all of my text views. First my animation one " Attempt to invoke virtual method 'void android.widget.Button.startAnimation(android.view. animation.Animation)' on a null object reference ". Now my text view error is " Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang. CharSequence)' on a null object reference " – Jonathania Jul 07 '18 at 07:08
  • Sorry for all of the questions I am new to android studio and programming in general. – Jonathania Jul 07 '18 at 07:08
  • you haven't initialized your button and textview. – Sachin Soma Jul 07 '18 at 07:11
  • Would there be any other reason, because I thought I had. – Jonathania Jul 07 '18 at 07:17
  • So do you have any idea what is wrong, or do you see something I have not initialized? – Jonathania Jul 07 '18 at 20:03
  • I Just Made a new project and placed the XML, and code for the randomize and it worked perfectly fine. – Jonathania Jul 07 '18 at 20:09