Good afternoon, there is such a problem:
public class QuizFragment extends Fragment { private static final String TAG = "CostumeQuiz Activity"; private static final int COSTUMES_IN_QUIZ = 10; private List<String> fileNameList; private List<String> quizWorldsList; private Set<String> worldsSet; private String correctAnswer; private int totalGuesses; private int correctAnswers; private int guessRows; private SecureRandom random; private Handler handler; private Animation shakeAnimation; private TextView questionNumberTextView; private ImageView costumeImageView; private LinearLayout[] guessLinearLayouts; private TextView answerTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ super.onCreateView(inflater,container,savedInstanceState); View view = inflater.inflate(R.layout.fragment_quiz,container,false); fileNameList = new ArrayList<String>(); quizWorldsList = new ArrayList<String>(); random = new SecureRandom(); handler = new Handler(); shakeAnimation = AnimationUtils.loadAnimation(getActivity(),R.anim.incorrect_shake); shakeAnimation.setRepeatCount(3); questionNumberTextView = (TextView) view.findViewById(R.id.questionNumberTextView); costumeImageView = (ImageView)view.findViewById(R.id.costumeImageView); guessLinearLayouts = new LinearLayout[3]; guessLinearLayouts[0] = (LinearLayout) view.findViewById(R.id.row1LinearLayout); guessLinearLayouts[1] = (LinearLayout) view.findViewById(R.id.row2LinearLayout); guessLinearLayouts[2] = (LinearLayout) view.findViewById(R.id.row3LinearLayout); answerTextView = (TextView) view.findViewById(R.id.answerTextView); for (LinearLayout row : guessLinearLayouts){ for (int column = 0;column<row.getChildCount();column++){ Button button = (Button)row.getChildAt(column); button.setOnClickListener(guessButtonListener); } } questionNumberTextView.setText(getResources().getString(R.string.question,1,COSTUMES_IN_QUIZ)); return view; } public void updateGuessRows(SharedPreferences sharedPreferences){ String choises = sharedPreferences.getString(mainActivity.CHOISES,null); if (choises != null){ guessRows = Integer.parseInt(choises)/3; choises = sharedPreferences.getString(mainActivity.CHOISES,null);} for (LinearLayout layout : guessLinearLayouts) layout.setVisibility(View.INVISIBLE); for (int row= 0;row<guessRows;row++) guessLinearLayouts[row].setVisibility(View.VISIBLE); } public void updateWorlds(SharedPreferences sharedPreferences){ worldsSet = sharedPreferences.getStringSet(mainActivity.COSTUMES,null); } public void resetQuiz(){ AssetManager assets = getActivity().getAssets(); fileNameList.clear(); try { for (String world : worldsSet){ String[] paths = assets.list(world); for (String path : paths) fileNameList.add(path.replace(".png","")); } } catch (IOException exeption){ Log.e(TAG,"Error loading image file names", exeption); } correctAnswers = 0; totalGuesses = 0; quizWorldsList.clear(); int costumeCounter = 1; int numberOfCostumes = fileNameList.size(); while (costumeCounter <= COSTUMES_IN_QUIZ){ int randomIndex = random.nextInt(numberOfCostumes); String fileName = fileNameList.get(randomIndex); if(!quizWorldsList.contains(fileName)){ quizWorldsList.add(fileName); ++costumeCounter; } } loadNextCostume(); } private void loadNextCostume(){ String nextImage = quizWorldsList.remove(0); correctAnswer = nextImage; answerTextView.setText(""); questionNumberTextView.setText(getResources().getString(R.string.question, (correctAnswers + 1), COSTUMES_IN_QUIZ)); String world = nextImage.substring(0,nextImage.indexOf('-')); AssetManager assets = getActivity().getAssets(); try { InputStream stream = assets.open(world + "/" + nextImage + ".png"); Drawable costume = Drawable.createFromStream(stream,nextImage); costumeImageView.setImageDrawable(costume); } catch (IOException exeption){ Log.e(TAG,"Error loading " + nextImage, exeption); } Collections.shuffle(fileNameList); int correct = fileNameList.indexOf(correctAnswer); fileNameList.add(fileNameList.remove(correct)); for (int row = 0; row < guessRows; row++){ for (int column = 0; column<guessLinearLayouts[row].getChildCount();column++){ Button newGuessButton = (Button) guessLinearLayouts[row].getChildAt(column); newGuessButton.setEnabled(true); String fileName = fileNameList.get((row*3)+column); newGuessButton.setText(getCostumeName(fileName)); } } int row = random.nextInt(guessRows); int column = random.nextInt(3); LinearLayout randomRow = guessLinearLayouts[row]; String costumeName = getCostumeName(correctAnswer); ((Button) randomRow.getChildAt(column)).setText(costumeName); } private String getCostumeName(String name){ return name.substring(name.indexOf('-')+1).replace('_',' ' ); } private View.OnClickListener guessButtonListener = new View.OnClickListener() { @Override public void onClick(View v) { Button guessButton = ((Button) v); String guess = guessButton.getText().toString(); String answer = getCostumeName(correctAnswer); ++totalGuesses; if (guess.equals(answer)) { ++correctAnswers; answerTextView.setText(answer + "!"); answerTextView.setTextColor(getResources().getColor(R.color.correct_answer)); disableButtons(); if (correctAnswers == COSTUMES_IN_QUIZ) { DialogFragment quizResults = new DialogFragment() { @Override public Dialog onCreateDialog(Bundle bundle) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setCancelable(false); builder.setMessage(getResources().getString(R.string.results, totalGuesses, (1000 / (double) totalGuesses))); builder.setPositiveButton(R.string.reset_quiz, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { resetQuiz(); } }); return builder.create(); } }; quizResults.show(getFragmentManager(), "quiz results"); } else { handler.postDelayed( new Runnable() { @Override public void run() { loadNextCostume(); } }, 2000); } } else { costumeImageView.startAnimation(shakeAnimation); answerTextView.setText(R.string.incorrect_answer); answerTextView.setTextColor(getResources().getColor(R.color.incorrect_answer)); guessButton.setEnabled(false); } } }; private void disableButtons(){ for (int row = 0;row <guessRows;row++){ LinearLayout guessRow = guessLinearLayouts[row]; for (int i = 0; i < guessRow.getChildCount();i++) guessRow.getChildAt(i).setEnabled(false); } } } Here is the error code, but I do not understand how to solve it:
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.hppavilion15.spidercostumequiz, PID: 9982 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hppavilion15.spidercostumequiz/com.example.Main.mainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.Set.iterator()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.Set.iterator()' on a null object reference at com.example.Main.QuizFragment.resetQuiz(QuizFragment.java:99) at com.example.Main.mainActivity.onStart(mainActivity.java:57) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) at android.app.Activity.performStart(Activity.java:6253) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
worldSet == nullYou do not initialize, or younullit. - Vladyslav Matviienko