Does not work Try invoking the virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

advertisements

I have searched my case but i cant find a proper answer im newbie here and its still not working so please help me...

Here is my code...

TextView TotalAmount;
int total_amount = 0;
RecyclerView dayrecyclerView;
List<Day_Details> DL;
Day_Adapter dayAdapter;
public static List<Day_Details> daysize;

 TotalAmount = (TextView) findViewById(R.id.event_total);
    TotalAmount.setText(String.valueOf(total_amount));
  public void Day(List<Day_Details> dyl) {
    //List<Day_Details>dayDetailsList

    Log.i("day response length", "dayresponse length " + dyl.size());

    if (dyl.size() > 0) {
        daysize = dyl;
        daylayout.setVisibility(LinearLayout.VISIBLE);
        DL = new ArrayList<>();
        dayAdapter = new Day_Adapter(this, DL);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        dayrecyclerView.setLayoutManager(layoutManager);
        dayrecyclerView.setItemAnimator(new DefaultItemAnimator());
        dayrecyclerView.setAdapter(dayAdapter);
        dayrecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        for (int i = 0; i < dyl.size(); i++) {
            String price = "₹ " + dyl.get(i).getDayPrice();
            Day_Details DD = new Day_Details(dyl.get(i).getId(),
            dyl.get(i).getDayTitle(), dyl.get(i).getDayDate(), price,
            dyl.get(i).getDayContent());
            Log.i(" Event_view ", "Day: Details "+DD.toString());
            DL.add(DD);
        }

        dayAdapter.notifyDataSetChanged();

    }

}
 public  void daychecker(Boolean ischecked,String id){

if(ischecked == true){
    for (int i = 0; i <daysize.size(); i++) {
        if(id == daysize.get(i).getId()){
           String price = daysize.get(i).getDayPrice();
           int amount = Integer.parseInt(price);
            total_amount = total_amount + amount;

        }
    }
    TotalAmount.setText("1234");
}else{
    for (int i = 0; i <=daysize.size(); i++) {
        if(id == String.valueOf(i)){
            String price = daysize.get(i).getDayPrice();
            int amount = Integer.parseInt(price);
            total_amount = total_amount - amount;
            TotalAmount.setText(String.valueOf(total_amount));
        }
    }
}
}

The days is in recyclerview when i check the checkbox it passes an id and price i have to show that price in that textview(TotalAmount) so when i click in checkbox it shows error as

04-25 11:27:29.366 9462-9462/com.appeonix.dithesh.doceventUI E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.appeonix.dithesh.doceventUI, PID: 9462
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                               at com.appeonix.doceventUI.Activity.UI.Event_View$override.daychecker(Event_View.java:258)
                                                                               at com.appeonix.doceventUI.Activity.UI.Event_View$override.access$dispatch(Event_View.java)
                                                                               at com.appeonix.doceventUI.Activity.UI.Event_View.daychecker(Event_View.java:0)
                                                                               at com.appeonix.doceventUI.Activity.Holder.Day_Holder$1.onClick(Day_Holder.java:39)
                                                                               at android.view.View.performClick(View.java:5184)
                                                                               at android.widget.CompoundButton.performClick(CompoundButton.java:128)
                                                                               at android.view.View$PerformClick.run(View.java:20910)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:145)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

here is my Oncreateview

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_view_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    sharedPreferences = getSharedPreferences("DocEventPreference", Context.MODE_PRIVATE);
    memid = sharedPreferences.getString("DocId", null);
    scrollView = (NestedScrollView) findViewById(R.id.Event_view_Scroll);
    //team
    teamrecyclerView = (RecyclerView) findViewById(R.id.team_recycler_view);
    //gallery
    galrecyclerView = (RecyclerView) findViewById(R.id.galleryrecycler_view);
    //day
    dayrecyclerView = (RecyclerView) findViewById(R.id.dayrecycler_view);

    //details
    About = (TextView) findViewById(R.id.abouttheevent);
    //qrcode
    imageView = (ImageView) findViewById(R.id.qrCode);
    //register
    TotalAmount = (TextView) findViewById(R.id.event_total);
    TotalAmount.setText(String.valueOf(total_amount));
    registerlay = (LinearLayout) findViewById(R.id.registerlayout);
    button = (Button) findViewById(R.id.Event_reg_confirm_btn);
    scrollView.post(new Runnable() {
        public void run() {
            scrollView.fullScroll(scrollView.FOCUS_UP);
        }
    });
    events_details = new Events_Details();
    events_details.setId(event_id);
    eventitle = (TextView) findViewById(R.id.Event_Name);
    Event_IMG = (ImageView) findViewById(R.id.backdrop);
    //LinearLayout
     eventgallerylayout = (LinearLayout)
    findViewById(R.id.EventView_Gallery);
    eventdetailslayout = (LinearLayout)
    findViewById(R.id.EventView_Details);
    daylayout = (LinearLayout) findViewById(R.id.EventDay_Details);
    eventteamlayout = (LinearLayout) findViewById(R.id.EventView_Team);
    eventQRlayout = (LinearLayout) findViewById(R.id.EventView_Qrcode);
    //        eventregisterlayout = (LinearLayout)
    findViewById(R.id.EventView_Register);
    eventdetailslayout.setVisibility(LinearLayout.GONE);
    button.setVisibility(View.GONE);
    eventteamlayout.setVisibility(LinearLayout.GONE);
    daylayout.setVisibility(LinearLayout.GONE);
    eventgallerylayout.setVisibility(LinearLayout.GONE);
    //        eventQRlayout.setVisibility(LinearLayout.GONE);
   //        eventregisterlayout.setVisibility(LinearLayout.GONE);
    SingleEventJavaWebservice webservice = new SingleEventJavaWebservice(
            Event_View.this, events_details, Event_View.this);
    webservice.execute();

    initCollapsingToolbar();
}

Thanks in advance...


@Dithesh Could you please check using debug that the TextView to which you are assigning value is initialized in that method. Also please check the String.valueOf(total_amount) returns a proper string value.