Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPankaj Kumar
1,125 PointsNullPointerException: Attempt to invoke virtual method
I am getting a NullpointerException upon clicking any item in the list. I cant understand why its throwing a NullpointerException even if the if statement checks if savedInstanceState is null or not. Any help Would be Appreciated,thanks in advance. Here's my code from IngredientFragment class
package com.example.akshay.smellslikebakin;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class IngredientsFragment extends Fragment {
private static final String KEY_CHECKED_BOXES = "key_check_boxes";
private CheckBox[] mCheckBoxes;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
int index = getArguments().getInt(ViewPagerFragment.KEY_RECIPE_INDEX);
View view = inflater.inflate(R.layout.fragments_ingredients,container,false);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ingredients_layout);
String[] ingredients = Recipes.ingredients[index].split("`");
mCheckBoxes = new CheckBox[ingredients.length];
boolean[] checkedBoxes = new boolean[mCheckBoxes.length];
if(savedInstanceState != null && savedInstanceState.getBooleanArray(KEY_CHECKED_BOXES) != null);
{ checkedBoxes = savedInstanceState.getBooleanArray(KEY_CHECKED_BOXES);
}
setUpCheckBoxes(ingredients,linearLayout,checkedBoxes);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
boolean[] stateOfCheckBoxes = new boolean[mCheckBoxes.length];
int i=0;
for(CheckBox checkbox : mCheckBoxes)
{
stateOfCheckBoxes[i]=checkbox.isChecked();
i++;
}
outState.putBooleanArray(KEY_CHECKED_BOXES,stateOfCheckBoxes);
super.onSaveInstanceState(outState);
}
private void setUpCheckBoxes(String[] ingredients, ViewGroup container, boolean[] checkedBoxes){
int i=0;
for (String ingredient: ingredients) {
mCheckBoxes[i] = new CheckBox(getActivity());
mCheckBoxes[i].setPadding(8,16,8,16);
mCheckBoxes[i].setTextSize(20);
mCheckBoxes[i].setText(ingredient);
container.addView(mCheckBoxes[i]);
if(checkedBoxes[i]){
mCheckBoxes[i].toggle();
}
i++;
}
}
}
Nd here is the logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean[] android.os.Bundle.getBooleanArray(java.lang.String)' on a null object reference
at com.example.akshay.smellslikebakin.IngredientsFragment.onCreateView(IngredientsFragment.java:27)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
1 Answer
Javier Mera
16,472 PointsYou have a semicolon at the end of your if statement. I copy pasted your code and ran it and it fails with the semicolon, but passes once you remove it.
chandrashekhar singh
507 Pointschandrashekhar singh
507 PointsI am getting same problem