1

I'm learning Android development and I'am currently developing a game where little flys appear on the screen and the user has to click on them so they dissapear. But my method for despawning them just gives me this error: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference

 private void removeFlys() {
        int number=0;
        while(number < playarea.getChildCount()) {
            ImageView muecke = (ImageView) playarea.getChildAt(number);
            Date birthdate = (Date) muecke.getTag(R.id.birthdate);
            long age = (new Date()).getTime() - birthdate.getTime();
            if(alter > 2000) {
                playarea.removeView(muecke);
            } else {
                number++;
            }
        }
    }

this is the xml file where the birthdate tag is defined

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name = "birthdate" type ="id" />
</resources>

This is my code to spawn the flys

private void spawnFlys() {
        int width = playarea.getWidth();
        int heigth = playarea.getHeight();
        int muecke_width = Math.round(x * 50);
        int muecke_heigth = Math.round(x * 42);
        int left = randomgenerator.nextInt(width - muecke_width);
        int top = randomgenerator.nextInt(heigth - muecke_heigth);


    ImageView muecke = new ImageView(this);
    muecke.setImageResource(R.drawable.fliege);
    muecke.setOnClickListener(this);


    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(muecke_width, muecke_heigth);
    params.leftMargin = left;
    params.topMargin = top;
    params.gravity = Gravity.TOP + Gravity.LEFT;
    playarea.addView(muecke, params);
    muecke.setTag(R.id.birthdate, new Date());
}

And this is where the spawnFlys method gets called

private void timer(){
    time = time -1;
    float randomNumber = randomgenerator.nextFloat();
    double probabilitiy = muecken * 1.5;
    if(probability > 1) {
        spawnFlys();
        if(randomNumber < probability -1) {
            spawnFlys();
        }
    } else {
        if (randomNumber < probability) {
            spawnFlys();
        }
    }
    removeFlys();
    updateScreen();
    if(!checkGameDone()) {
        if(!checkRoundDone()){
            handler.postDelayed(this, 1000);
        }
    }
}
JangoCG
  • 823
  • 10
  • 23

2 Answers2

1

I think Date birthdate = (Date) muecke.getTag(R.id.birthdate); returns null try logging it out to see what happen

adam yong
  • 115
  • 2
  • 9
  • How can I log? I'm realy new to Android, working through a beginners book right now and this is the code of the book which is not working... I tried Log.e("MyTag", birthdate" but it wants a string – JangoCG Mar 17 '19 at 16:04
  • @JangoCG sorry I did not see the notification, you probably have fixed it by now right ? if not thenImageView "muecke" is not initialized yet – adam yong Mar 31 '19 at 14:28
  • @JangoCG btw I dont understand **muecke**, it is a ImageView in `spawnFly()` but you use it in a calculation in `double probabilitiy = muecken * 1.5` – adam yong Mar 31 '19 at 14:30
  • Thanks for the help but I gave up on it and returned the book. It was realy frustrating when the example from the book doesn't work and even tho it is the "solution" code from the publishers website. – JangoCG Apr 01 '19 at 15:42
0

The value returned by getTag is always going to be null unless you have called setTag beforehand. It seems your layout is a list of ImageViews inside a parent view, so at some point beforehand, you need to sett the R.id.birthDate on all those ImageView objects. Something like this:

for (int i = 0; i < playarea.getChildCount(); i++) {
    View muecke = playarea.getChildAt(i);
    Date birthdate = getTheDateForThisViewFromSomewhere();
    muecke.setTag(R.id.birthDate, birthDate);
}
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • I'm calling setTag when spawning the flys. I editet my question and inserted the code for spawning the flys as well – JangoCG Mar 17 '19 at 15:33
  • I editet in as well. Sorry I always need some minutes to translate all my variables from German to English. – JangoCG Mar 17 '19 at 15:47