Legendary Lambe
Legendary Lambe

Reputation: 163

Need a error notification

Here is some of the main code for a little app I have made but there seems to be a runtime problem could someone please have a look and tell me exactly what the problem is:

// there are two button only one for exit and the other to calculate**
public class TimerCodeActivity extends Activity {

    protected Object timebox;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timerlayout);

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener(){

            @Override    
            public void onClick(View v) {
                finish();
                System.exit(0);}
        });    

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){    

            int digits = Integer.parseInt(((String) timebox).substring(0,2));
            int digits1 = Integer.parseInt(((String) timebox).substring(2,4));
            int digits2 = Integer.parseInt(((String) timebox).substring(4,6));
            int newnumber = ((digits + 777) + (digits1 * 16) + (digits2 * 3)) ;

            @Override
            public void onClick(View v) { 
                Toast.makeText(TimerCodeActivity.this, String.valueOf(newnumber), Toast.LENGTH_LONG).show();
            }
        });
    }
}

the Error Log cat as follows:

D/AndroidRuntime(539): Shutting down VM
W/dalvikvm(539): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
E/AndroidRuntime(539): FATAL EXCEPTION: main
E/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity        
ComponentInfo{com.Timer.Code/com.Timer.Code.TimerCodeActivity}:   
java.lang.NullPointerException
E/AndroidRuntime(539):  at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
E/AndroidRuntime(539):  at   
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
E/AndroidRuntime(539):  at   
android.app.ActivityThread.access$600(ActivityThread.java:122)
E/AndroidRuntime(539):  at    
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(539):  at  
android.app.ActivityThread.main(ActivityThread.java:4340)
E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(539):  at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(539):  at    
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(539): Caused by: java.lang.NullPointerException
E/AndroidRuntime(539):  at com.Timer.Code.TimerCodeActivity$2.<init>  
(TimerCodeActivity.java:32)
/AndroidRuntime(539):   at     
com.Timer.Code.TimerCodeActivity.onCreate(TimerCodeActivity.java:29)
E/AndroidRuntime(539):  at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(539):  at   
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(539):  at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
E/AndroidRuntime(539):  ... 11 more

Kindly tell me the problem...Thank you

Upvotes: 0

Views: 124

Answers (1)

Jordy Langen
Jordy Langen

Reputation: 3591

I'm guessing it's a NullPointerException?

You are initialising the digits with the timebox's value. But I don't see the timebox being initialised. I'm also wondering why timebox is an Object, since you are handling it as a String.

Note: The digits are also being initialised when you call the setOnClickListener, not at the OnClick method.

EDIT (After comment):

This should do the trick.

public class TimerCodeActivity extends Activity {

    protected TextView timebox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timerlayout);

        timebox = (TextView) findViewById(R.id.name_of_textview);

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener(){

            @Override    
            public void onClick(View v) {
                finish();
                System.exit(0);}
        });    

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){    
            @Override
            public void onClick(View v) { 
                String timeboxRepresentation = timebox.getText();
                int firstDigit = Integer.parseInt(timeboxRepresentation.substring(0,2));
                int secondDigit = Integer.parseInt(timeboxRepresentation.substring(2,4));
                int thirdDigit = Integer.parseInt(timeboxRepresentation.substring(4,6));
                int sum = ((firstDigit + 777) + (secondDigit * 16) + (thirdDigit * 3)) ;
                Toast.makeText(TimerCodeActivity.this, String.valueOf(sum), Toast.LENGTH_LONG).show();
            }
        });
    }
}

Upvotes: 1

Related Questions