Jump to content
roguehippo

how to use an arraylist.

Recommended Posts

Hello forums, just a note before i start, i try to look up the answers for my questions on google before i come here to ask, and ive been trying for hours to get this to work to no avial. anyways, i have an arrayList of strings for a loot list (or atleast i think i do, every time i try to log the contents i just get a null pointer). anyways. im trying to turn the arraylist into an array so i can use it to find the closest ground item. 

here is my current function to determine if i should loot a stackable item. 

 

 
the arraylist is transfered from an arraylist from the gui using this.(which is called in onStart() )
 
 if(gui.UseLooting)
       {
    log("setting looting shit");
    UseLooting = true;
       
    LootListStackable = gui.StackableLootList;
       
    LootListNotStackable = gui.NonStackableLootList;
    log("DISPLAYING LISTS");
   // log(gui.StackableLootList.size());
    //log(LootListStackable.size()); commented out because they both caused null pointers
       }
 
the arraylists are declared in the gui like so
 
public ArrayList<String>  StackableLootList;
 
and contents are added to the list in the gui when a button is pushed like so
 
 StackableLootList.add(LootToAdd.getText());
 
i literally have no idea why i get a null pointer whenever i try to do anything with the array lists in my main code. please help sad.png
 
 
 

 

Edited by roguehippo
Link to comment
Share on other sites

Post all of the relevant code so I can see your problem, and Ill answer everything in one reply.

Plz indent it properly as well, ty.

 

here is the code for my gui. 

 

http://pastebin.com/HUNyAuFY

 

the adding of the strings when the loot button is pressed is at line 281.

 

here is my main code. 

 

http://pastebin.com/HsuELXZi

 

the looting functions are at the bottom

 

thanks :)

Link to comment
Share on other sites

here is the code for my gui. 

 

http://pastebin.com/HUNyAuFY

 

the adding of the strings when the loot button is pressed is at line 281.

 

here is my main code. 

 

http://pastebin.com/HsuELXZi

 

the looting functions are at the bottom

 

thanks smile.png

 

public ArrayList<String>  StackableLootList;
public ArrayList<String>  NonStackableLootList;
 
Should be
 
public ArrayList<String>  StackableLootList = new ArrayList<>();
public ArrayList<String>  NonStackableLootList = new ArrayList<>();
  • Like 1
Link to comment
Share on other sites

 

public ArrayList<String>  StackableLootList;
public ArrayList<String>  NonStackableLootList;
 
Should be
 
public ArrayList<String>  StackableLootList = new ArrayList<>();
public ArrayList<String>  NonStackableLootList = new ArrayList<>();

 

thanks so much for getting back to me :) i didnt know that initializing it in this specific way was necesarry or even made a difference. is it easy to explain why this makes a difference? (i just want to make my future coding as good as possible)

Link to comment
Share on other sites

thanks so much for getting back to me smile.png i didnt know that initializing it in this specific way was necesarry or even made a difference. is it easy to explain why this makes a difference? (i just want to make my future coding as good as possible)

If you don't initialize a variable before you use it, it will return as null. There are some exceptions such as int, long etc.

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.

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...