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

2012/5/24

Referencing Strings from another class.

JayWood860 JayWood860

2012/5/24

#
Hey guys, I am trying to recreate a version of the "Simsimi" app, but i am having trouble referencing the TextBox from Simsimi. I thought about making the Textbox class extend the Simsimi class, or (vice versa), but I am not sure if this is right or the simplest way to do this. Anyway, the way i have it set up, the following error comes up on my screen through the Greenfoot Terminal Window: "java.lang.NullPointerException at Simsimi.<init>(Simsimi.java:12) at world.<init>(world.java:13) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at greenfoot.core.Simulation.newInstance(Simulation.java:520) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411) at greenfoot.core.Simulation.maybePause(Simulation.java:269) at greenfoot.core.Simulation.runContent(Simulation.java:201) at greenfoot.core.Simulation.run(Simulation.java:194)" I have checked line 13 and 12 of the world and Simsimi class respectively, and see no logic nor syntax errors. Please help? world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Point;
/**
 * Write a description of class world here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class world extends World
{
    TextBox textbox = new TextBox(new Point(250, 100), "");
    Simsimi simsim = new Simsimi();
    /**
     * Constructor for objects of class world.
     * 
     */
    public world()
    {    
        super(600, 400, 1); 
        addObject(textbox, 450, 300);
        addObject(simsim, 50, 40);
        Color gold = new Color(255, 215, 0);
        color(gold);
        
        Greenfoot.start();
    }
    public TextBox getTextbox()
    {
        return textbox;
    }
    public Simsimi getSimsim()
    {
        return simsim;
    }
}
Simsimi:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Browser here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Simsimi extends Actor
{
    world world = (world) getWorld();
    TextBox textbox = world.getTextbox();
    GreenfootImage img = getImage();
    public Simsimi()
    {
        img.scale(150, 70);
    }
    /**
     * Act - do whatever the Browser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       
    }
}
TextBox Parameter and Method:
public TextBox(Point size, String text)
    {
        this.size = size;
        this.text = text;
        caret = text.length();
        paint(false);
        panel.addMouseWheelListener(scroller);
    }
    /**
     * Get the contents of this TextBox.
     * @return The text in this TextBox.
     */
    public String getText()
    {
        return text;
    }
JayWood860 JayWood860

2012/5/24

#
As for the textbox, the method is irrelevant at this point i did not mean to add it. just the parameter to show that i was adding it to the world correctly. and if this was not previously stated, there are no syntax errors through the program, just this issue i have right now.
You need to login to post a reply.