# Fehlermeldung, brauche Hilfe.



## AaronMaster (19. Sep 2014)

Ich kriege eine Java NullpointerException kann mir aber nicht erklären wieso. Dies geschieht laut LogCat in der fillTextView methode, wo ich den die methode setText aufrufe.

```
package tapthefrog.graphitentertainment.de.tapthefrog;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;



public class MyActivity extends Activity implements View.OnClickListener{

    private static final int FROG_ID = 3214565;
    private int points;
    private int round;
    private int countdown;
    private ImageView frog;
    private Random rnd = new Random();
    private Handler handler = new Handler();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            showStartFragment();
    }



    private void newGame() {
        points= 0;
        round=1;
        initRound();
    }




    private void initRound() {
        countdown= 10;
        ViewGroup container = (ViewGroup) findViewById(R.id.container);
        container.removeAllViews();
        WimmelView wv = new WimmelView(this);
        container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        wv.setImageCount(8 * (10 + round));
        frog = new ImageView(this);
        frog.setId(FROG_ID);
        frog.setImageResource(R.drawable.frog);
        frog.setScaleType(ImageView.ScaleType.CENTER);
        float scale = getResources().getDisplayMetrics().density;
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(Math.round(64*scale),Math.round(61*scale));
        lp.leftMargin = rnd.nextInt(container.getWidth()-64);
        lp.topMargin = rnd.nextInt(container.getHeight()-61);
        lp.gravity = Gravity.TOP + Gravity.LEFT;
        frog.setOnClickListener(this);
        container.addView(frog, lp);
        update();
        handler.postDelayed(runnable, 1000-round*50);
    }

    private void update() {
        fillTextView(R.id.points, Integer.toString(points)+" ");
        fillTextView(R.id.round, " "+Integer.toString(round));
        fillTextView(R.id.countdown, Integer.toString(countdown*1000)+" ");
    }

    private void fillTextView(int id, String text) {
        TextView tv = (TextView)findViewById(id);
        tv.setText(text);
    }






    private void showStartFragment(){
        ViewGroup container = (ViewGroup) findViewById(R.id.container);
        container.removeAllViews();
        container.addView(getLayoutInflater().inflate(R.layout.fragment_start, null));
        container.findViewById(R.id.start).setOnClickListener(this);
    }

    private void showGameOverFragment(){
        ViewGroup container = (ViewGroup) findViewById(R.id.container);
        container.addView( getLayoutInflater().inflate(R.layout.fragment_gameover, null) );
        container.findViewById(R.id.play_again).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.start){
            startGame();
        }
        else if(v.getId() == R.id.play_again) {
            showStartFragment();
        } else if(v.getId()==FROG_ID){
            handler.removeCallbacks(runnable);
            Toast.makeText(this, R.string.tapped, Toast.LENGTH_SHORT).show();
            points += countdown*1000;
            round++;
            initRound();
        }
    }

    private void startGame() {
        newGame();
    }
    private void countdown(){
        countdown--;
        update();
        if(countdown<= 0){
            frog.setOnClickListener(null);
            showGameOverFragment();
        } else {
            handler.postDelayed(runnable, 1000-round*50);
        }
    }

    private Runnable runnable = new Runnable(){
        @Override
        public void run(){
            countdown();
        }
    };

    @Override
    protected void onPause(){
        super.onPause();
        handler.removeCallbacks(runnable);
    }



}
```


8586-8586/tapthefrog.graphitentertainment.de.tapthefrog E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.fillTextView(MyActivity.java:79)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.update(MyActivity.java:71)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.initRound(MyActivity.java:66)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.newGame(MyActivity.java:42)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.startGame(MyActivity.java:117)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.onClick(MyActivity.java:103)


----------



## stg (19. Sep 2014)

Prüfe mal, was 
*(TextView)findViewById(id);*
tatsächlich zurückgibt.


----------



## JavaMeister (19. Sep 2014)

1. Schlechter Titel
2. Google mal den Fehler. Erscheint mir trivial.

Problem ist, dass man die NPE Fehler im Forum nie findet, weil die Titel dazu immer nix mit NPE zu tun haben.


----------

