简体   繁体   English

从抽象类获取变量

[英]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... 基本上,我想在主Mod类中的字符串中获取从“ World.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. World不是静态类...您需要一个World兼容对象的实例来获取名称。 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 您尚未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 因此,当您尝试实例化World_Spy调用其父类构造函数World() ,会在以下位置获得NullPointerException

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: 我相信通过在此处抛出NullPointerException来“崩溃”:

this.worldInfo.setWorldName(par2Str);

Indeed variable worldInfo was never initialized by you try to call its method setWorldName() . 实际上,变量worldInfo从未通过您尝试调用其方法setWorldName()来初始化。 Since the variable is null at this point throwing NullPointerException sounds reasonable. 由于此时变量为null ,因此抛出NullPointerException听起来很合理。

In java (exactly like in all other programming languages I know) you have to initialize variable before using it. 在Java中(就像我所知道的所有其他编程语言一样),必须在使用变量之前对其进行初始化。 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. 但是,自定义类型的变量被初始化为null ,这可能会使初学者感到困惑。

To initialize you have to use new keyword following by constructor call: 要初始化,您必须在构造函数调用之后使用new关键字:

worldInfo = new WorldInfo();

now you can call setters and other methods of worldInfo . 现在您可以调用setter和worldInfo其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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