Jump to content

Constructor crop error


Zedicus

Recommended Posts

Not wanting too much preamble I was a little bored this morning and decided to make a constructor/class that could 'theoretically' allow me to make 'new' crops very easily, basically just started with my working crop file and added variables that I can control in the constructor; it was working, possibly, but when I returned to my computer It was erroring, It's possible I made some edit between then and the 'run' but I couldn't work out the source so I thought I'd come to the forge community for a little guidance -

 

Constructor from the main class -

 

cropblock = new BlockCrop(231, 0, Main.cropseed.shiftedIndex, Main.cropseed, Main.itemFlax.shiftedIndex, Block.grass.blockID, 0).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName("cropblock");

 

The crop class -

 

public class BlockCrop extends BlockFlower {

private int seedtype;

private Item seedtype2;

private int droppedblock;

private int underblock;

private int indent;

protected BlockCrop(int par1, int par2, int par3, Item par4,
		int par5, int par6, int par7) {
        super(par1, par2);
        blockIndexInTexture = par2;
        setTickRandomly(true);
        float f = 0.5F;
        setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
        this.setCreativeTab((CreativeTabs)null);
        this.disableStats();
        this.setRequiresSelfNotify();
        seedtype = par3;
        seedtype2 = par4;
        droppedblock = par5;
        underblock = par6;
        indent = par7 + 2;
    }

protected boolean canThisPlantGrowOnThisBlockID(int par1) {
        return par1 == underblock;
    }

public String getTextureFile() {
            return CommonProxy.BLOCK_PNG;
    }

public int getRenderType() {
        return 6;
    }

public int idPicked(World par1World, int par2, int par3, int par4) {
        return seedtype;
    }

public void fertilize(World par1World, int par2, int par3, int par4) {
        par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
    }

public int getBlockTextureFromSideAndMetadata(int par1, int par2) {
  switch(par2) {
   case 0:
    return 0 + indent;
   case 1:
    return 1 + indent;
   case 2:
    return 2 + indent;
   default:
    return 0 + indent;
  }
    }

public int idDropped(int par1, Random par2Random, int par3) {
        if (par1 == 2) {
            return droppedblock;
        }
        else {
            return 0;
        }
    }

public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random) {
        super.updateTick(par1World, par2, par3, par4, par5Random);

        if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9) {
            int i = par1World.getBlockMetadata(par2, par3, par4);

            if (i < 2) {
                float f = getGrowthRate(par1World, par2, par3, par4);

                if (par5Random.nextInt((int)(25F / f) + 1) == 0) {
                    i++;
                    par1World.setBlockMetadataWithNotify(par2, par3, par4, i);
                }
            }
        }
    }

private float getGrowthRate(World par1World, int par2, int par3, int par4) {
        float f = 1.0F;
        int i = par1World.getBlockId(par2, par3, par4 - 1);
        int j = par1World.getBlockId(par2, par3, par4 + 1);
        int k = par1World.getBlockId(par2 - 1, par3, par4);
        int l = par1World.getBlockId(par2 + 1, par3, par4);
        int i1 = par1World.getBlockId(par2 - 1, par3, par4 - 1);
        int j1 = par1World.getBlockId(par2 + 1, par3, par4 - 1);
        int k1 = par1World.getBlockId(par2 + 1, par3, par4 + 1);
        int l1 = par1World.getBlockId(par2 - 1, par3, par4 + 1);
        boolean flag = k == blockID || l == blockID;
        boolean flag1 = i == blockID || j == blockID;
        boolean flag2 = i1 == blockID || j1 == blockID || k1 == blockID || l1 == blockID;

        for (int i2 = par2 - 1; i2 <= par2 + 1; i2++) {
            for (int j2 = par4 - 1; j2 <= par4 + 1; j2++) {
                int k2 = par1World.getBlockId(i2, par3 - 1, j2);
                float f1 = 0.0F;

                if (k2 == underblock) {
                    f1 = 1.0F;

                    if (par1World.getBlockMetadata(i2, par3 - 1, j2) > 0) {
                        f1 = 3F;
                    }
                }

                if (i2 != par2 || j2 != par4) {
                    f1 /= 4F;
                }

                f += f1;
            }
        }

        if (flag2 || flag && flag1) {
            f /= 2.0F;
        }

        return f;
    }
public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7) {
        super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, 0);

        if (par1World.isRemote) {
            return;
        }

        int i = 3 + par7;

        for (int j = 0; j < i; j++) {
            if (par1World.rand.nextInt(5) == 0) {
                float f = 0.7F;
                float f1 = par1World.rand.nextFloat() * f + (1.0F - f) * 0.5F;
                float f2 = par1World.rand.nextFloat() * f + (1.0F - f) * 0.5F;
                float f3 = par1World.rand.nextFloat() * f + (1.0F - f) * 0.5F;
                EntityItem entityitem = new EntityItem(par1World, (float)par2 + f1, (float)par3 + f2, (float)par4 + f3, new ItemStack(seedtype2));
                entityitem.delayBeforeCanPickup = 10;
                par1World.spawnEntityInWorld(entityitem);
            }
        }
    }
}

 

And finally the error -

 

java.lang.NullPointerException
at metocraft.common.Main.load(Main.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:440)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:69)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:317)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:300)
at com.google.common.eventbus.EventBus.post(EventBus.java:268)
at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:69)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:317)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:300)
at com.google.common.eventbus.EventBus.post(EventBus.java:268)
at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:83)
at cpw.mods.fml.common.Loader.initializeMods(Loader.java:651)
at cpw.mods.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:196)
at net.minecraft.client.Minecraft.startGame(Minecraft.java:469)
at net.minecraft.client.Minecraft.run(Minecraft.java:756)
at java.lang.Thread.run(Unknown Source)

 

Note: line 44 is the constructor; also should point out it's in no way a finished product, just a little project.

 

EDIT: If the constructor's removed it works, hence why I thought the issue's probably there.

EDIT2: MCP version 722 - Forge version 6.3.0.372

Link to comment
Share on other sites

Looks like you didn't initialize "Main.cropseed" and/or "Main.itemFlax" before you call that constructor.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Okay, I understand the initialization of variables in java... but I'm still new enough to forge/minecraft modding, how would I go about initializing it before the constructor's called?

 

@Mod(modid="metocraft", name="Metocraft", version="0.1")
@NetworkMod(clientSideRequired=true, serverSideRequired=false)

public class Main {

public static Block cropblock;
public static Item cropseed;
public static Item itemFlax;

@Instance("metocraft")
public static Main instance;

@SidedProxy(clientSide="metocraft.client.ClientProxy", serverSide="metocraft.common.CommonProxy")
public static CommonProxy proxy;

@PreInit
public void preInit(FMLPreInitializationEvent event) {

}

@Init
public void load(FMLInitializationEvent event) {

	proxy.registerRenderers();

	cropblock = new BlockCrop(231, 0, Main.cropseed.shiftedIndex, Main.cropseed, Main.itemFlax.shiftedIndex, Block.grass.blockID, 0).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName("cropblock");

	cropseed = new ItemCropSeed(555, Main.cropblock.blockID, Block.grass.blockID).setIconIndex(0).setItemName("flaxseed");

	itemFlax = new ItemFlax(556).setIconIndex(1).setItemName("Flaxfood");

	GameRegistry.registerBlock(cropblock);

	LanguageRegistry.addName(cropblock, "Custom Crop");
	LanguageRegistry.addName(cropseed, "Flax Seed");
	LanguageRegistry.addName(itemFlax, "Flax");

}

@PostInit
public void postInit(FMLPostInitializationEvent event) {

}
}

 

That's more or less my main class.

Link to comment
Share on other sites

Okay, I understand the initialization of variables in java... but I'm still new enough to forge/minecraft modding, how would I go about initializing it before the constructor's called?

 

@Mod(modid="metocraft", name="Metocraft", version="0.1")
@NetworkMod(clientSideRequired=true, serverSideRequired=false)

public class Main {

public static Block cropblock;
public static Item cropseed;
public static Item itemFlax;

@Instance("metocraft")
public static Main instance;

@SidedProxy(clientSide="metocraft.client.ClientProxy", serverSide="metocraft.common.CommonProxy")
public static CommonProxy proxy;

@PreInit
public void preInit(FMLPreInitializationEvent event) {

}

@Init
public void load(FMLInitializationEvent event) {

	proxy.registerRenderers();

	cropblock = new BlockCrop(231, 0, Main.cropseed.shiftedIndex, Main.cropseed, Main.itemFlax.shiftedIndex, Block.grass.blockID, 0).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName("cropblock");

	cropseed = new ItemCropSeed(555, Main.cropblock.blockID, Block.grass.blockID).setIconIndex(0).setItemName("flaxseed");

	itemFlax = new ItemFlax(556).setIconIndex(1).setItemName("Flaxfood");

	GameRegistry.registerBlock(cropblock);

	LanguageRegistry.addName(cropblock, "Custom Crop");
	LanguageRegistry.addName(cropseed, "Flax Seed");
	LanguageRegistry.addName(itemFlax, "Flax");

}

@PostInit
public void postInit(FMLPostInitializationEvent event) {

}
}

 

