Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:04+00:00 2026-05-28T05:42:04+00:00

My calculator program just doesnt seem to work. It crashes everytime i click on

  • 0

My calculator program just doesnt seem to work. It crashes everytime i click on a number button. Please help.Thanks in advance

The logcat report:

01-17 16:08:46.949: D/AndroidRuntime(321): Shutting down VM
    01-17 16:08:46.949: W/dalvikvm(321): threadid=1: thread exiting with uncaught     exception (group=0x4001d800)
    01-17 16:08:46.969: E/AndroidRuntime(321): FATAL EXCEPTION: main
    01-17 16:08:46.969: E/AndroidRuntime(321): java.lang.NullPointerException
    01-17 16:08:46.969: E/AndroidRuntime(321):  at net.learn.calc.CalciActivity.handlenumber(CalciActivity.java:189)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at net.learn.calc.CalciActivity$9.onClick(CalciActivity.java:110)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at android.view.View.performClick(View.java:2408)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at android.view.View$PerformClick.run(View.java:8816)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at android.os.Handler.handleCallback(Handler.java:587)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at android.os.Handler.dispatchMessage(Handler.java:92)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at android.os.Looper.loop(Looper.java:123)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    01-17 16:08:46.969: E/AndroidRuntime(321):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-17 16:08:46.969: E/AndroidRuntime(321):    at java.lang.reflect.Method.invoke(Method.java:521)
    01-17 16:08:46.969: E/AndroidRuntime(321):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    01-17 16:08:46.969: E/AndroidRuntime(321):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    01-17 16:08:46.969: E/AndroidRuntime(321):    at dalvik.system.NativeStart.main(Native Method)

here’s the program:

package net.learn.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CalciActivity extends Activity {
/** Called when the activity is first created. */
EditText ent=null;
Button a,b,c,d,e,f,g,h,i,j,add,sub,mul,div,equ,dot,cl,del;
double num=0;
int operator=1;
boolean clearrdy=false;
boolean numbpre=false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    a=(Button)findViewById(R.id.zero);
    b=(Button)findViewById(R.id.one);
    c=(Button)findViewById(R.id.two);
    d=(Button)findViewById(R.id.three);
    e=(Button)findViewById(R.id.four);
    f=(Button)findViewById(R.id.five);
    g=(Button)findViewById(R.id.six);
    h=(Button)findViewById(R.id.seven);
    i=(Button)findViewById(R.id.eight);
    j=(Button)findViewById(R.id.nine);
    add=(Button)findViewById(R.id.plus);
    sub=(Button)findViewById(R.id.minus);
    mul=(Button)findViewById(R.id.multiply);
    div=(Button)findViewById(R.id.divide);
    equ=(Button)findViewById(R.id.equal);
    dot=(Button)findViewById(R.id.decimal);
    cl=(Button)findViewById(R.id.clear);
    del=(Button)findViewById(R.id.delete);
    a.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handlenumber(0);
        }
    });
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handlenumber(1);
        }
    });
    c.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handlenumber(2);
        }
    });
    d.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handlenumber(3);
        }
    });
    e.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handlenumber(4);
        }
    });
  f.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handlenumber(5);
        }
    });
  g.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handlenumber(6);
    }
});
  h.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handlenumber(7);
    }
});
  i.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handlenumber(8);
    }
});
  j.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handlenumber(9);
    }
});
  add.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handleop(1);
    }
});
 sub.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handleop(2);
    }
});
 mul.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handleop(3);
    }
});
 div.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handleop(4);
    }
});
 equ.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handleop(0);
    }
});
 dot.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handledecimal();
    }
});
 cl.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        reset1();
    }
});
 del.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        backsp1();
    }
});

}
public void handlenumber(int n)
{
    String str=ent.getText().toString();
    if(clearrdy)
    {
        str="";
        clearrdy=false;
    }
    String val=Integer.toString(n);
   str=str+val;
   ent.setText(str);
   ent.setSelection(str.length());

   numbpre=true;
}
public void handledecimal()
{ String str=ent.getText().toString();
    if(clearrdy)
    {
        str="0.";

    }
    else
    {
        str=str+".";

    }
    ent.setText(str);
    ent.setSelection(str.length());
    clearrdy=false;
    numbpre=true;
}
public void handleop(int newop)
{
    if(numbpre)
    {
        switch(operator)
        {
        case 1:
        num=num + Double.parseDouble(ent.getText().toString());
        break;
        case 2:
        num=num - Double.parseDouble(ent.getText().toString());
        break;
        case 3:
        num=num * Double.parseDouble(ent.getText().toString());
        break;
        case 4:
        num=num/Double.parseDouble(ent.getText().toString());
        break;
        }
    }
    String str=Double.toString(num);
    ent.setText(str);
    ent.setSelection(str.length());
    numbpre=false;
    clearrdy=true;
    operator=newop;
}
public void reset1()
{
    numbpre=false;
    clearrdy=false;
    operator=1;
    num=0;
}
public void backsp1()
{
     String str=ent.getText().toString();
       if (str.length() > 0) {
        str=str.substring(0,str.length() - 1);
        ent.setText(str);
        ent.setSelection(str.length());

}
}
}


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/entry"
    android:layout_width="fill_parent"
    android:layout_height="84dp"
    android:layout_x="0dp"
    android:layout_y="29dp" >

    <requestFocus />
