Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

[1.4.2](Forge)java.java.lang.NullPointerException


  • Please log in to reply
9 replies to this topic

#1

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 01 November 2012 - 02:24 PM

Hi !

I'm trying to create my mod but I have a little problem with my new Gui.

API : Forge 6.0.1.341

problem : When I use this function to recover the Stack in slot, i have an java.java.lang.NullPointerException :

Code :

extractor.craftMatrix.getStackInSlot(0).getItem();


Error :

java.lang.NullPointerException
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiAmeExtractor.actionPerformed(GuiAmeExtractor.java:49)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiScreen.mouseClicked(GuiScreen.java:120)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiContainer.mouseClicked(GuiContainer.java:301)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiScreen.handleMouseInput(GuiScreen.java:192)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiScreen.handleInput(GuiScreen.java:171)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.client.Minecraft.runTick(Minecraft.java:1502)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:854)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.client.Minecraft.run(Minecraft.java:779)
2012-11-01 14:50:16 [INFO] [STDERR] at java.lang.Thread.run(Unknown Source)


I don't know where is the problem .

Register or log in to remove.

#2

Salinor
    Salinor

    Stone Miner

  • Members
  • 72 posts

Posted 01 November 2012 - 02:41 PM

View Postvalb, on 01 November 2012 - 02:24 PM, said:

Hi !

I'm trying to create my mod but I have a little problem with my new Gui.

API : Forge 6.0.1.341

problem : When I use this function to recover the Stack in slot, i have an java.java.lang.NullPointerException :

Code :

extractor.craftMatrix.getStackInSlot(0).getItem();


Error :

java.lang.NullPointerException
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiAmeExtractor.actionPerformed(GuiAmeExtractor.java:49)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiScreen.mouseClicked(GuiScreen.java:120)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiContainer.mouseClicked(GuiContainer.java:301)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiScreen.handleMouseInput(GuiScreen.java:192)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.src.GuiScreen.handleInput(GuiScreen.java:171)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.client.Minecraft.runTick(Minecraft.java:1502)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:854)
2012-11-01 14:50:16 [INFO] [STDERR] at net.minecraft.client.Minecraft.run(Minecraft.java:779)
2012-11-01 14:50:16 [INFO] [STDERR] at java.lang.Thread.run(Unknown Source)


I don't know where is the problem .

Did you intialize everything? Most frequently I will declare the variable, but sometimes forget to actually instantiate (create) something into it. So make sure you have something like extractor = new CoolExtractor(a,B);  or whatever it's constructor/class is ...

#3

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 01 November 2012 - 02:54 PM

I have initializ , but I always have NullPointerExecption :

This is my class :

package net.minecraft.src;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import net.minecraft.client.Minecraft;
import org.lwjgl.opengl.GL11;
@SideOnly(Side.CLIENT)
public class GuiAmeExtractor extends GuiContainer
{
public Minecraft mc = ModLoader.getMinecraftInstance();

public TileEntityAmeExtractor tile = new TileEntityAmeExtractor();

public ContainerAmeExtractor extractor = new ContainerAmeExtractor(mc.thePlayer.inventory, tile);
/**
* Defini les longueurs du Gui, qui serviront a le dessiner dans drawGuiContainerBackgroundLayer().
*/
private int xSize = 228;
private int ySize = 202;

public GuiAmeExtractor(InventoryPlayer par1InventoryPlayer, TileEntityAmeExtractor extractor)
{
	 super(new ContainerAmeExtractor(par1InventoryPlayer, extractor));
}
/**
	 * CETTE CLASSE DESSINE LE GUI (Fond, boutton .... )
	 */

/**
	 * Draw the background layer for the GuiContainer (everything behind the items)
	 */
protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
{
	 int var4 = this.mc.renderEngine.getTexture("/mod/Alchimist2.png");
	 GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	 this.mc.renderEngine.bindTexture(var4);
	 int var5 = (this.width - this.xSize) / 2;
	 int var6 = (this.height - this.ySize) / 2;
	 this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
	 controlList.add(new GuiSmallButton(100, var5+ 86, var6 + 95, 60, 20, "Transmutation"));
}


public void actionPerformed(GuiButton guibutton)
{
	 if(guibutton.id==100){
		 System.out.println("updateEntity");

			 if(extractor.craftMatrix.getStackInSlot(1).getItem() == mod_ItemVal.montreAlchimist)
			 {
				 System.out.println("Yesss !!!!");
			 }
			 }
		 System.out.println("Fin");
	 }


public static int func(ItemStack par1ItemStack){
	
	
	 if (par1ItemStack == null)
	 {
		 return 0;
	 }
	
	 int i = par1ItemStack.getItem().shiftedIndex;
	 int j = mod_ItemVal.montreAlchimist.shiftedIndex;
	
	 if( i == j){
		
		 return 1;
	 }else{
		 return 0;
	 }
}

public int func_2(ContainerAmeExtractor ext){
	
	 int i = -1;
	 try{
	 ext.craftMatrix.getStackInSlot(0);
	 } catch (NullPointerException e) {
	 i = 0;
	 }
	 try{
	 ext.craftMatrix.getStackInSlot(1);
	 } catch (NullPointerException e) {
	 i = 1;
	 }
	 try{
	 ext.craftResult.getStackInSlot(0);
	 } catch (NullPointerException e) {
	 i = 3;
	 }
	 return i;
}
}


