0

I get the error Variable might be initialized I am using onChildClickListener, which makes me declare the variable as final. I need to put my variable inside try-Catch that is why I need to declare the variable outside.

I can´t figure how to declare the variable to make this error disappear. Any idea?

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_balance, container, false);
    ExpandableListView expandableListView = (ExpandableListView)view.findViewById(R.id.fragBalance_expListView);
    final ArrayList<Balance> arrayList;
    try {
        arrayList = (ArrayList<Balance>) new BalanceController(new BalanceFromApiJson()).getBalance(getContext(),"http://10.113.10.10:50591/api/Balance?reservationID=1452455");
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    BalanceAdapter balanceAdapter = new BalanceAdapter(getContext(),arrayList);
    expandableListView.setAdapter(balanceAdapter);



    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Toast.makeText(getContext(),arrayList.get(groupPosition).getTransactions().get(childPosition).toString() , Toast.LENGTH_SHORT).show();
            return false;
        }
    });


    return view;
}
mavi
  • 1,074
  • 2
  • 15
  • 29
  • 2
    Just put `= null` after it. – Steve Smith Mar 22 '17 at 16:59
  • 1
    Possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Anton Balaniuc Mar 22 '17 at 17:00
  • If I do that It would appear another error that says "Cannot assign value to a final variable" – mavi Mar 22 '17 at 17:01
  • 1
    The way you've written it, if the line initialising your `arrayList` throws an exception, you want the method to still go on and try and run the rest of its code _without the arrayList_. I think the compile error is a hint that maybe your method ought to give up at that point and not try and use an arrayList that doesn't exist. – khelwood Mar 22 '17 at 17:02

2 Answers2

0

arrayList is not initialised if an exception is thrown during the construction of the BalanceController.

You then emit the possibility of an uninitialised arrayList being read after the try / catch block. That's not allowed in Java and compilation fails.

If the rest of the program is able to tolerate a null arrayList, then the easiest thing to do is to write arrayList = null in the catch handlers if you want to keep it final (let's use the multi-catch syntax to avoid repetition):

} catch (ExecutionException | InterruptedException e) {
    e.printStackTrace();
    arrayList = null;
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • If I do that another error appears **"Variable might already have been assigned to"** – mavi Mar 22 '17 at 17:09
  • I can fix that declaring another variable `final ArrayList arrayList; ArrayList arrayList1; try { arrayList1 = (ArrayList) new BalanceController(new BalanceFromApiJson()).getBalance(getContext(),"http://10.113.10.10:50591/api/Balance?reservationID=456465"); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); arrayList1 = null; } arrayList = arrayList1;` – mavi Mar 22 '17 at 17:10
0

Thanks for your answers but the solution that worked for my was declaring a second variable of the same type, add the operation that I needed and finally copying the object to the final ArrayList.

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_balance, container, false);
    ExpandableListView expandableListView = (ExpandableListView)view.findViewById(R.id.fragBalance_expListView);
    final ArrayList<Balance> arrayList;
    ArrayList<Balance> arrayList1 = null;
    try {
        arrayList1 = (ArrayList<Balance>) new BalanceController(new BalanceFromApiJson()).getBalance(getContext(),"http://10.113.10.10:50591/api/Balance?reservationID=4564548");
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
        //arrayList1 = null;
    }

    arrayList = arrayList1;
    if(arrayList1 != null){
        BalanceAdapter balanceAdapter = new BalanceAdapter(getContext(),arrayList);
        expandableListView.setAdapter(balanceAdapter);
    }



    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Toast.makeText(getContext(),arrayList.get(groupPosition).getTransactions().get(childPosition).toString() , Toast.LENGTH_SHORT).show();
            return false;
        }
    });


    return view;
}
mavi
  • 1,074
  • 2
  • 15
  • 29