Jump to content

getItems(); throws NullPointerException


Recommended Posts

Posted (edited)

It would have worked even if it was an interface.

But yeah, any method defined in the superclass can be invoked on the subclass instances, even if the static type is the superclass.

 

you know im glad you mentioned the getInventory function, whenever i use just "inventory" it will return the correct number of ores on the first cycle then the next 3 it returns 0, either way here is the final code minus the removal of generics

<T extends ItemContainer>int getOreCount ( T itemContainer, int... IDs)

{
	if (itemContainer != null)
	 return (int)itemContainer.getAmount(IDs);
	return 0;

}
 public int onLoop() throws InterruptedException

{

  log(Integer.toString(getOreCount(getInventory(),IDs[0],IDs[1])));
return random(200, 300);
}
 

which so far is working fine and ended up being way more efficient than the old code, although Im not sure if checking for a null ItemContainer reference is necessary since that never seems to be the case, tho i suppose if i don't need to check if it's null i could just narrow it down to

log(Long.toString(getInventory().getAmount(IDs[0],IDs[1])));
Edited by senpai jinkusu
Posted

 

you know im glad you mentioned the getInventory function, whenever i use just "inventory" it will return the correct number of ores on the first cycle then the next 3 it returns 0, either way here is the final code minus the removal of generics

<T extends ItemContainer>int getOreCount ( T itemContainer, int... IDs)

{
	if (itemContainer != null)
	 return (int)itemContainer.getAmount(IDs);
	return 0;

}
 public int onLoop() throws InterruptedException

{

  log(Integer.toString(getOreCount(getInventory(),IDs[0],IDs[1])));
return random(200, 300);
}
 

which so far is working fine and ended up being way more efficient than the old code, although Im not sure if checking for a null ItemContainer reference is necessary since that never seems to be the case, tho i suppose if i don't need to check if it's null i could just narrow it down to

log(Long.toString(getInventory().getAmount(IDs[0],IDs[1])));

 

The default instances of Bank, Inventory, Equipment etc. will never be null, so you don't need to nullcheck them.

"getInventory()" versus "inventory" is a matter of personal preference/convention. getInventory merely returns the inventory instance. It is the exact same thing.

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...