java.lang.RuntimeException: Unable to start activity ComponentInfo Android Eclipse

11,282

Solution 1

line 60: x = (Integer) null; This line will compile to this bytecode (disassembled by javap):

  1. aconst_null
  2. checkcast #2; //class java/lang/Integer
  3. invokevirtual #3; //Method java/lang/Integer.intValue:()I

Third line will cause a NullPointerException becouse Integer object is actually your null constant :)

Primitive data types (int, long etc.) is the only non-object types in Java. null is used to show that the current variable (Object variable) is not backed by the actual object (no memory was allocated). For primitive types memory allocates immediately so they cant have this null state.

So you should check for "if(x == 0)" or define it as Integer.

P.S. And don't cast null to anything :)

Solution 2

It's like this

you're not getting much errors because the application can't launch. it cannot launch because it's onCreate() cannot finish.

onCreate() cannot finish because of a nullPointerException.

you cast null into integer twice, instead of simply instantiating a new integer which will default to 0. once you get rid of that, it should work.

see?

E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2071):     at
MainActivity.onCreate(MainActivity.java:60)

and i bet that this is line 60

x = (Integer) null;

so change it to

x = new Integer();
Share:
11,282

Related videos on Youtube

Chris
Author by

Chris

Updated on June 04, 2022

Comments

  • Chris
    Chris almost 2 years

    I know a bunch of people have asked this same thing but I just don't know what's going on. I'm trying to make a calculator in Eclipse, but I keep getting a list of errors. There are no errors in the file that the program notices, although there is an error in the layout.xml but it hasn't caused a problem before so that shouldn't cause a problem.

    07-30 08:19:50.470: D/AndroidRuntime(2071): Shutting down VM
    07-30 08:19:50.470: W/dalvikvm(2071): threadid=1: thread exiting with uncaught
    exception (group=0x40a421f8)
    07-30 08:19:50.480: E/AndroidRuntime(2071): FATAL EXCEPTION: main
    07-30 08:19:50.480: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start
    activity
    ComponentInfo{com.example.se.miun.chris.calculator/com.example.se.miun.chris.
    calculator.MainActivity}: java.lang.NullPointerException
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.ActivityThread.access$600(ActivityThread.java:123)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.os.Handler.dispatchMessage(Handler.java:99)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.os.Looper.loop(Looper.java:137)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.ActivityThread.main(ActivityThread.java:4424)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    java.lang.reflect.Method.invokeNative(Native Method)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    java.lang.reflect.Method.invoke(Method.java:511)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    dalvik.system.NativeStart.main(Native Method)
    07-30 08:19:50.480: E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    com.example.se.miun.chris.calculator.MainActivity.onCreate(MainActivity.java:60)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.Activity.performCreate(Activity.java:4465)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    07-30 08:19:50.480: E/AndroidRuntime(2071):     ... 11 more
    

    This is my coding. It doesn't really do anything yet, but I wanted to just run it to see if it encountered any errors. This is the mainActivity.java file.

    import android.os.Bundle;
    import android.app.Activity;
    import android.text.Editable;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener {
    Button Seven;
    Button Eight;
    Button Nine;
    Button Four;
    Button Five;
    Button Six;
    Button One;
    Button Two;
    Button Three;
    Button Zero;
    Button Point;
    Button Negative;
    TextView TextBox;
    int x;
    int y;
    String z;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Seven = (Button)findViewById(R.id.NumberSeven);
        Seven.setOnClickListener(this);
        Eight = (Button)findViewById(R.id.NumberEight);
        Eight.setOnClickListener(this);
        Nine = (Button)findViewById(R.id.NumberNine);
        Nine.setOnClickListener(this);
        Four = (Button)findViewById(R.id.NumberFour);
        Four.setOnClickListener(this);
        Five = (Button)findViewById(R.id.NumberFive);
        Five.setOnClickListener(this);
        Six = (Button)findViewById(R.id.NumberSix);
        Six.setOnClickListener(this);
        One = (Button)findViewById(R.id.NumberOne);
        One.setOnClickListener(this);
        Two = (Button)findViewById(R.id.NumberTwo);
        Two.setOnClickListener(this);
        Three = (Button)findViewById(R.id.NumberThree);
        Three.setOnClickListener(this);
        Zero = (Button)findViewById(R.id.NumberZero);
        Zero.setOnClickListener(this);
        Point = (Button)findViewById(R.id.Point);
        Point.setOnClickListener(this);
        Negative = (Button)findViewById(R.id.NNegative);
        Negative.setOnClickListener(this);
        TextBox = (TextView)findViewById(R.id.Screen);
        x = (Integer) null;
        y = (Integer) null;
    
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void onClick(View One) {
        if(z == null){
            x = 1;
                TextBox.setText(x);
                TextBox.setText("diggity");
            }
            else if(z != null) {
                y = 1;
                TextBox.setText(x);
                TextBox.setText(z);
                TextBox.setText(y);
            }
    
    
        }
    }
    
    • Shark
      Shark over 11 years
      you cannot cast null into objects lol :D
  • Chris
    Chris over 11 years
    Thank you, that worked. I have another question though,How can i make diffrent onClick methods for every button? I thought using One might work, but it the logcat says there's an error from inactivity. IF you can help, thanks.
  • Shark
    Shark over 11 years
    btnOne.setOnClickListener(new OnClickListener() { //ctrl+space or override onClick() }); repeat for each button.
  • Chris
    Chris over 11 years
    That worked too, thanks. But now I get this, 07-30 12:24:39.480: E/AndroidRuntime(3284): android.content.res.Resources$NotFoundException: String resource ID #0x5
  • Shark
    Shark over 11 years
    open up a new question, post stuff again, and i'll see if i can help you. even tho you didn't accept my answer and i'm the only one doing shit for you here :P