#4

_bau5
    _bau5

    Gold Miner

  • Members
  • 498 posts
  • Location: United States
  • Minecraft: _bau5bot

Posted 01 November 2012 - 03:57 PM

I ran into this problem a while ago. if you use getStackInSlot(..) and there is no itemstack in that slot,it will return null. But if you then try to use getItem(...) on that ItemStack, that is null, the game will crash. So just add a null check:

if(getStackInSlot(0) != null){
	  item = getStackInSlot(0).getItem();
}
Should resolve your issue
something - _bau5(formerly rickrage12).

#5

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 01 November 2012 - 09:42 PM

I have this code but the if() doesn't work :

public void actionPerformed(GuiButton guibutton)
{
	 if(guibutton.id==100){
		 System.out.println("updateEntity");

			 if(extractor.craftMatrix.getStackInSlot(0) != null){
				 System.out.println("If 1 passé");
			 if(extractor.craftMatrix.getStackInSlot(1) != null){
					 System.out.println("If 2 passé");
				 if(extractor.craftMatrix.getStackInSlot(1).getItem() == mod_ItemVal.montreAlchimist){
					 System.out.println("If 3 passé");
				 if(extractor.craftMatrix.getStackInSlot(0).getItem() == mod_ItemVal.ossementSkel){
						 System.out.println("If 4 passé");
				 extractor.craftResult.setInventorySlotContents(2, new ItemStack(mod_ItemVal.lumivert, 1));
				 }
				 }
				
				 }
			 }
			 }

		 System.out.println("Fin");
	 }


#6

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 02 November 2012 - 12:48 PM

UP Posted Image

#7

_bau5
    _bau5

    Gold Miner

  • Members
  • 498 posts
  • Location: United States
  • Minecraft: _bau5bot

Posted 02 November 2012 - 04:59 PM

This code will not work. In fact, forge does this for you. You need to register the tile entity. Then you need to make a class that handles the opening of the gui that is called when you right click on the block. The GuiHandler will have a getClientGUIElement function and a getServerElement function. The getClient one will return the gui and open it, the server element one will return the container and bind the two together thus when you click in the gui it's sent to the container. It's all handled for you.

I'm sorry but you need to look at a tutorial for this and redo it completely.
something - _bau5(formerly rickrage12).

#8

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 02 November 2012 - 06:00 PM

You mean this :

public boolean onBlockActivated(World world, int par2, int par3, int par4, EntityPlayer entityplayer, int par6, float par7, float par8, float par9)
{
	 if (world.isRemote){
	 return true;
	 }
	 else{
	
	 if(entityplayer instanceof EntityPlayerMP){
	 ModLoader.serverOpenWindow((EntityPlayerMP)entityplayer, new ContainerAmeExtractor(entityplayer.inventory, extractor), mod_AmeExtractor.GUI_Ame, par2, par3, par4);
	 }
	 else{
	 Minecraft mc = new ModLoader().getMinecraftInstance();
	 mc.displayGuiScreen(new GuiAmeExtractor(entityplayer.inventory, extractor));
	 }
	 }
	 return true;
}


#9

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 03 November 2012 - 02:33 PM

UP

#10

valb
    valb

    Out of the Water

  • Members
  • 7 posts

Posted 03 November 2012 - 04:53 PM

Hi!

My if () then run, but multiply items in my gui, and it is impossible to recover them, they disappear immediately.




Here are my codes:

mod_AmeExtractor

