-2

I have a extended class, in class have a var, I define this by value, but say this is null !!!!

public class CalendarCell extends TextView
{

    public static final String TAG = "CalendarCell";

    protected ArrayList<Integer> cellStates = new ArrayList<>();

public CalendarCell(Context context)
{
    super(context);
}

public CalendarCell(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public CalendarCell(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
protected int[] onCreateDrawableState(int states)
{
    int newStatesSize = cellStates.size();  // Error here (null)
    if (newStatesSize > 0)
    {

        ...
    }
    else
    {
        return super.onCreateDrawableState(states);
    }
}

public void resetStates()
{
    cellStates.clear();
}

public void addState(int state)
{
    if(!cellStates.contains(state))
        cellStates.add(state);
    }
}

I defined 'cellStates' by value.but say this is null. Why???

java.lang.NullPointerException
 at yaran.CalendarSection.ViewSection.CalendarCell.onCreateDrawableState(CalendarCell.java:44)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ali
  • 11
  • 1

1 Answers1

0

Make cellStates final like so:

protected final ArrayList<Integer> cellStates = new ArrayList<>();
devgianlu
  • 1,547
  • 13
  • 25
  • I add final, but I get error so – ali Sep 02 '17 at 13:25
  • @ali It's pretty much impossible, a final field can't be changed and you're initializing it in declaration. There must be something else in the code that's not working. – devgianlu Sep 02 '17 at 13:27
  • When i add blow code ,its resolve problem. if(cellStates == null) cellStates = new ArrayList<>(); but for increment speed i want remove this pice – ali Sep 02 '17 at 14:32
  • 1
    @ali You must be missing something. Try debugging the code and checking for value assignation to your field. – devgianlu Sep 02 '17 at 15:25