Java "null" error
Here is my code :
I got error in my console "null", i don't know where is the wrong code ..
String items = _perso.getItemsIDSplitByChar(";");
String item = items.split(";");
String lastOne = item[item.length-1];
int itemRanded = 0;
for(int i = 0; i < item.length; i++)
if(item.length == 0)
rand = 1+(int)(Math.random()*((item.length-1) + 1));
itemRanded = Integer.parseInt(item[rand-1]);
System.out.print("Here is a randed item : " + itemRanded);
System.out.print("Error : " + e.getMessage().toString());
Drag the stack out by using e.printStackTrace() instead. Also, never pull the toString off of the exception's getMessage; it is possible for that to be null.
Once you have the stack it should give you the line number. Since there are several items here that can throw exceptions you need to determine which it is. To me the only one that makes sense is the split method. The array itself would have one result even on an empty string, but if the string is null to start with than you cannot split it.
You can also place a breakpoint on the try and walk it through a debugger to confirm.
I have resolved the problem but i don't know where was the error line^^ i have remake from 0 the code, anyway thank's for your reply Fou-Lu :)
Yep, you bet.
You can simplify it as well by converting a collection out of it. Collections can be shuffled, so you could simply convert the array to a list, and then shuffle the list and pull the first item.
For a simple example.
String s = "1;17;22;14;2";
List<String> list = Arrays.asList(s.split(";"));
System.out.println("Random item: " + list.get(0));