Solution 1 :
There seems to be a typo in your code. The edittext R.id.etQty
is nonexistent. The layout XML file has an EditText with the id tvQty
.
Solution 2 :
change id of EditText from “etQty” to “tvQty”
Problem :
I am getting the below Null pointer error. please help be debug this.
I am getting the below Null pointer error. please help be debug this.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.distributionlist.CreateNewOrder$1.afterTextChanged(CreateNewOrder.java:72)
at android.widget.TextView.sendAfterTextChanged(TextView.java:10551)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:13388)
This is my Layout: when I try to run the code it is giving me a nullpointerexception. Any help would be greatly appreciated.
I am getting the below Null pointer error. please help be debug this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_background="#ffffff"
android_orientation="vertical"
android_padding="10dp"
<ScrollView
android_layout_width="match_parent"
android_layout_height="match_parent">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_marginTop="20dp"
android_layout_marginBottom="10dp"
android_orientation="vertical">
<androidx.appcompat.widget.AppCompatSpinner
android_id="@+id/spItem"
android_layout_width="match_parent"
android_layout_height="50dp"
android_layout_gravity="top"
android_layout_marginTop="1dp"
android_layout_marginBottom="1dp"
android_background="@drawable/spinner_background" />
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_marginTop="10dp"
android_layout_marginBottom="10dp"
android_orientation="horizontal">
<EditText
android_id="@+id/tvQty"
android_layout_width="170dp"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginRight="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/spinner_background"
android_hint="Quantity #"
android_textSize="20sp" />
<androidx.appcompat.widget.AppCompatSpinner
android_id="@+id/spUnits"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginTop="5dp"
android_layout_marginRight="5dp"
android_layout_marginBottom="1dp"
android_layout_toRightOf="@id/etQty"
android_background="@drawable/spinner_background" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_marginTop="10dp"
android_layout_marginBottom="10dp"
android_orientation="horizontal">
<EditText
android_id="@+id/tvPrice"
android_layout_width="170dp"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginRight="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/spinner_background"
android_hint="Price per unit"
android_textSize="20sp" />
<EditText
android_id="@+id/tvSubtotal"
android_layout_width="match_parent"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/text_background"
android_enabled="false"
android_hint="Subtotal"
android_textSize="20sp" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_marginTop="10dp"
android_layout_marginBottom="10dp"
android_orientation="horizontal">
<androidx.appcompat.widget.AppCompatSpinner
android_id="@+id/spGstper"
android_layout_width="170dp"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginRight="8dp"
android_layout_marginBottom="1dp"
android_layout_toLeftOf="@+id/etGstamt"
android_background="@drawable/spinner_background" />
<EditText
android_id="@+id/tvGstamt"
android_layout_width="match_parent"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/text_background"
android_enabled="false"
android_hint="GST Amount"
android_textSize="20sp" />
</LinearLayout>
<EditText
android_id="@+id/tvTotalamt"
android_layout_width="match_parent"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/text_background"
android_enabled="false"
android_hint="Total Amount"
android_textSize="20sp" />
<EditText
android_id="@+id/tvAmtpaid"
android_layout_width="match_parent"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/spinner_background"
android_hint="Amount Paid"
android_textSize="20sp" />
<EditText
android_id="@+id/tvbalance"
android_layout_width="match_parent"
android_layout_height="55dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="1dp"
android_background="@drawable/text_background"
android_enabled="false"
android_hint="Amount Receivable"
android_textSize="20sp" />
<Button
android_id="@+id/btcreate"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginLeft="10dp"
android_layout_marginTop="25dp"
android_layout_marginRight="10dp"
android_layout_marginBottom="5dp"
android_background="#DDBB4D"
android_text="Create New Order"
android_textColor="#fff" />
<Button
android_id="@+id/btcancel"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginLeft="10dp"
android_layout_marginTop="25dp"
android_layout_marginRight="10dp"
android_layout_marginBottom="5dp"
android_background="#DDBB4D"
android_text="Cancel"
android_textColor="#fff" />
</LinearLayout>
</ScrollView>
</LinearLayout>
this is my Code:
I am getting the below Null pointer error. please help be debug this.
public class CreateNewOrder extends AppCompatActivity {
//declaring dropdowns
private Spinner spinner1, spinner2, spinner3;
//declaring Text fields
EditText txtSubtotal, txtGstamt, txtTotal, txtbalance, txtPrice, txtamtpaid, txtQty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new_order_pg);
//Hooks for txt fields
txtQty = findViewById(R.id.etQty);
txtPrice = findViewById(R.id.tvPrice);
txtSubtotal = findViewById(R.id.tvSubtotal);
txtGstamt = findViewById(R.id.tvGstamt);
txtTotal = findViewById(R.id.tvTotalamt);
txtamtpaid = findViewById(R.id.tvAmtpaid);
txtbalance = findViewById(R.id.tvbalance);
//Hook for subtotal Calculation
txtPrice.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
double pric = Double.parseDouble(txtPrice.getText().toString());
double quan = Double.parseDouble(txtQty.getText().toString());
double subtot = quan * pric;
txtSubtotal.setText((String.valueOf(subtot)));
}
});
Comments
Comment posted by Hulk
well, which line is line number 72? There seems to be a problem in
Comment posted by PM 77-1
R.id.etQty
Comment posted by sunil kumar
Facepalm… Thanks
Comment posted by Ranzz
@sunilkumar please accept the answer as correct if it has helped you 🙂
Comment posted by sunil kumar
Facepalm… Thanks