Jump to content

getItems(); throws NullPointerException


senpai jinkusu

Recommended Posts

//can anyone explain how to fix my code? 

 

<T extends ItemContainer> int getOreCount (int mode,T itemContainer)

{

try
{
 Item Items [] = itemContainer.getItems();
....
....
return oreCount;
}
catch (NullPointerException e)
  {
log("NullPointerExeption");
}
 
 
return -1;
}

 

//thanks in advance

Edited by senpai jinkusu
Link to comment
Share on other sites

//can anyone explain how to fix my code? 

 

<T extends ItemContainer> int getOreCount (int mode,T itemContainer)

{

try
{
 Item Items [] = itemContainer.getItems();
....
....
return oreCount;
}
catch (NullPointerException e)
  {
log("NullPointerExeption");
}
 
 
return -1;
}

 

//thanks in advance

I believe I've had this issue before and I solved it by adding into the item check code, 

if item!=null
    execute item check
else
    do something

I might let someone more experienced wade in, but that should be a quick thing to test in the interim :).

Best of luck with it!

Link to comment
Share on other sites

Log the stack trace or the error log.

I see you are loving the word npe. Are you sure it's a npe and not anything else

 

before it threw the exception the client would crash and recursively print errors and I'm sure it led to an npe, which let me know what exception to catch

 

here's the complete code:

http://pastebin.com/hTznFb8C

 

here's the stack trace:

https://gyazo.com/92cc578ca49b9137652665f9c3479bd4

 

 

Show us the stacktrace

 

Are you certain that your itemContainer instance isn't null?

 

not sure yet, not sure how it could be null either

Edited by senpai jinkusu
Link to comment
Share on other sites

before it threw the exception the client would crash and recursively print errors and I'm sure it led to an npe, which let me know what exception to catch

 

here's the complete code:

http://pastebin.com/hTznFb8C

 

here's the stack trace:

https://gyazo.com/92cc578ca49b9137652665f9c3479bd4

 

That indentation gave me cancer :/

 

Also, why are you using generics in that method? It's entirely redundant from what I can tell.

 

Anyway; the problem is probably that you need to nullcheck the items here:

                for (Item i : Items)
                {
                if (i.getId() == 440 || i.getId() == 441)

Regardless, using getInventory().getAmount(440, 441); would be quite a bit cleaner

 

  • Like 2
Link to comment
Share on other sites

if im checking the amount of coal in the players bank , players inventory ,etc then i would just have to call getOres(arg0,  ItemContainer subclass)

 

The getItems method is declared in the ItemContainer superclass. There's no need for generics

Generics are generally bad for performance (unless the compiler sees that it's silly and removes it), so try avoiding them where they're redundant

 

Link to comment
Share on other sites

I believe I've had this issue before and I solved it by adding into the item check code, 

if item!=null
    execute item check
else
    do something

I might let someone more experienced wade in, but that should be a quick thing to test in the interim smile.png.

Best of luck with it!

this just worked without generics, ill try with generics this time and see the results, it seems that empty inventory slots are null rather than a default value, never thought of that

The getItems method is declared in the ItemContainer superclass. There's no need for generics

 

this is where I am confused, if you call the getItems method from the upperClass what are you checking exactly?(inventory,bank,etc) , also how can i call the getItems method from the itemContainer superClass if one can't instantiate an abstract class? the only way is to use one of the subclass methods

Edited by senpai jinkusu
Link to comment
Share on other sites

this just worked without generics, ill try with generics this time and see the results, it seems that empty inventory slots are null rather than a default value, never thought of that

this is where I am confused, if you call the getItems method from the upperClass what are you checking exactly?(inventory,bank,etc) , also how can i call the getItems method from the itemContainer superClass if one can't instantiate an abstract class? the only way is to use one of the subclass methods

That's really up to you, you can change it in the first part. I.e.

inventory.getItems()

or

bank.getItems()

If you check in the API and look at each of the types (inventory, bank etc) you can determine what methods you can call from them. In that case, .getItems() is predefined for both so you don't have to create any custom code (unless you have a reason I'm overlooking, and I'm still learning all of this myself so that's possible)

Edited by omni4life
Link to comment
Share on other sites

That's really up to you, you can change it in the first part. I.e.

Inventory.getItems()

or

Bank.getItems()

If you check in the API and look at each of the types (inventory, bank etc) you can determine what methods you can call from them. In that case, .getItems() is predefined for both so you don't have to create any custom code (unless you have a reason I'm overlooking, and I'm still learning all of this myself so that's possible)

that's the whole point of me using generics , T is a type parameter, meaning it can be any type that extends ItemContainer that way 

inventory.getItems();
or
bank.getItems();

could be replaced with: (note the T would be replaced by a variable such as "foo",etc)

T.getItems();

one last note, this seems to work with generics , however getItems always seems to default to 1 when i use the bank instance

Edited by senpai jinkusu
Link to comment
Share on other sites

that's the whole point of me using generics , T is a type parameter, meaning it can be any type that extends ItemContainer that way

 

Just use ItemContainer in the method signature. eg.

 

public int getOreCount(ItemContainer container, int... ids) {

    return container.getAmount(ids);

}

 

and use it like so:

int invCoal = getOreCount(inventory, 453, 454);

int bankIron = getOreCount(bank, 440, 441);

 

the methods getAmount, getItems, contains etc. etc. are defined in the superclass, however if overridden in the subclass, invoking the method will still work even if your reference has the superclass as static type.

 

In other words, these are the same:

getInventory().getAmount(id);

((ItemContainer)getInventory()).getAmount(id);

  • Like 2
Link to comment
Share on other sites

that's the whole point of me using generics , T is a type parameter, meaning it can be any type that extends ItemContainer that way

inventory.getItems();orbank.getItems();
could be replaced with: (note the T would be replaced by a variable such as "foo",etc)

T.getItems();
one last note, this seems to work with generics , however getItems always seems to default to 1 when i use the bank instance
You don't get it then -.-

ItemContainer is a abstract class that have methods that other class extends. So rather then typing those same method inside each class again. they just simply extend ItemContainer. Now that we have enough classes extending ItemContainer. We could use oop rather than genetics. You just simply grab and feed an instance of the class. Then you can do whatever you want with it.

Genetics is used more for our filters

http://osbot.org/forum/topic/63619-oop-tutorial/

Edited by jos3dpay
Link to comment
Share on other sites

I think my misunderstanding was thinking of the ItemContainer as an interface rather than a class, so I was only limited to those methods explicitly stated by the API under a given child class, so does this mean I can call any ItemContainer method and expect a relevant result?, also that link cleared a lot of things up for me as well

Edited by senpai jinkusu
Link to comment
Share on other sites

I think my misunderstanding was thinking of the ItemContainer as an interface rather than a class, so I was only limited to those methods explicitly stated by the API under a given child class, so does this mean I can call any ItemContainer method and expect a relevant result?, also that link cleared a lot of things up for me as well

 

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.

 

Edited by FrostBug
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

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