package net.minecraft.src;
import net.minecraft.client.Minecraft;public class mod_AmeExtractor extends BaseMod{

public static TileEntityAmeExtractor extractor = new TileEntityAmeExtractor();

public static final int GUI_Ame = 2;
public static final Block AlchimistAmeExtractor = (new BlockAmeExtractor(205)).setBlockName("AlchimistAmeExtractor").setCreativeTab(CreativeTabs.tabBlock);
public void load()
	 {
/** Block Register */
ModLoader.registerBlock(AlchimistAmeExtractor);
/** Block Name */
ModLoader.addName(AlchimistAmeExtractor, "Extracteur d'Ame"); /** Gui Register */
ModLoader.registerContainerID(this, GUI_Ame);
	 }


public GuiContainer getContainerGUI(EntityClientPlayerMP player, int guiID, int i, int j, int k)
{
if(guiID == this.GUI_Ame){
Minecraft mc = ModLoader.getMinecraftInstance();
mc.thePlayer.addChatMessage("mod_AmeExtractor");
return new GuiAmeExtractor(player.inventory, this.getTile());
}
return null;
}

public static TileEntityAmeExtractor getTile(){
return extractor;
}

	 public String getVersion()
	 {
		 return "1.4.2";
	 }}


BlockAmeExtractor :

package net.minecraft.src;
import net.minecraftforge.*;
import net.minecraft.client.Minecraft;
import java.util.*;
public class BlockAmeExtractor extends Block
{
// public mod_AmeExtractor ame = new mod_AmeExtractor();

protected BlockAmeExtractor(int par1)
{
	 super(par1, Material.rock);
	 this.blockIndexInTexture = 59;
	 this.setTextureFile(mod_ItemVal.textureBlock);
	 this.setCreativeTab(CreativeTabs.tabDecorations);
}

public int getBlockTextureFromSide(int i)
{
		 if (i == 0) // Ici, 0 est la texture du dessous.
		 {
				 return 29;
		 }
		 if (i == 1) // Ici, 1 est la texture du dessus.
		 {
				 return 30;
		 }
		 else // Ici, 2 est la texture de gauche.
		 {
				 return 28;
		 }
		
}
/** Apelle le GUI lorsqu'on clique droit sur le block */
public boolean onBlockActivated(World world, int par2, int par3, int par4, EntityPlayer entityplayer, int par6, float par7, float par8, float par9)
{
	 if (world.isRemote){
	 return true;
	 }
	 else{
	
	 if(entityplayer instanceof EntityPlayerMP){
	 ModLoader.serverOpenWindow((EntityPlayerMP)entityplayer, new ContainerAmeExtractor(entityplayer.inventory, mod_AmeExtractor.getTile()), mod_AmeExtractor.GUI_Ame, par2, par3, par4);
	 }
	 else{
	 Minecraft mc = new ModLoader().getMinecraftInstance();
	 mc.displayGuiScreen(new GuiAmeExtractor(entityplayer.inventory, mod_AmeExtractor.getTile()));
	 }
	 }
	 return true;
}

}

GuiAmeExtractor :

package net.minecraft.src;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.*;
import org.lwjgl.opengl.GL11;
@SideOnly(Side.CLIENT)
public class GuiAmeExtractor extends GuiContainer
{
public Minecraft mc = ModLoader.getMinecraftInstance();

//public mod_AmeExtractor ame = new mod_AmeExtractor();

//public TileEntityAmeExtractor tile = new TileEntityAmeExtractor();

public ContainerAmeExtractor extract = new ContainerAmeExtractor(mc.thePlayer.inventory, mod_AmeExtractor.getTile());

/**
* Defini les longueurs du Gui, qui serviront a le dessiner dans drawGuiContainerBackgroundLayer().
*/
private int xSize = 228;
private int ySize = 202;


public GuiAmeExtractor(InventoryPlayer par1InventoryPlayer, TileEntityAmeExtractor extractor)
{
	 super(new ContainerAmeExtractor(par1InventoryPlayer, extractor));
	 //super(new ContainerAmeExtractor(par1InventoryPlayer, extractor));
}
/**
	 * CETTE CLASSE DESSINE LE GUI (Fond, boutton .... )
	 */

/**
	 * Draw the background layer for the GuiContainer (everything behind the items)
	 */
protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
{
	 int var4 = this.mc.renderEngine.getTexture("/mod/Alchimist2.png");
	 GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	 this.mc.renderEngine.bindTexture(var4);
	 int var5 = (this.width - this.xSize) / 2;
	 int var6 = (this.height - this.ySize) / 2;
	 this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
	 controlList.add(new GuiSmallButton(100, var5+ 86, var6 + 95, 60, 20, "Transmutation"));
}


public void actionPerformed(GuiButton guibutton)
{
	 if(guibutton.id==100){
		 extract.Craft();
			 }
	 }




}

