简体   繁体   中英

Get a variable from an abstract class

Basicly i want to get the name of the map that is played from the "World.class", in a string on my main mod class...

    public abstract class World implements IBlockAccess{
    protected WorldInfo worldInfo;
    //=====OtherStuff=====
    public World(ISaveHandler par1ISaveHandler, String par2Str, WorldSettings par3WorldSettings, WorldProvider par4WorldProvider, Profiler par5Profiler, ILogAgent par6ILogAgent)
        {
        this.worldInfo.setWorldName(par2Str);
        }
    //=====OtherStuff=====
}

i created a class in the same package with this one

public class World_Spy extends World{

    public World_Spy(ISaveHandler par1iSaveHandler, String par2Str,
            WorldProvider par3WorldProvider, WorldSettings par4WorldSettings,
            Profiler par5Profiler, ILogAgent par6iLogAgent) {
        super(par1iSaveHandler, par2Str, par3WorldProvider, par4WorldSettings,
                par5Profiler, par6iLogAgent);
    }

    @Override
    protected IChunkProvider createChunkProvider() {
        return null;
    }

    @Override
    public Entity getEntityByID(int i) {
        return null;
    }


    String TheName = "";
    public void gotIt(){
        TheName = this.worldInfo.getWorldName();
        System.out.println(TheName);
    }

}

and i call it from my main class with:

World_Spy WName = new World_Spy(null, null, null, null, null, null);

but it chrashes on startup... any ideas?

World is not a static class... you need an instance of a World compatible object to get the name. One way to get an instance of a World and then the name:

World world = Minecraft.getMinecraft().isIntegratedServerRunning() ? mc.getIntegratedServer().worldServerForDimension(Minecraft.getMinecraft().thePlayer.dimension) : Minecraft.getMinecraft().theWorld;
String worldName = world.getWorldInfo().getWorldName();

This code should work client-side.

You have not initalized worldInfo

protected WorldInfo worldInfo; // initialization MISSING!

So, when you try to instantiate World_Spy which in turn calls its parent class constructor World() you get a NullPointerException at

this.worldInfo.setWorldName(par2Str); // NullPointerException here

To resolve the issue simply provide an instance

protected WorldInfo worldInfo = new WorldInfo();

I believe it "crashes" by throwing NullPointerException here:

this.worldInfo.setWorldName(par2Str);

Indeed variable worldInfo was never initialized by you try to call its method setWorldName() . Since the variable is null at this point throwing NullPointerException sounds reasonable.

In java (exactly like in all other programming languages I know) you have to initialize variable before using it. Indeed primitive types are initialized by default using some kind of "normal" value. However variables of custom types are initialized to null that may confuse beginners.

To initialize you have to use new keyword following by constructor call:

worldInfo = new WorldInfo();

now you can call setters and other methods of worldInfo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM