roguehippo Posted May 21, 2016 Share Posted May 21, 2016 (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. 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, 2016 by roguehippo Quote Link to comment Share on other sites More sharing options...
Tom Posted May 21, 2016 Share Posted May 21, 2016 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. Quote Link to comment Share on other sites More sharing options...
Disposition Posted May 21, 2016 Share Posted May 21, 2016 Make sure that when you declare the list you instantiate it, i.e. public ArrayList<String> StackableLootList = new ArrayList<>(); Quote Link to comment Share on other sites More sharing options...
FrostBug Posted May 21, 2016 Share Posted May 21, 2016 .NET conventions in Java .. Anyway, we need to see whats going on in the gui class to identify the problem 2 Quote Link to comment Share on other sites More sharing options...
roguehippo Posted May 22, 2016 Author Share Posted May 22, 2016 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 Quote Link to comment Share on other sites More sharing options...
Tom Posted May 22, 2016 Share Posted May 22, 2016 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<>(); 1 Quote Link to comment Share on other sites More sharing options...
Woody Posted May 22, 2016 Share Posted May 22, 2016 String[] lootArray = new String[stackableLootList.size()]; StackableLootList.toArray(lootArray); 1 Quote Link to comment Share on other sites More sharing options...
roguehippo Posted May 23, 2016 Author Share Posted May 23, 2016 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) Quote Link to comment Share on other sites More sharing options...
Woody Posted May 23, 2016 Share Posted May 23, 2016 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. Quote Link to comment Share on other sites More sharing options...
roguehippo Posted May 24, 2016 Author Share Posted May 24, 2016 i guess something else is wrong because im still getting null pointers when i do anything with the grounditem Quote Link to comment Share on other sites More sharing options...
GaetanoH Posted May 24, 2016 Share Posted May 24, 2016 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. Quote Link to comment Share on other sites More sharing options...
Woody Posted May 24, 2016 Share Posted May 24, 2016 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 Quote Link to comment Share on other sites More sharing options...
roguehippo Posted May 25, 2016 Author Share Posted May 25, 2016 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. -___- Quote Link to comment Share on other sites More sharing options...
Acerd Posted May 25, 2016 Share Posted May 25, 2016 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 Quote Link to comment Share on other sites More sharing options...