-1

I have posted my code and the errors below:

package com.example.chirag.numgame;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
TextView TV = (TextView)findViewById(R.id.TV);
TextView score = (TextView)findViewById(R.id.score);
Button upbutton = (Button)findViewById(R.id.upbutton);
Button downbutton = (Button)findViewById(R.id.downbutton);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView score = (TextView)findViewById(R.id.score);
    Button upbutton = (Button)findViewById(R.id.upbutton);
    Button downbutton = (Button)findViewById(R.id.downbutton);
    generate();
}
void generate()
{
    int number = (int)Math.random();
    TV.setText(number);
}
}

I cannot find the error in this code.

07-17 12:58:56.518 30381-30381/com.example.chirag.numgame I/Process: Sending signal. PID: 30381 SIG: 9 07-17 12:59:13.823 3894-3894/com.example.chirag.numgame D/AndroidRuntime: Shutting down VM 07-17 12:59:13.825 3894-3894/com.example.chirag.numgame E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.chirag.numgame, PID: 3894 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.chirag.numgame/com.example.chirag.numgame.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) 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:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:72) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183) at com.example.chirag.numgame.MainActivity.(MainActivity.java:9) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1089) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  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:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

halfer
  • 19,824
  • 17
  • 99
  • 186
Chirag
  • 149
  • 2
  • 17

2 Answers2

-1

Declare variable outside the method and bind the views in the onCreate() method:

public class MainActivity extends AppCompatActivity {
TextView TV;
TextView score;
Button upbutton;
Button downbutton;

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

    TV = (TextView)findViewById(R.id.TV);
    score = (TextView)findViewById(R.id.score);
    upbutton = (Button)findViewById(R.id.upbutton);
    downbutton = (Button)findViewById(R.id.downbutton);

    generate();
}
void generate()
{
    int number = (int)Math.random();
    TV.setText(number);
}
}
Harsh Pandey
  • 831
  • 7
  • 12
-1

You can't find a component in a view when the view has not been created. Also, I can see only the TV is used as global variable. So you can switch it to the code below, haven't tried but hopefully it should work.

public class MainActivity extends AppCompatActivity {
TextView TV;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TV = (TextView)findViewById(R.id.TV);
    TextView score = (TextView)findViewById(R.id.score);
    Button upbutton = (Button)findViewById(R.id.upbutton);
    Button downbutton = (Button)findViewById(R.id.downbutton);
    generate();
}
void generate()
{
    int number = (int)Math.random();
    TV.setText(number);
}}
bbofosu
  • 34
  • 4
  • I tried what you said, but the app on my phone just won't open. all it says is "Unfortunately yourapp has stopped". – Chirag Jul 17 '16 at 08:11