senpai jinkusu Posted August 22, 2015 Share Posted August 22, 2015 (edited) //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 August 22, 2015 by senpai jinkusu Quote Link to comment Share on other sites More sharing options...
omni4life Posted August 22, 2015 Share Posted August 22, 2015 //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! Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 22, 2015 Share Posted August 22, 2015 Show us the stacktrace Are you certain that your itemContainer instance isn't null? Quote Link to comment Share on other sites More sharing options...
Joseph Posted August 22, 2015 Share Posted August 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
senpai jinkusu Posted August 22, 2015 Author Share Posted August 22, 2015 (edited) 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 August 22, 2015 by senpai jinkusu Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 22, 2015 Share Posted August 22, 2015 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 2 Quote Link to comment Share on other sites More sharing options...
senpai jinkusu Posted August 22, 2015 Author Share Posted August 22, 2015 Also, why are you using generics in that method? It's entirely redundant from what I can tell. 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) Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 22, 2015 Share Posted August 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
senpai jinkusu Posted August 22, 2015 Author Share Posted August 22, 2015 (edited) 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! 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 August 22, 2015 by senpai jinkusu Quote Link to comment Share on other sites More sharing options...
omni4life Posted August 22, 2015 Share Posted August 22, 2015 (edited) 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 August 22, 2015 by omni4life Quote Link to comment Share on other sites More sharing options...
senpai jinkusu Posted August 22, 2015 Author Share Posted August 22, 2015 (edited) 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 August 22, 2015 by senpai jinkusu Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 22, 2015 Share Posted August 22, 2015 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); 2 Quote Link to comment Share on other sites More sharing options...
Joseph Posted August 22, 2015 Share Posted August 22, 2015 (edited) 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 instanceYou 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 August 22, 2015 by jos3dpay Quote Link to comment Share on other sites More sharing options...
senpai jinkusu Posted August 22, 2015 Author Share Posted August 22, 2015 (edited) 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 August 22, 2015 by senpai jinkusu Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 22, 2015 Share Posted August 22, 2015 (edited) 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 August 22, 2015 by FrostBug Quote Link to comment Share on other sites More sharing options...