-3

I have a problem with Android Studio. I'm trying to do a pretty simple app but on my phone (Galaxy S8) it always gives me the same error. And I use two computer it does the same at home and at school. When I use the emulator, everything's good.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – TofferJ Apr 19 '19 at 19:18

1 Answers1

0

Try this defensive programming code. This should avoid NPE problem, but the root reason are:

  1. You should refactor your game code with some Design Mode (try MVC at lease). You should not relay on the TextView String Value for game logic decision.

  2. Some UI Tree node maybe has been changed inside the loop.

public boolean checkEndOfGame(LinearLayout cartes, LinearLayout piles){
        if(nbCartes == 0){
            return true;
        }
        for (int i=0; i< piles.getChildCount(); i++){
            LinearLayout sorte = (LinearLayout)piles.getChildAt(i);
            for(int j=0; j< sorte.getChildCount(); j++){
                View sorteChild = sorte.getChildAt(j);
                if(sorteChild instanceof ConstraintLayout){
                    ConstraintLayout pile = (ConstraintLayout)sorteChild;
                    if(0 == pile.getChildCount) {
                        Log.e("TAG", "pile's child count = 0 ");
                        continue; //or break ?
                    }
                    View pileChild = pile.getChildAt(0);
                    if(pileChild instanceof TextView) {
                        TextView textePile = (TextView) pileChild;
                        int noPile = Integer.valueOf(textePile.getText());
                        for(int k=0; k< cartes.getChildCount(); k++){
                            View cartesChild = cartes.getChildAt(i);
                            if(cartesChild instanceof LinearLayout) {
                                LinearLayout rangee = (LinearLayout)cartesChild;
                                for(int l=0; l< rangee.getChildCount(); l++){
                                    View carteChild = rangee.getChildAt(l);
                                    if(carteChild instanceof ConstraintLayout) {
                                        ConstraintLayout carte = (ConstraintLayout)carteChild;
                                        View tv = carte.getChildAt(0);
                                        if(tv instanceof TextView) {
                                            TextView texteCarte = (TextView) tv;
                                            int noCarte = Integer.valueOf(texteCarte.getText());
                                            if(i>0){ //UP
                                                if(noCarte>noPile){
                                                    return false;
                                                }
                                            }else{ //DOWN
                                                if(noCarte<noPile){
                                                    return false;
                                                }
                                            }
                                        }

                                    }

                                }
                            }

                        }
                    }

                }
            }
        }
        return true;
    }
Qi Chen
  • 128
  • 1
  • 9