</EditText>

       <Button
           android:id="@+id/equal"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="250dp"
           android:layout_y="407dp"
           android:text="=" />

       <Button
           android:id="@+id/divide"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="180dp"
           android:layout_y="403dp"
           android:text="/" />

       <Button
           android:id="@+id/multiply"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="250dp"
           android:layout_y="324dp"
           android:text="X" />

       <Button
           android:id="@+id/nine"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="176dp"
           android:layout_y="322dp"
           android:text="9" />

       <Button
           android:id="@+id/seven"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="38dp"
           android:layout_y="322dp"
           android:text="7" />

       <Button
           android:id="@+id/eight"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="107dp"
           android:layout_y="322dp"
           android:text="8" />

       <Button
           android:id="@+id/zero"
           android:layout_width="58dp"
           android:layout_height="72dp"
           android:layout_x="107dp"
           android:layout_y="403dp"
           android:text="0" />

          <Button
              android:id="@+id/decimal"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="36dp"
              android:layout_y="403dp"
              android:text="." />

          <Button
              android:id="@+id/five"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="105dp"
              android:layout_y="247dp"
              android:text="5" />

          <Button
              android:id="@+id/four"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="36dp"
              android:layout_y="243dp"
              android:text="4" />

          <Button
              android:id="@+id/six"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="174dp"
              android:layout_y="247dp"
              android:text="6" />

          <Button
              android:id="@+id/minus"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="246dp"
              android:layout_y="245dp"
              android:text="-" />

          <Button
              android:id="@+id/three"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="174dp"
              android:layout_y="168dp"
              android:text="3" />

          <Button
              android:id="@+id/two"
              android:layout_width="59dp"
              android:layout_height="72dp"
              android:layout_x="104dp"
              android:layout_y="168dp"
              android:text="2" />

          <Button
              android:id="@+id/one"
              android:layout_width="57dp"
              android:layout_height="70dp"
              android:layout_x="40dp"
              android:layout_y="168dp"
              android:text="1" />

          <Button
              android:id="@+id/plus"
              android:layout_width="58dp"
              android:layout_height="72dp"
              android:layout_x="244dp"
              android:layout_y="170dp"
              android:text="+" />

                <Button
                    android:id="@+id/delete"
                    android:layout_width="88dp"
                    android:layout_height="wrap_content"
                    android:layout_x="194dp"
                    android:layout_y="116dp"
                    android:text="DEL" />


                <Button
                    android:id="@+id/clear"
                    android:layout_width="84dp"
                    android:layout_height="wrap_content"
                    android:layout_x="66dp"
                    android:layout_y="118dp"
                    android:text="C" />

 </AbsoluteLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:42:05+00:00Added an answer on May 28, 2026 at 5:42 am

    From what I can see, you don’t assign ent to any control (all you have is EditText ent=null;), so you get a null error when calling:

    String str=ent.getText().toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a small number-crunching program that basically loops over an N-dimensional grid
I have just made a program which calculates pi. However, even with 10 million
Let's say you have a small calculator program that takes numbers and an operator
I've just started learning Objective-C. I'm doing one of the standard calculator exercises. It's
I'm doing an expression valuation program, just like this . My problem is that
I am trying to make a simple calculator program where a user can opt
I want to print floats points in a simple calculator program for iOS: if
I am working on a program which needs to cube a number. Long story
So I'm trying to make a calculator app (just to get a hang of
I'm thinking of the case where the program doesn't really compute anything, it just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.