2

I am new in Android and have a problem with these method these methods to set default values use

 @SuppressLint("StringFormatMatches")
public void setTipValues() {

   tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
    tvBillTotalAmount.setText(getString(R.string.main_msg_billTotalResult,finalBillAmount));
    tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
}

and to calculate final bill

  private void CalculteFinalBill() {
    if(Percentage==0)
        Percentage=Default_Tip_Percent;
    if(!etBillAmount.getText().toString().equals("") && !etBillAmount.getText().toString().equals("."))

        totalBillAmount=Float.valueOf(etBillAmount.getText().toString());
    else
        totalBillAmount=0;
    tipTotal=(totalBillAmount*Percentage)/100;
    finalBillAmount=totalBillAmount+tipTotal ;
}

and i add these strings to strings.xml

<resources>
<string name="app_name">Tip Calculator</string>
<string name="main_msg_billAmount">Bill AMOUNT</string>
<string name="main_hint_billAmount">Enter Bill Amount Here</string>
<string name="main_msg_serviceRating">Service Rating</string>
<string name="main_msg_tipPercent">%s%% Tip Percent</string>
<string name="main_msg_tipTotal">$%s Tip Total</string>
<string name="main_msg_billTotal">Bill Total</string>
<string name="main_msg_billTotalResult">$%s</string>

the variables are

 TextView tvTipPercent;
TextView tvBillTotalAmount;
TextView tvTipAmount;
EditText etBillAmount;
float Percentage = 0;
float tipTotal = 0;
float finalBillAmount = 0;
float Regular_Tip_Percent = 10;
float Default_Tip_Percent = 15;
float Excellent_Tip_Percent = 20;
float totalBillAmount;
ImageButton Im1;
ImageButton Im2;
ImageButton Im3;

when i try to add setTipValues(); to Oncreate and run my app i have a fatal exception

03-23 08:56:00.768 1836-1836/com.example.tipcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tipcalculator/com.example.tipcalculator.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    at android.app.ActivityThread.access$600(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.example.tipcalculator.MainActivity.setTipValues(MainActivity.java:48)
    at com.example.tipcalculator.MainActivity.onCreate(MainActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

in line 48 which refer to `

 tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));

i Know butterKnife solve this annotation problem but how i can without using it

can someone help me?

    public class MainActivity extends AppCompatActivity {
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
float Percentage = 0;
float tipTotal = 0;
float finalBillAmount = 0;
float Regular_Tip_Percent = 10;
float Default_Tip_Percent = 15;
float Excellent_Tip_Percent = 20;
float totalBillAmount;
ImageButton Im1;
ImageButton Im2;
ImageButton Im3;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
final EditText etBillAmount=(EditText)findViewById(R.id.etBillAmount);
        final TextView tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
        final TextView tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
        final TextView tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);


        setTipValues();

    }


    @SuppressLint("StringFormatMatches")
    public void setTipValues() {

        tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
        tvBillTotalAmount.setText(getString(R.string.main_msg_billTotalResult, finalBillAmount));
        tvTipAmount.setText(getString(R.string.main_msg_tipTotal, tipTotal));
    }
}
Hamdy
  • 25
  • 8

3 Answers3

2

Welcome to SO, first off you should read how to ask; remember to never add images of code, but add the entire code to the answer, because it is really hard to replicate any problem having an image of it instead of the text (which can be copy-pasted).

Now, which is the line throwing error for you? MainActivity:java:48 refers to which line?

This error means you are trying to access a variable which is null

Please add the line 48 of your code which is throwing error; and have a look at What is NullPointerException

PS: This is not entirely an answer, but to help you I preferred add some hints here rather than posting a not formatted comment. Once you provide the needed informations I will improve this


EDIT after your new informations

I think your problems is in how you are initializing variables: in OnCreate method you call something like this:

    final EditText etBillAmount=(EditText)findViewById(R.id.etBillAmount);
    final TextView tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
    final TextView tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
    final TextView tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);

first off, you don't need the final attribute; but more important you are creating 4 new local variables, you are not using your class-scoped ones which are those:

TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;

if you want to use your class-variables you just need to call them without re-creating new ones, like this:

    etBillAmount=(EditText)findViewById(R.id.etBillAmount);
    tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
    tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
    tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);

as you can see, since you already created them in your activity, you just need to call them to get access.

That said, your error came off because you were not initializing the class-variables, but you were creating new ones. for this reason, the next time you try to access them, they are null.

Let me know if this helped you! Have a nice day

Pier Giorgio Misley
  • 4,977
  • 2
  • 20
  • 59
0

Use below code.

tvTipPercen.setText(getString(R.string.main_msg_tipPercent)+" "+Percentage);
tvBillTotalAmoun.setText(getString(R.string.main_msg_billTotalResult )+" "+finalBillAmount));
tvTipAmoun.setText(getString(R.string.main_msg_tipTotal)+" "+tipTotal));
AskNilesh
  • 58,437
  • 15
  • 99
  • 129
0

As an addition to Pier's answer with some Android basics, is tvTipPercent being initialized, for example tvTipPercent = findViewById(R.id.tip_percent); or something which is an assignment to those variables, before line 40 inside onCreate()?

If no (because you didn't post the full MainActivity.java), then it is very possible that the root reason is here. Assign them.

Geno Chen
  • 3,835
  • 6
  • 16
  • 32