I want to build an app that takes input from EditText in a ListView. Basically, I have three columns and in each column I have ediText.Now when I try to get the input from the user and store it in a variable of type String and when I run this is the app, It crashes as it gets NullPointerException.
This is my MainActivity.java
package com.example.manuj.autocalc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText editTotal;
EditText editItem;
EditText editAmt;
String item;
String amt;
String total;
ListView listView;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editItem=findViewById(R.id.itemEdit);
editTotal=findViewById(R.id.editTotal);
editAmt=findViewById(R.id.amtEdit);
item=editItem.getText().toString();
total=editTotal.getText().toString();
amt=editAmt.getText().toString();
final ArrayList<Calc> calcs=new ArrayList<>();
calcs.add(new Calc(" -"," -"," -"));
calcs.add(new Calc(" -"," -"," -"));
calcs.add(new Calc(" -"," -"," -"));
calcs.add(new Calc(" -"," -"," -"));
calcs.add(new Calc(" -"," -"," -"));
listView=findViewById(R.id.listView);
customAdapter=new CustomAdapter(this,-1,calcs);
listView.setAdapter(customAdapter);
// Calc calc=new Calc(item,amt,total);
// calcs.add(calc);
customAdapter.notifyDataSetChanged();
}
}
This is my CustomAdapter class
package com.example.manuj.autocalc;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<Calc> {
public CustomAdapter(Context context, int resource, ArrayList<Calc> calcs) {
super(context, resource, calcs);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Calc calc = getItem(position);
if (view == null){
view = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
//get and set the items
EditText item=(EditText)view.findViewById(R.id.itemEdit);
item.setText(calc.getmItemEdit());
//get and set the amt
EditText amt=view.findViewById(R.id.amtEdit);
amt.setText(calc.getmAmtEdit());
//get and set the totalAmt
EditText total=view.findViewById(R.id.editTotal);
total.setText(calc.getmTotalEdt());
return view;
}
}
This is my model class
package com.example.manuj.autocalc;
import android.text.Editable;
import android.widget.EditText;
public class Calc {
private String mItemEdit;
private String mTotalEdt;
private String mAmtEdit;
Calc(String itemEdit,String totalEdit,String amtEdit){
this.mItemEdit=itemEdit;
this.mTotalEdt=totalEdit;
this.mAmtEdit=amtEdit;
}
public String getmItemEdit(){return mItemEdit; }
public void setmItemEdit(String mItemEdit){
this.mItemEdit=mItemEdit;
}
public String getmTotalEdt(){return mTotalEdt; }
public void setmTotalEdt(String mTotalEdt){
this.mTotalEdt=mTotalEdt;
}
public String getmAmtEdit(){return mAmtEdit;}
public void setmAmtEdit(String mAmtEdit){
this.mAmtEdit=mAmtEdit;
}
}
This is my Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.manuj.autocalc, PID: 6618
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manuj.autocalc/com.example.manuj.autocalc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.manuj.autocalc.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6956)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Application terminated.