The null pointer must be coming from one of these I'd assume.
EDIT: Maybe see if theres is a getItemBySlot(int slot) methodCode:inventoryItems.add(new Item(player.getInventory().getItem(i).getId(), player.getInventory().getItem(i).getAmount()));
|
Back again with yet another java question. This one has been frustrating me quite a bit.
I have declared an ArrayList:
I am trying to initialize it using this method I made:Code:private ArrayList<Item> inventoryItems = new ArrayList<>();
Code:public void init() { for (int i = 0; i < 28; i++) { if(player.getInventory().getItem(i) != null) inventoryItems.add(new Item(player.getInventory().getItem(i).getId(), player.getInventory().getItem(i).getAmount()));//null pointer here } }
I keep getting a nullPointerException and I'm pretty sure its because I'm calling "add" on a null ArrayList, but I don't know how to properly initialize an ArrayList.
I should also point out that I don't want to fill the arraylist using multiple lines of:
Any knowledge or hints is greatly appreciated.Code:inventoryItems.add(new Item(1, 1));
EDIT: More information
Just like my last post I've created a separate class called DeathBuyBack.java:
And in Player.java I've declared:Code:import com.rs.game.player.Player; import java.io.Serializable; import java.util.ArrayList; public class DeathBuyBack implements Serializable { private static final long serialVersionUID = -908683410389745985L; private Player player; public void setPlayer(Player player) { this.player = player; } private ArrayList<Item> inventoryItems = new ArrayList<>(); public ArrayList<Item> getInventoryItems() { return inventoryItems; } private ArrayList<Item> equipmentItems = new ArrayList<>(); public ArrayList<Item> getEquipmentItems() { return equipmentItems; } public void init() { for (int i = 0; i < 28; i++) { if(player.getInventory().getItem(i) != null) inventoryItems.add(new Item(player.getInventory().getItem(i).getId(), player.getInventory().getItem(i).getAmount())); } for (int i = 0; i < 14; i++) { if(player.getEquipment().getItem(i) != null) equipmentItems.add(new Item(player.getEquipment().getItem(i).getId(), player.getEquipment().getItem(i).getAmount())); } } }
In Player.java in the new player method:Code:/** * Death Buyback */ private DeathBuyBack deathBuyBack; public DeathBuyBack getDeathBuyBack() { return deathBuyBack; }
AND in the player init() method:Code:deathBuyBack = new DeathBuyBack();
and in the run() method:Code:if(deathBuyBack == null) deathBuyBack = new DeathBuyBack(); deathBuyBack.setPlayer(this);
Maybe I should try initializing in the init method? I only put it in the run method because thats where a lot of other stuff was being initialized.Code:deathBuyBack.init();
The null pointer must be coming from one of these I'd assume.
EDIT: Maybe see if theres is a getItemBySlot(int slot) methodCode:inventoryItems.add(new Item(player.getInventory().getItem(i).getId(), player.getInventory().getItem(i).getAmount()));
You initialized the ArrayList perfectly fine.
I think you're saying one thing in this thread and doing something else, because otherwise I don't see how you could get a NPE from this. I initially thought it may be because the item in the inventory was null, but you covered that so good on you.
I am honestly really confused on this just as you are. Maybe someone else can offer help. If you want me to take a look on teamviewer i'd be down because I really want to see what the issue is.
Not possible. Based on the line the NPE is calling, it isn't an issue with the player.getInventory() being null, because otherwise it would have called the NPE on the if statement. And I'm sure you're smart enough to know that primitive types cannot hold null values.
Debug.
I'm assuming you're using Matrix or something of the sort, and iirc Serializable fucks inliner stuff up. That means you have to make the list mutable, and then within your getter have some ugly shit like:
This is ugly as fuck and demonstrates poor quality of code, but that's the fixCode:public List<Item> getList() { if (list == null) { list = new ArrayList<>(); } return list;
Considering this is matrix(most likely), I think the solution to this problem is the exact same as in the last thread you created. You're not initializing the list for existing players. Matrix servers do not create a new player object for players that already exist. They load up a serialized file. If the file was made before that field was added, the field will be null on your character, throwing the exception. You need to initialize it in whatever method is always executed on login, if the variable is null of course.
Additionally, the code is a bit sloppy. I suggest..
It's over exaggeration of course, but try to grow onto these small habits. In small cases such as this it really doesn't matter, but if you start dealing with large loops, you'll want to gain every nanoseconds you possibly can, as it can have a huge impact on the cycle time otherwise.Code:private final List<Item> inventoryItems = new ArrayList<Item>(28); .. public void init() { final Inventory inventory = player.getInventory(); for (int i = 0; i < 28; i++) { final Item item = inventory.getItem(i); if (item == null) continue; inventoryItems.add(new Item(item.getId(), item.getAmount())); } }
Edit: Kaleem already managed to post before me >.>
It'd really help if you'd rather show the error that's being thrown, full format, and show us the lines it's stemming from (the closest location to the error that you can access - meaning within source), plus some lines before the error line and after that line, point out which line the error stems from though.
Often times the help threads are misleading, which could very well be the issue here.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |