Alec
Alec

Reputation: 347

.equals(null) is probably not what was intended

I am trying to make a Calculator Application and i have a problem : the (null) is colored in orange and it says: .equals(null) is probably not what was intended.What should i do?

case R.id.equal: if(!optr.equals(null)){
                    if(op2 != 0){
                        if(optr.equals("+")){
                            disp.setText(""); /*op1 = op1 + op2;*/
                         disp.setText("Result : " + Integer.toString(op1));

Upvotes: 1

Views: 581

Answers (2)

cygery
cygery

Reputation: 2319

I assume you want to check if optr is either null or the empty String. In this case, use TextUtils.isEmpty():

case R.id.equal: if(!TextUtils.isEmpty(optr)){
                    if(op2 != 0){
                        if(optr.equals("+")){
                            disp.setText(""); /*op1 = op1 + op2;*/
                         disp.setText("Result : " + Integer.toString(op1));

Upvotes: 2

ligi
ligi

Reputation: 39539

this is how you do it. Null is not an object - so has no equals:

case R.id.equal: if(optr!=null){
                    if(op2 != 0){
                        if(optr.equals("+")){
                            disp.setText(""); /*op1 = op1 + op2;*/
                         disp.setText("Result : " + Integer.toString(op1));

Upvotes: 2

Related Questions