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 8136697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:50:32+00:00 2026-06-06T10:50:32+00:00

I’m doing basic calculator for Android in Java. My calculator worked but i had

  • 0

I’m doing basic calculator for Android in Java. My calculator worked but i had all code in one class. Then i wanted to make code more readable and i created another Calculation class and i put calculation code in there. And now for some reason my app crashes. LogCat says: NullPointerException. (My app starts fine and then when i choose desirable currency to convert and when i click on ImageButton(convert) then app crashes). Here is my code:

CroToEu class:

package com.eu.calculator;


import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;



public class CroToEur extends Activity {

TextView resultView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cro_to_eur);

    final ImageButton convert = (ImageButton) findViewById(R.id.converButton);
    convertButton(convert);


}


private void convertButton(final ImageButton convert) {

    resultView = (TextView) findViewById(R.id.resultView);

    convert.setOnTouchListener(new View.OnTouchListener() {



        @Override
        public boolean onTouch(View v, MotionEvent event) {

            Calculate now = new Calculate();
            now.croToEu();


            if(event.getAction() == MotionEvent.ACTION_DOWN) {

                convert.setImageResource(R.drawable.convert_button_ontouch);   

                checkForEmptyEntry();

            } else if (event.getAction() == MotionEvent.ACTION_UP) {

                convert.setImageResource(R.drawable.convert_button);
            }
            return false;
        }



        private void checkForEmptyEntry() {


            if(Calculate.HRKfield.getText() == null || "".equals(Calculate.HRKfield.getText().toString())) {

                resultView.setText("You left empty field");
            } else {

            resultView.setText(Calculate.HRKfield.getText()+" HRK = "+Calculate.fixDecimal+" EUR");

            }
        }
    });
}  
}

And my calculation class:

package com.eu.calculator;


import java.math.BigDecimal;

import android.app.Activity;
import android.widget.EditText;




public class Calculate extends Activity {


public static EditText HRKfield;    //S tem dobimo vrednost iz polja edittext  
public static double EUR = 0.133;//drži vrednost
public static Double HRK;   // Možnost uporabe double za parsing
public static double result; // rezultat
public static BigDecimal fixDecimal; // rezultat pretvori na decimalko



public BigDecimal croToEu() {

HRKfield = (EditText) findViewById(R.id.enterField);



    try {
        HRK = Double.parseDouble(HRKfield.getText().toString()); //tukaj dobimo čisto številko, ki jo uporabnik vnese v polje
        result = HRK * EUR;
        fixDecimal = new BigDecimal(result);
        fixDecimal = fixDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);

    } catch (NumberFormatException e) {

    }
    return fixDecimal;
}

}
  • 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-06-06T10:50:33+00:00Added an answer on June 6, 2026 at 10:50 am

    If you are trying to just create a helper class whcih does the calculation for you then don’t extend Activity on your Calculate class. Instead get your croToEu method to return a variable and call this from the other class as follows.

    Calculate now = new Calculate();
    BigDecimal val = now.croToEu();
    

    Id actually have the caluclate class as follows

    public abstract class Calculate {
    
        public static final double EUR = 0.133;//drži vrednost
    
        public static BigDecimal croToEu(double hrkValue) {
    
            BigDecimal fixDecimal = new BigDecimal(hrkValue * EUR);
            fixDecimal = fixDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    
            return fixDecimal;
        }
    
    }
    

    Then in your main activity class call

    BigDecimal val = Calculate.croToEu(hrkValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.