ContainerAmeExtractor :

package net.minecraft.src;
import net.minecraft.client.Minecraft;
public class ContainerAmeExtractor extends Container
{
//public mod_AmeExtractor ame = new mod_AmeExtractor();
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
// private TileEntityAmeExtractor extract = new TileEntityAmeExtractor();
public IInventory craftResult = new InventoryCraftResult();


public Minecraft mc = ModLoader.getMinecraftInstance();


/**
	 * Ici, le contructeur du Container creer les slots du GUI ou seront les items.
	 *
	 * - En premier les slots pour crafter
	 * - Ensuite, les slots de l'inventaire, (3 * 9)
	 * - Puis les 9 slot "raccourcies" en bas de l'inventaire
	 * - Ensuite un slot specifique au GUI
	 * - Pour finir, le slot du resultat du Craft, lui aussi specifique au GUI
	 *
	 *
	 */
public ContainerAmeExtractor(InventoryPlayer par1InventoryPlayer, TileEntityAmeExtractor extractor)
{
	
	 int j = 1;
	 int i = 0;

	 this.addSlotToContainer(new SlotAmeExtractor(extractor, 0, 22, 16 + j * 18));

	 for (j = 0; j < 3; ++j)
	 {
		 for (i = 0; i < 9; ++i)
		 {
			 this.addSlotToContainer(new Slot(par1InventoryPlayer, i + j * 9 + 9, 9 + i * 18, 105 + j * 18));
		 }
	 }
	
	 for (j = 0; j < 9; ++j)
	 {
		 this.addSlotToContainer(new Slot(par1InventoryPlayer, j, 9 + j * 18, 163));
	 }
this.addSlotToContainer(new SlotAmeExtractor(extractor, 1, 81, 36));
this.addSlotToContainer(new SlotAmeExtractor(extractor, 2, 140, 36));
// this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 2, 140, 35));
	
}
		
public void Craft(){
	
	 System.out.println("updateEntity");

	 if(mod_AmeExtractor.getTile() != null){
		 System.out.println("If 1 passé");
	 if(mod_AmeExtractor.getTile().getStackInSlot(0) != null){
			 System.out.println("If 2 passé");

	 if(mod_AmeExtractor.extractor.getStackInSlot(1) != null){
			 System.out.println("If 3 passé");
	 if(mod_AmeExtractor.extractor.getStackInSlot(1).getItem() == mod_ItemVal.montreAlchimist){
			 System.out.println("If 4 passé");
		 if(mod_AmeExtractor.extractor.getStackInSlot(0).getItem() == mod_ItemVal.ossementZombie){
				 System.out.println("If 5 passé");
				 mod_AmeExtractor.extractor.setInventorySlotContents(2, new ItemStack(mod_ItemVal.lumivert, 1));
				 }
		 }
	 }
		 }
	 }

	 System.out.println("Fin !");
	
}

/**
	 * Cette fonction est apellee lorsqu'on ferme le gui (echap) pour redonner au joueur les items qu'il a laisse dans les slots du Gui
	 */
/**
	 * Cette fonction permet au joueur d'interagir avec le block, la distance .
	 */
public boolean canInteractWith(EntityPlayer entityplayer)
{
		 return true;
}



}

TileEntityAmeExtractor :

