Jump to content

how to use an arraylist.


Recommended Posts

Posted (edited)

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
Posted

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 :)

Posted

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
Posted

 

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)

Posted

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.

Posted

Im posting here just so anyone else that has this problem can hopefully find the answer. After looking on many forums i found out the problem was actually due to an import mistake. i accidently imported like java.awt.List instead of java.util.List. -___-

classic beginner mistake :boge:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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