This site requires JavaScript, please enable it in your browser!
Greenfoot back
mitrolex
mitrolex wrote ...

2017/2/16

Flappy Bird Score

mitrolex mitrolex

2017/2/16

#
I have made a Flappy Bird project for school purposes. I got to the part where the bird goes through the pipes and the score goes up by one. But it doesn't. A terminal windows pops up and it gives me a java lang null pointer exception at line 37 of my world class. Can someone help? Names of pretty much all the variable, classes, etc are not in english so you might have problems with that. If needed, i can translate them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import greenfoot.*;
public class background extends World
{  
   int razmak_cevi=100;
    int cevbr=0;
    int ptbr=0;
    int score=0;
    int prva_cev=230;  
    int zmbr=0;
    skor skorObj=null;
    public bg()
    {          
        super(600, 400, 1,false);
        setPaintOrder(skor.class,ptica.class,zemlja.class,cev.class,cevnadole.class,kraj.class);
        ptica pt = new ptica();
        addObject(pt, 100, getHeight()/2-50);             
        zemlja Zemlja = new zemlja();//Stvori zemlju.
        addObject(Zemlja, 500, 375);//Dodaj zemlju.
        skor Skor=new skor();
        Skor.postaviskor(0);
        addObject(Skor ,getWidth()/2,getHeight()/2-100);          
    }
    public void act()
    {
        cevbr++;
        if (cevbr % 100 == 0)
        {
            stvoricev();
        }
        if(cevbr >= prva_cev)
        {  
            if(ptbr % 100 ==0)
            {              
              score++;
              skorObj.postaviskor(score);//It shows me the null pointer at this line.
              Greenfoot.playSound("sfxpoint.mp3");
            }
            ptbr++;           
        }
        zmbr++;
        if(zmbr % 100 == 0)
        
            stvorizemlju();
        }
    }      
 
    private void stvoricev()
    {       
        cevnadole Cevnd = new cevnadole();
        cev Cev = new cev();       
        GreenfootImage image = Cevnd.getImage();       
        addObject(Cev,getWidth(),getHeight()/2 + image.getHeight()- Greenfoot.getRandomNumber(200));
        addObject(Cevnd,getWidth(),Cev.getY() - image.getHeight() - razmak_cevi);
    }
    private void stvorizemlju()
    {
      zemlja Zemlja = new zemlja();
      addObject(Zemlja, 500, 375);
       
    }   
}
Super_Hippo Super_Hippo

2017/2/16

#
Line 10 sets it to null and it is not set to anything else.
valdes valdes

2017/2/17

#
Line 19 should be
1
skorObj=new skor();
line 20
1
skorObj.postaviskor(0);
line 21
1
addObject(skorObj ,getWidth()/2,getHeight()/2-100);
mitrolex mitrolex

2017/2/20

#
Thanks valdes, i'll try that.
mitrolex mitrolex

2017/2/22

#
@valdes It doesn't give me the null pointer now but there's no score on my screen..
Nosson1459 Nosson1459

2017/2/23

#
The skor in the constructor is different than those of the rest of the class since it is defined in "public bg()". On line 10 do "skor skorObj = new skor();", and take out line 19 (from above code) having lines 20 and 21 the way valdes told you to. Show the skor class code if you have questions about it.
mitrolex mitrolex

2017/2/23

#
There's no scoreon the screen again... Here's the skor class code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
public class skor extends Actor
    public void act()
    {        
      GreenfootImage img =new GreenfootImage(getWorld().getWidth()/2,getWorld().getHeight()/2);
      setImage(img);
    }   
    public void postaviskor(int score)
    {
      GreenfootImage img = getImage();
      img.clear();
      img.drawString(""+score,30,30);
      setImage(img);
    }
}
Nosson1459 Nosson1459

2017/2/24

#
What's the point in the code in the skor act method?! All that is doing is making the skor image be a big transparent image, if it's a transparent image 1/4 the size of your world then you won't see anything and since it's in the act method it keeps on getting called each act cycle.
danpost danpost

2017/2/24

#
What Nosson1459 said is, in fact, true. Change line 5 in the skor class code above to this:
1
public skor()
mitrolex mitrolex

2017/2/26

#
Thank's guys, it works like a charm now.
You need to login to post a reply.