May 21, 20169 yr 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. http://pastebin.com/rgxNpPbf 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 Edited May 21, 20169 yr by roguehippo
May 21, 20169 yr 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.
May 21, 20169 yr Make sure that when you declare the list you instantiate it, i.e. public ArrayList<String> StackableLootList = new ArrayList<>();
May 21, 20169 yr .NET conventions in Java .. Anyway, we need to see whats going on in the gui class to identify the problem
May 22, 20169 yr Author 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
May 22, 20169 yr 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 public ArrayList<String> StackableLootList; public ArrayList<String> NonStackableLootList; Should be public ArrayList<String> StackableLootList = new ArrayList<>(); public ArrayList<String> NonStackableLootList = new ArrayList<>();
May 22, 20169 yr String[] lootArray = new String[stackableLootList.size()]; StackableLootList.toArray(lootArray);
May 23, 20169 yr Author 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)
May 23, 20169 yr 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) 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.
May 24, 20169 yr Author i guess something else is wrong because im still getting null pointers when i do anything with the grounditem
May 24, 20169 yr i guess something else is wrong because im still getting null pointers when i do anything with the grounditem Just implement enough checks to see if the GroundItems are null? Just check out your logs there would be the exact line the program is throwing a NullPointer.
May 24, 20169 yr i guess something else is wrong because im still getting null pointers when i do anything with the grounditem You have to define and do a null check before using it
May 25, 20169 yr Author 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. -___-
May 25, 20169 yr 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
Create an account or sign in to comment