Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2013, 11:43 AM   PM User | #1
bullet1
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bullet1 is an unknown quantity at this point
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
bullet1 is offline   Reply With Quote
Old 01-08-2013, 01:59 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-08-2013, 06:20 PM   PM User | #3
bullet1
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bullet1 is an unknown quantity at this point
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
bullet1 is offline   Reply With Quote
Old 01-08-2013, 07:26 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:10 PM.


Advertisement
Log in to turn off these ads.