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

2025/5/8

Help with null pointer exception while trying to use showtext

Mp2yayy Mp2yayy

2025/5/8

#
im trying to run this code and it keeps giving me null pointer exceptions in relation to the show text parts of it i have all my code below(there are 1 or 2 other objects reffered to but they are completly empty so theres no code to post) WORLD CODE: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { boolean whoseturn = true; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { super(600, 400, 1); } public void act() { P1Charizard p1Charizard = new P1Charizard(); Actor test = new Actor; test = new P1Charizard(); addObject(test, 160, 300); if(whoseturn = true) { p1Charizard.Moveset(); } else if(whoseturn = false) { } } } ACTOR CODE: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class P1Charizard here. * * @author (your name) * @version (a version number or a date) */ public class P1Charizard extends Actor { /** * Act - do whatever the P1Charizard wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { } public void Moveset() { Actor CharizardMoveset = new Actor; CharizardMoveset = populateMoveset(CharizardMoveset); boolean ts = true; if(ts = true) { getWorld().showText("What Action Would You Like To Take",430, 280); getWorld().showText("Attack(1)", 350, 310); getWorld().showText("Swap Out(2)", 500, 310); } if(Greenfoot.isKeyDown("1")) { ts = false; getWorld().showText("", 430, 280); getWorld().showText("", 350, 310); getWorld().showText("", 500, 310); getWorld().showText("Which Attack Would You Like To Use",430, 280); getWorld().showText("Flamethrower(3)", 350, 310); getWorld().showText("Fire Blast(4)", 500, 310); getWorld().showText("Fly(5)", 350, 360); getWorld().showText("Dragon Claw(6)", 500, 360); if(Greenfoot.isKeyDown("3")) { getWorld().addObject(CharizardMoveset, 300, 200); Greenfoot.delay(100); getWorld().removeObject(CharizardMoveset); getWorld().showText("", 430, 280); getWorld().showText("", 350, 310); getWorld().showText("", 500, 310); getWorld().showText("", 350, 360); getWorld().showText("", 500, 360); } Greenfoot.delay(1); } } public Actor populateMoveset(Actor CharizardMoveset) { CharizardMoveset = new Flamethrower(); return CharizardMoveset; } }
danpost danpost

2025/5/9

#
Mp2yayy wrote...
im trying to run this code and it keeps giving me null pointer exceptions in relation to the show text parts of it i have all my code below << Code Omitted >>
The first four lines in your act method are supposed to create a new P1Charizard actor and add it to the world. Well. it would if no problems were present. The proper code should be like one of the following:
P1Charizard p1Charizard; // declaring an object reference variable
p1Charizard = new P1Charizard(); // assigning an object for the variable to reference
addObject(p1Charizard, 160, 300); // using that reference to add the object into the world
or, another way:
Actor p1C = new P1Charizard(); // declaring variable and assigning an object to it
addObject(p1C, 160, 300);
( the world will accept any Actor type object to be added into it; that is, the variable does not have to be specific to its type of Actor object ) or more simply:
addObject(new P1Charizard(), 160, 300);
In your case, since you are going to be calling a method on the object being referenced in the MyWorld class, your code should be as follows:
/**     in MyWorld class    */
// above the constructor
P1Charizard p1Charizard = new P1Charizard();

// in constructor, after super call
addObject(p1Charizard(160, 300);
This way, each MyWorld object created will create exactly one P1Charizard object and maintain a reference to that one it creates. You will also need to clean up the code in the P1Charizard class. The first two lines in the MoveSet method has same issue as above. Other "issue" is that you declare a new boolean variable and set it to true; then, immediate asks if it is true; but, of course it is (so why ask). Actually not sure why the boolean is there to begin with (don't see how it does anything useful). Oh, and the code for checking the boolean will not work as posted. It should be:
if (ts == true)
(using two equal signs (==) to signify a comparison, over an assignment statement. Same problem in world with whoseturn boolean.
You need to login to post a reply.