Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java "null" error

    Hi guys,
    Here is my code :
    Code:
    try{
                                    String items = _perso.getItemsIDSplitByChar(";");
                                	String[] item = items.split(";");
                                	String lastOne = item[item.length-1];
                                    int rand;
                                	int itemRanded = 0;
                                	for(int i = 0; i < item.length; i++)
                                	{
                                		if(item.length == 0)
                                		{
                                		System.out.print("No items");
                                		}
                                		else
                                		{
                                		 rand = 1+(int)(Math.random()*((item.length-1) + 1));
                                		 itemRanded = Integer.parseInt(item[rand-1]);
                                		}
                                	}
    System.out.print("Here is a randed item : " + itemRanded);
    }
    catch(Exception e)
    {
    System.out.print("Error : " + e.getMessage().toString());
    }
    I got error in my console "null", i don't know where is the wrong code ..
    Thank's

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
            String s "1;17;22;14;2";
            List<
    String> list = Arrays.asList(s.split(";"));
            
    Collections.shuffle(list);
            
    System.out.println("Random item: " + list.get(0)); 
    For a simple example.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •