0

Every time I run this code in Java Android:

package com.example.basiccalculator;

import android.os.Bundle;


public class MainActivity extends Activity 

{

LinearLayout layout1;
TextView text1;
EditText number1text;
EditText number2text;
Button multiplyButton;
Button divideButton;
Button subtractButton;
Button addButton;
TextView answerText;

@Override
protected void onCreate(Bundle savedInstanceState) 

{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout1 = new LinearLayout(this);
    text1 = new TextView(this);
    number1text = new EditText(this);
    number2text = new EditText(this);
    multiplyButton = new Button(this);
    answerText = new TextView(this);
    divideButton = new Button(this);

    layout1.setOrientation(LinearLayout.VERTICAL);
    layout1.setGravity(Gravity.CENTER_HORIZONTAL);
    answerText.setWidth(150);
    answerText.setGravity(Gravity.CENTER_HORIZONTAL);

    answerText.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
    number1text.setInputType(InputType.TYPE_CLASS_NUMBER);
    number2text.setInputType(InputType.TYPE_CLASS_NUMBER);


    multiplyButton.setOnClickListener(multiplyClicked);
    divideButton.setOnClickListener(divideClicked);
    subtractButton.setOnClickListener(subtractClicked);
    addButton.setOnClickListener(addClicked);

    answerText.setText("0");
    addButton.setText("+");
    subtractButton.setText("-");
    multiplyButton.setText("X");
    divideButton.setText("/");

    layout1.addView(number1text);
    layout1.addView(number2text);
    layout1.addView(multiplyButton);
    layout1.addView(answerText);
    layout1.addView(divideButton);
    layout1.addView(addButton);
    layout1.addView(subtractButton);
    setContentView(layout1);

    number1text.setLayoutParams(new LinearLayout.LayoutParams(550,200));
    number2text.setLayoutParams(new LinearLayout.LayoutParams(550,200));
    multiplyButton.setLayoutParams(new LinearLayout.LayoutParams(250,250));
    divideButton.setLayoutParams(new LinearLayout.LayoutParams(250, 250));
    addButton.setLayoutParams(new LinearLayout.LayoutParams(250,250));
    subtractButton.setLayoutParams(new LinearLayout.LayoutParams(250, 250));


    //Catch methods

}

private OnClickListener multiplyClicked = new OnClickListener() {

    @Override
    public void onClick(View v) {
        String firststring = number1text.getText().toString();
        String secondstring = number1text.getText().toString();
        double firstnumber = Double.parseDouble(firststring);
        double secondnumber = Double.parseDouble(secondstring);
        double result = firstnumber * secondnumber;
        String resultString = String.valueOf(result);
        answerText.setText(resultString);

    }


};


private OnClickListener divideClicked = new OnClickListener() {

    @Override
    public void onClick(View v) {
        String firststring = number1text.getText().toString();
        String secondstring = number1text.getText().toString();
        double firstnumber = Double.parseDouble(firststring);
        double secondnumber = Double.parseDouble(secondstring);
        double result = firstnumber / secondnumber;
        String resultString = String.valueOf(result);
        answerText.setText(resultString);

    }


};

private OnClickListener subtractClicked = new OnClickListener() {

    @Override
    public void onClick(View v) {
        String firststring = number1text.getText().toString();
        String secondstring = number1text.getText().toString();
        double firstnumber = Double.parseDouble(firststring);
        double secondnumber = Double.parseDouble(secondstring);
        double result = firstnumber - secondnumber;
        String resultString = String.valueOf(result);
        answerText.setText(resultString);

    }


};

private OnClickListener addClicked = new OnClickListener() {

    @Override
    public void onClick(View v) {
        String firststring = number1text.getText().toString();
        String secondstring = number1text.getText().toString();
        double firstnumber = Double.parseDouble(firststring);
        double secondnumber = Double.parseDouble(secondstring);
        double result = firstnumber + secondnumber;
        String resultString = String.valueOf(result);
        answerText.setText(resultString);

    }


};





}

I keep getting these runtime errors in my console:

- 03-24 15:27:06.300: D/dalvikvm(913): Not late-enabling CheckJNI (already on)

- 03-24 15:27:10.110: D/AndroidRuntime(913): Shutting down VM
- 03-24 15:27:10.110: W/dalvikvm(913): threadid=1: thread exiting with uncaught exception (group=0xb1a18b90)
- 03-24 15:27:10.260: D/dalvikvm(913): GC_FOR_ALLOC freed 110K, 6% free 3272K/3452K, paused 62ms, total 69ms
- 03-24 15:27:10.280: E/AndroidRuntime(913): FATAL EXCEPTION: main
- 03-24 15:27:10.280: E/AndroidRuntime(913): Process: com.example.basiccalculator, PID: 913
- 03-24 15:27:10.280: E/AndroidRuntime(913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.basiccalculator/com.example.basiccalculator.MainActivity}: - java.lang.NullPointerException
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.ActivityThread.access$700(ActivityThread.java:135)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.os.Handler.dispatchMessage(Handler.java:102)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.os.Looper.loop(Looper.java:137)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.ActivityThread.main(ActivityThread.java:4998)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at java.lang.reflect.Method.invokeNative(Native Method)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at java.lang.reflect.Method.invoke(Method.java:515)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at dalvik.system.NativeStart.main(Native Method)
- 03-24 15:27:10.280: E/AndroidRuntime(913): Caused by: java.lang.NullPointerException
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at com.example.basiccalculator.MainActivity.onCreate(MainActivity.java:60)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.Activity.performCreate(Activity.java:5243)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
- 03-24 15:27:10.280: E/AndroidRuntime(913):    ... 11 more

But when I remove the catch methods, it seems to run fine. Does anyone know why I keep getting these runtime errors?

Chaos
  • 11,213
  • 14
  • 42
  • 69
SS149
  • 41
  • 5

2 Answers2

0

looks like you did not initialize your subtractButton before setting the OnClickListener here:

subtractButton.setOnClickListener(subtractClicked);

the same for the addButton.

==> calling methods on not initialized Objects will lead to a NullPointerException (NPE)

BTW: you may want to actually use the layout xml. I wonder why you never make use of findViewById method to look up elements inside the R.layout.activity_main ?

Community
  • 1
  • 1
donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • But I did initialize the Objects (addClicked and subtractClicked) on lines 35 and 36. – SS149 Mar 24 '14 at 21:22
  • you declared the fields `substractClicked` and `addClicked` - so far so good - but you did _not_ initialize them. You will need to do the same like you did with the other buttons `divideButton = new Button(this);` ...or even better use layout xml and look up your button with `findViewById` method. – donfuxx Mar 24 '14 at 21:51
  • It helps, but I cannot initialize them because it wouldn't make sense to convert a Button to a View.OnClickListener. There for, I am stuck. (But keep in mind I am just a beginner with making UI's.) – SS149 Mar 25 '14 at 18:31
  • I see my comment was confusing. (wanted to say `substractButton` and `addButton` ). So I don't want to convert your buttons to OnClickListeners. I wanted to say that you have to initialize your buttons _before_ you can attach the OnClickListener. So for example add the lines: `substractButton = new Button(this);` and `addButton = new Button(this);` right after your line `divideButton = new Button(this);` – donfuxx Mar 25 '14 at 18:40
0

Using the keyword this to reference to is just referencing the containing class. As stated above the standard way to initialize your ui elements by using findViewById, like so, subtractButton = (Button)findViewById(R.your_layout.buttonID);. You would then use the same syntax for every additional UI element you are working with.

And if you wanted to you could define a single OnClickHandler as an attribute to each of your buttons using onClick="myBtnHandler". Then you would wite the corresponding method in your code like so.

public void btnHandler(View v) { int btnId = v.getId(); switch(btnId) { case R.id.subtractBtn: { //Code Goes Here... } } }

Additional buttons would go inside the switch. This setup will help keep your code more organized.

gollum18
  • 182
  • 1
  • 8