Thread: Initialize ArrayList

Page 1 of 3 123 LastLast
Results 1 to 10 of 22
  1. #1 Initialize ArrayList 
    Registered Member
    Join Date
    Mar 2017
    Posts
    101
    Thanks given
    25
    Thanks received
    2
    Rep Power
    11
    Back again with yet another java question. This one has been frustrating me quite a bit.

    I have declared an ArrayList:
    Code:
    private ArrayList<Item> inventoryItems = new ArrayList<>();
    I am trying to initialize it using this method I made:
    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:
    Code:
    inventoryItems.add(new Item(1, 1));
    Any knowledge or hints is greatly appreciated.

    EDIT: More information

    Just like my last post I've created a separate class called DeathBuyBack.java:
    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()));
            }
        }
    }
    And in Player.java I've declared:
    Code:
     /**
         * Death Buyback
         */
        private DeathBuyBack deathBuyBack;
        public DeathBuyBack getDeathBuyBack() { return deathBuyBack; }
    In Player.java in the new player method:
    Code:
    deathBuyBack = new DeathBuyBack();
    AND in the player init() method:
    Code:
    if(deathBuyBack == null)
    		    deathBuyBack = new DeathBuyBack();
    deathBuyBack.setPlayer(this);
    and in the run() method:
    Code:
    deathBuyBack.init();
    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.
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Feb 2016
    Posts
    123
    Thanks given
    32
    Thanks received
    31
    Rep Power
    68
    The null pointer must be coming from one of these I'd assume.

    Code:
    inventoryItems.add(new Item(player.getInventory().getItem(i).getId(),  player.getInventory().getItem(i).getAmount()));
    EDIT: Maybe see if theres is a getItemBySlot(int slot) method
    Reply With Quote  
     

  3. #3  
    Registered Member
    Andys1814's Avatar
    Join Date
    Feb 2013
    Posts
    974
    Thanks given
    688
    Thanks received
    455
    Rep Power
    727
    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.

    Quote Originally Posted by Jaybrav View Post
    The null pointer must be coming from one of these I'd assume.

    Code:
    inventoryItems.add(new Item(player.getInventory().getItem(i).getId(),  player.getInventory().getItem(i).getAmount()));
    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.
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Nov 2015
    Age
    24
    Posts
    1,980
    Thanks given
    334
    Thanks received
    1,051
    Rep Power
    5000
    Debug.
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Registered Member
    Join Date
    Feb 2016
    Posts
    123
    Thanks given
    32
    Thanks received
    31
    Rep Power
    68
    Quote Originally Posted by Andys1814 View Post
    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.
    It was just a thought, I didn't catch anything else on the line that called the exception.
    Reply With Quote  
     

  7. Thankful user:


  8. #6  
    Registered Member
    Andys1814's Avatar
    Join Date
    Feb 2013
    Posts
    974
    Thanks given
    688
    Thanks received
    455
    Rep Power
    727
    Quote Originally Posted by Jaybrav View Post
    It was just a thought, I didn't catch anything else on the line that called the exception.
    Yeah you're right, that's why I think he's doing something in his code that he's not including in the thread or something like that.
    Reply With Quote  
     

  9. #7  
    Registered Member

    Join Date
    Dec 2012
    Posts
    2,999
    Thanks given
    894
    Thanks received
    921
    Rep Power
    2555
    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:
    Code:
    public List<Item> getList() {
    if (list == null) {
    list = new ArrayList<>();
    }
    return list;
    This is ugly as fuck and demonstrates poor quality of code, but that's the fix
    Attached image
    Reply With Quote  
     

  10. Thankful users:


  11. #8  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    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..
    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()));
            }
    }
    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.

    Edit: Kaleem already managed to post before me >.>
    Attached image
    Reply With Quote  
     

  12. #9  
    Registered Member
    Join Date
    Mar 2017
    Posts
    101
    Thanks given
    25
    Thanks received
    2
    Rep Power
    11
    Quote Originally Posted by Kris View Post
    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..
    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()));
            }
    }
    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.

    Edit: Kaleem already managed to post before me >.>
    Updated post to show that Its not the exact same as last post I don't think. Also thanks for the idea to use constants for optimization

    Quote Originally Posted by Kaleem View Post
    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:
    Code:
    public List<Item> getList() {
    if (list == null) {
    list = new ArrayList<>();
    }
    return list;
    This is ugly as fuck and demonstrates poor quality of code, but that's the fix
    Isn't it naturally mutable?
    Reply With Quote  
     

  13. #10  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by LlamaZerker View Post
    Updated post to show that Its not the exact same as last post I don't think. Also thanks for the idea to use constants for optimization



    Isn't it naturally mutable?
    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.
    Attached image
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Firemaking (unlimited fires) - ArrayList
    By tj007razor in forum Tutorials
    Replies: 60
    Last Post: 04-08-2011, 04:07 PM
  2. rs2hd - initial release
    By Graham in forum Downloads
    Replies: 320
    Last Post: 05-25-2010, 01:18 PM
  3. ArrayList
    By tj007razor in forum Application Development
    Replies: 5
    Last Post: 02-17-2009, 02:02 AM
  4. Initiate Defender Oo
    By .Zach in forum Models
    Replies: 23
    Last Post: 03-20-2008, 03:45 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •