Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.7.10] [Solved] NullPointerException, because the constructor is never called.

Featured Replies

Posted

Hello, I am having a little bit of trouble with the following

code, I am trying to make a seed plantable on farmland which

works fine if I specify which block is suppose to be placed directly in the main seedPlacement class

however I would like to use this class as a reference for future seeds and so I wish to use a variable to assign the block.

 

However the variable is never assigned because it is never called in my constructor.

And I am currently out of ideas what to try.

 

Any help will be much appreciated!

 

Regards, Venomous

 

The seed item class:

public class ItemLettuceSeeds extends seedPlacement{

public ItemLettuceSeeds() {
	super(0, 0.3f, RealismMod.blockLettuceCrop, Blocks.farmland);
}

}

 

The main seed class:

public class seedPlacement extends ItemFood implements IPlantable
{    
    private final Block theBlockPlant;
    private final Block soilId;

    public seedPlacement(int parHealAmount, float parSaturationModifier, Block parBlockPlant, Block parSoilBlock)
    {
        super(parHealAmount, parSaturationModifier, false);
        theBlockPlant = parBlockPlant; //not getting assigned
        soilId = parSoilBlock; //not getting assigned
    }
    //constructor isn't even being called

    @Override
    public boolean onItemUse(ItemStack parItemStack, EntityPlayer parPlayer, 
          World parWorld, int parX, int parY, int parZ, int par7, float par8, 
          float par9, float par10)
    {
     // not sure what this parameter does, copied it from the potato class
        if (par7 != 1)
        { 
            return false;
        }
        else if (parPlayer.canPlayerEdit(parX, parY+1, parZ, par7, parItemStack))
        {
        	System.out.println("Result: " + theBlockPlant + " and " + soilId);  //Result: Null and Null

            if (parWorld.getBlock(parX, parY, parZ).canSustainPlant(parWorld, 
                  parX, parY, parZ, ForgeDirection.UP, this) && parWorld
                  .isAirBlock(parX, parY+1, parZ))
            {
            	//crashes cause null pointer exception since theBlockPlant is never assigned.
                //parWorld.setBlock(parX, parY+1, parZ, theBlockPlant);

                --parItemStack.stackSize;
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

 

When in doubt, use brute force.

  • Author

Oh wow, you're right.. It works now.

 

I didn't think it would affect it, thanks!

 

[solved]

When in doubt, use brute force.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.