#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    89
    Rep Power
    2

    Error in compare


    I have an error when i compare the element of String[] to null. but i need compare them , how can i do that ?

    function where i use compare :
    Code:
    	public static void nullDel(String[] recc){
    		
    		List<String> asL= Arrays.asList(recc);
    		List<String> list= new LinkedList<String>(asL);
    		
    		for(int i= 0; i< recc.length; i++){
    			if(recc[i].equals("null")){//124 row
    				list.remove(i);
    			}
    		}
    		finRecArray= (String[]) list.toArray(new String[list.size()]);
    		
    	}
    error :
    Exception in thread "main" java.lang.NullPointerException
    at Task.nullDel(Task.java:124)
    at Task.main(Task.java:32)

    nullDel(recc);// 32nd row

    p.s. i also tried null without this: " "
    Last edited by FAQer; September 3rd, 2013 at 03:19 AM.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    The problem is that that element of the array is null. You can use a logical and statement to solve the problem.

    First check to see if the element is null before you do anything.

    Code:
    if (recc[i] != null &&
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    89
    Rep Power
    2
    Thank you. This check helped me to avoid the error , but i need to remove the null elements. not to avoid them .
    here is the output :
    1001-40
    1002-80
    1003-60
    1005-55
    1007-68
    1009-99
    1004-89
    null
    null
    null

    Can u help me with that ?
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Note that in your code, you are not removing a String from the list
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    89
    Rep Power
    2
    I see this in my output. But how can i do that ?

IMN logo majestic logo threadwatch logo seochat tools logo