Jump to content

[1.7.10] Problems converting GuiTextField to Integer


Ms_Raven

Recommended Posts

I have a shop screen where a player enters into text fields the amount of certain items they want to buy, and it removes non-numbers then converts the fields into integers so they can be used with addItemStackToInventory.

 

But it's giving me a NullPointerException on my convertToInteger() method even though I make sure they're not null...

Everything is initialised as 0 or "0" and 'amount1/2/3/4' are the text fields.

I can't figure out what I'm doing wrong.

 

 

//When clickin on the "Buy" button

protected void actionPerformed(GuiButton button)

{

MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();

World world = server.worldServers[0];

EntityPlayer player = Minecraft.getMinecraft().thePlayer;

int i = (int)player.posX;

int j = (int)player.posY;

int k = (int)player.posZ;

 

if (button.id == 0)

{

checkout(player);

 

}

}

 

//Checks all entered values, if the player has inventory room and the total price

//then spends the money and gives the player the items.

public void checkout(EntityPlayer player)

{

ExtendedPlayer props = ExtendedPlayer.get(player);

int posX = (this.width) /2;

int posY = (this.height) /2;

 

correctValues();

fixValues();

convertToInteger();

checkValues();

toMoney();

 

totalPrice = (paid1+paid2+paid3+paid4);

 

if(inventoryHasRoom(player))

{

if(props.getMoney() >= (totalPrice))

{

if(final_amount1>0){player.inventory.addItemStackToInventory(new ItemStack(HorrorReg.haloOfTheSun, final_amount1));}

if(final_amount2>0){player.inventory.addItemStackToInventory(new ItemStack(Main.bomb, final_amount2));}

if(final_amount3>0){player.inventory.addItemStackToInventory(new ItemStack(Main.miningHelmet, final_amount3));}

if(final_amount4>0){player.inventory.addItemStackToInventory(new ItemStack(HorrorReg.backpack, final_amount4));}

 

props.spendMoney(totalPrice);

 

this.fontRendererObj.drawString("Purchased!", posX+(82), posY+(40), 0xfff000);

}

else

{

this.fontRendererObj.drawString("You can't afford that.", posX+(70), posY+(40), 0xfff000);

 

}

}

else

{

this.fontRendererObj.drawString("You don't have enough room.", posX+(76), posY+(40), 0xfff000);

 

}

}

 

//Converts the text fields into strings and sets empty fields to 0.

public void correctValues()

{

text_amount1 = amount1.getText();

text_amount2 = amount2.getText();

text_amount3 = amount3.getText();

text_amount4 = amount4.getText();

 

if(amount1.getText().isEmpty())

{

text_amount1 = "0";

}

if(amount2.getText().isEmpty())

{

text_amount2 = "0";

}

if(amount3.getText().isEmpty())

{

text_amount3 = "0";

}

if(amount4.getText().isEmpty())

{

text_amount4 = "0";

}

}

 

//Gets rid of letters from the text fields so they can be turned into integers.

public void fixValues()

{

if(text_amount1.contains("\\D"))

{

Tools.println("Original string: " + text_amount1 + " becomes: " + text_amount1.replaceAll("\\D", ""));

text_amount1 = text_amount1.replaceAll("\\D", "");

if(text_amount1 == null)

{

text_amount1 = "0";

}

}

if(text_amount2.contains("\\D"))

{

Tools.println("Original string: " + text_amount2 + " becomes: " + text_amount2.replaceAll("\\D", ""));

text_amount2 = text_amount2.replaceAll("\\D", "");

if(text_amount2 == null)

{

text_amount2 = "0";

}

}

if(text_amount3.contains("\\D"))

{

Tools.println("Original string: " + text_amount3 + " becomes: " + text_amount3.replaceAll("\\D", ""));

text_amount3 = text_amount3.replaceAll("\\D", "");

if(text_amount3 == null)

{

text_amount2 = "0";

}

}

if(text_amount4.contains("\\D"))

{

Tools.println("Original string: " + text_amount4 + " becomes: " + text_amount4.replaceAll("\\D", ""));

text_amount4 = text_amount4.replaceAll("\\D", "");

if(text_amount4 == null)

{

text_amount4 = "0";

}

}

}

 

//Converts the converted text fields into integers.

public void convertToInteger()

{

final_amount1 = Integer.getInteger(text_amount1);

final_amount2 = Integer.getInteger(text_amount2);

final_amount3 = Integer.getInteger(text_amount3);

final_amount4 = Integer.getInteger(text_amount4);

}

 

//Checks the amount of stacks will needed to be put into the inventory

//and sets any entered numbers above 64 to 64.

public void checkValues()

{

int stacks = 0;

if(final_amount1>0){stacks++;};

if(final_amount2>0){stacks++;};

if(final_amount3>0){stacks++;};

if(final_amount4>0){stacks++;};

if(final_amount1>64){final_amount1=64;};

if(final_amount2>64){final_amount2=64;};

if(final_amount3>64){final_amount3=64;};

if(final_amount4>64){final_amount4=64;};

}

 

//Take the amount of items wanted and multiply them by their price to get the total cost.

public void toMoney()

{

paid1 = final_amount1 * 1500;

paid2 = final_amount2 * 50;

paid3 = final_amount3 * 500;

paid4 = final_amount4 * 750;

}

 

//Goes through the inventory to find empty space for the amount of itemstacks being purchased

public boolean inventoryHasRoom(EntityPlayer player)

{

int emptySlots = 0;

int neededSlots = (final_amount1+final_amount2+final_amount3+final_amount4);

for(int i = 0; i <35; i++)

{

if(player.inventory.getStackInSlot(i) != null)

{

emptySlots++;

}

}

if(emptySlots > neededSlots)

{

return true;

}

else return false;

}

 

Link to comment
Share on other sites

You know how to code, but your recent posts show you're missing a consistent approach to debugging.

 

Based on the faith that computers are completely logical and exact, you can solve most bugs by tracing the logic.

 

You should either use your IDE debug mode to watch the values of the fields of interest, or I personally tend to just add console statements (System.out.println()) all over the place. I put a console statement at the beginning of every method to confirm that the method is being called, I put a console statement inside every if statement to confirm whether the condition was met, and I put a console statement after every major change in field value.

 

So I personally would put console statements in your correctValues() and fixValues() methods after every line to output what the previous line actually did. I guarantee you that the problem will be obvious if you have good tracing through this method.

 

Once you learn this technique of tracing the logic, you won't every have to come to forums again for a null pointer exception problem.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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