package net.minecraft.src;
import net.minecraft.client.Minecraft;
public class TileEntityAmeExtractor extends TileEntity implements IInventory{

private ItemStack[] itemstacks = new ItemStack[6];


boolean canCraft = false;


/**
* Constructeur qui definit le nombre de slots, ect ...
*/
public TileEntityAmeExtractor()
{
	 itemstacks = new ItemStack[3];
}
/**
* Compte les Slots
*/
public int getSizeInventory() {
return itemstacks.length;
}
/**
* Permet de recuperer le contenue des Slots dans d'autres classes: TRES IMPORTANT car TRES UTILE !!!!
*/
public ItemStack getStackInSlot(int i) {
return itemstacks[i];
}
/**
* Permet de faire la gestion du clic droit. Peu etre recopier de TileEntityFurnace
*/
public ItemStack decrStackSize(int par1, int par2)
{

	 if (this.itemstacks[par1] != null)
	 {
		 ItemStack var3;
		 if (this.itemstacks[par1].stackSize <= par2)
		 {
			 var3 = this.itemstacks[par1];
			 this.itemstacks[par1] = null;
			 return var3;
		 }
		 else
		 {
			 var3 = this.itemstacks[par1].splitStack(par2);
			 if (this.itemstacks[par1].stackSize == 0)
			 {
				 this.itemstacks[par1] = null;
			 }
			 return var3;
		 }
	 }
	 else
	 {
		 return null;
	 }
}

/**
* Pareil, peu etre recopier de TileEntityFurnace
*/
public void setInventorySlotContents(int i, ItemStack itemstacks) {

this.itemstacks[i] = itemstacks;
	 if (itemstacks != null && itemstacks.stackSize > this.getInventoryStackLimit())
	 {
		 itemstacks.stackSize = this.getInventoryStackLimit();
	 }
}
/**
* Retourne le nom du Gui
*/

public String getInvName() {

return "Extracteur d'Ame";
}
/**
* retourner la taille maximum des stacks, c'est a dire 64.
*/
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void openChest() {
// TODO Auto-generated method stub

}
@Override
public void closeChest() {
// TODO Auto-generated method stub

}

/**
* Enregistre les donnees du Block : Explications :
* Reste les fonctions readFromNBT et writeToNBT. Elles servent a stocker et a lire le contenu du bloc dans les fichiers du World.
* Cela permet donc de sauvegarder l'etat du bloc dans les fichiers. Ce dont nous avons besoin, c'est stocker le niveau actuel de biere,
* mais il faut egalement appeler la classe parent pour gerer les donnees communes a tous les blocs, et faire la gestion du contenu des slots.
* L'argument de la fonction, le nbttagcompound, fait l'enregistrement et la lecture des donnees. Il a le meme fonctionnement qu'une Map.
* Il faut utiliser setShort("un nom", (short)3215) pour stocker la valeur 3215 a l'emplacement "un nom".
* On peut faire ca pour tous les types de donnees : il y a une fonction setInt, setString, etc. Pour recuperer la valeur on doit utiliser getShort("un nom").
*/

public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
	 super.readFromNBT(par1NBTTagCompound);
/**	 NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
	 this.itemstacks = new ItemStack[this.getSizeInventory()];
	 for (int var3 = 0; var3 < var2.tagCount(); ++var3)
	 {
		 NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
		 byte var5 = var4.getByte("Slot");
		 if (var5 >= 0 && var5 < this.itemstacks.length)
		 {
			 this.itemstacks[var5] = ItemStack.loadItemStackFromNBT(var4);
		 }
	 }
	
	 canCraft = par1NBTTagCompound.getBoolean("canCraft");*/
}
/**
	 * Writes a tile entity to NBT.
	 */
public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
	 super.writeToNBT(par1NBTTagCompound);
	 /** NBTTagList var2 = new NBTTagList();
	 par1NBTTagCompound.setBoolean("canCraft", canCraft);
	
	 for (int var3 = 0; var3 < this.itemstacks.length; ++var3)
	 {
		 if (this.itemstacks[var3] != null)
		 {
			 NBTTagCompound var4 = new NBTTagCompound();
			 var4.setByte("Slot", (byte)var3);
			 this.itemstacks[var3].writeToNBT(var4);
			 var2.appendTag(var4);
		 }
	 }
	 par1NBTTagCompound.setTag("Items", var2);*/
}



	 /**
	 * Acesseur de la variable level
	 */
	
	 /**
	 * Sert a determiner si le bloc est a portee
	 */
	 public boolean canInteractWith(EntityPlayer entityplayer)
	 {
		 if(worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this)
		 {
			 return false;
		 }
		 return entityplayer.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64D;
	 }

	 /**
	 * Fonction personnel, qui autorise le craft, seulement si le slot specifique au Gui AlchimistCraftingTable
	 * contient l'objet desire. Elle retourne 0 si le Slot est vide, et 1 si il contient le bon objet.Elle est
	 * apellee dans ContainerAlchimistCraftingTable juste avant le choix du fichier gerant les crafts.
	 */
	 /**
	 * ?
	 */
public ItemStack getStackInSlotOnClosing(int var1) {
		 return itemstacks[var1];
		
}



}

Thank you in advance