That's more or less my main class.

 

Tha errort has nothing to do with Forge modding, it's a simple syntax error you made. Put this line:

cropblock = new BlockCrop(231, 0, Main.cropseed.shiftedIndex, Main.cropseed, Main.itemFlax.shiftedIndex, Block.grass.blockID, 0).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName("cropblock");

 

below this line:

itemFlax = new ItemFlax(556).setIconIndex(1).setItemName("Flaxfood");

 

Just to explain that to you: the cropblock constructor gives you an NPE, because the variables cropseed and itemFlax are null when you call them. You try to initialize them after initializing cropblock and because cropblock needs these two variables, it simply doesn't work.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Well, the reason I brought up my knowledge of 'forge modding' was because, I was fairly certain that's what you meant... I did it... and it still sent the same error message the difference being it's now on like 45, due to the move, I also attempted to put every single notice of 'cropblock' below the ItemFlax e.t.c. and it still gave the error message, hence why I edited my post, what ever have I done wrong xD

Link to comment
Share on other sites

Okay then, maybe this works:

Remove all "Main." references from your variables, like in this one: "Main.cropseed.shiftedIndex"

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

I'm afraid that also didn't work, still the exact same error... this is ever so slightly frustrating, but I'm sure it'll work out eventually ;)

 

So you said you moved the line like I told you. Could you provide the changed code?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Currently -

public class Main {

public static Item cropseed;
public static Item itemFlax;
public static Block cropblock;


@Instance("metocraft")
public static Main instance;

@SidedProxy(clientSide="metocraft.client.ClientProxy", serverSide="metocraft.common.CommonProxy")
public static CommonProxy proxy;

@PreInit
public void preInit(FMLPreInitializationEvent event) {

}

@Init
public void load(FMLInitializationEvent event) {

	cropseed = new ItemCropSeed(555, cropblock.blockID, Block.grass.blockID).setIconIndex(0).setItemName("flaxseed");

	itemFlax = new ItemFlax(556).setIconIndex(1).setItemName("Flaxfood");

	cropblock = new BlockCrop(231, 0, cropseed.shiftedIndex, cropseed, itemFlax.shiftedIndex, Block.grass.blockID, 0).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName("blockcrop");

	GameRegistry.registerBlock(cropblock);

	LanguageRegistry.addName(cropblock, "Custom Crop");
	LanguageRegistry.addName(cropseed, "Flax Seed");
	LanguageRegistry.addName(itemFlax, "Flax");

	proxy.registerRenderers();

}

@PostInit
public void postInit(FMLPostInitializationEvent event) {

}
}

Link to comment
Share on other sites

Currently -

public class Main {

public static Item cropseed;
public static Item itemFlax;
public static Block cropblock;


@Instance("metocraft")
public static Main instance;

@SidedProxy(clientSide="metocraft.client.ClientProxy", serverSide="metocraft.common.CommonProxy")
public static CommonProxy proxy;

@PreInit
public void preInit(FMLPreInitializationEvent event) {

}

@Init
public void load(FMLInitializationEvent event) {

	cropseed = new ItemCropSeed(555, cropblock.blockID, Block.grass.blockID).setIconIndex(0).setItemName("flaxseed");

	itemFlax = new ItemFlax(556).setIconIndex(1).setItemName("Flaxfood");

	cropblock = new BlockCrop(231, 0, cropseed.shiftedIndex, cropseed, itemFlax.shiftedIndex, Block.grass.blockID, 0).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName("blockcrop");

	GameRegistry.registerBlock(cropblock);

	LanguageRegistry.addName(cropblock, "Custom Crop");
	LanguageRegistry.addName(cropseed, "Flax Seed");
	LanguageRegistry.addName(itemFlax, "Flax");

	proxy.registerRenderers();

}

@PostInit
public void postInit(FMLPostInitializationEvent event) {

}
}

 

I see the problem now. Replace

cropblock.blockID

 

in your seed initialization with the actual Block ID

231

 

If you grab the IDs from a config file in the future, grab the actual number and put it there.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Thanks! So my code wasn't 'too' bad, haha.

Awesome, technically I could make a mod using that crop template make others with different growth stages etc, that'd be a little dull, wouldn't want to pretty much rip of Pam's, though an idea comes to mind... grow-able 'ores' iron plant, that kind of thing, anyway, enough rambling. Configs for the IDs are easy enough, as I mentioned this was just a little project I attempted this morning, may actually do something with it. ;)

 

Thanks again

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.