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

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    CompareTo null was expected


    Code:
    public String smallest() {
    	String smallest = "";
    	for (int i = 0; i <= size(); i++) {
    		if(log[i++] == null) {
    			smallest = log[i];
    		}
    		else if(log[i].compareToIgnoreCase(log[i++]) >0) {
    			smallest = log[i++];
    		}
    		else {
    			smallest = log[i];
    		}
    	}
    	return smallest;
    }
    Code:
    public void test_1Element() {
    		strLog.insert("string 1");
    		assertEquals(strLog.smallest(), "string 1");
    	}
    The first part is the method meant to look through an array, compare the first element to the second element, and see which is smaller alphabetically, then do the same with the next element. The second is the actual test to test this.

    When I run the second as a junit test, it fails and gives me an "expected:<null> but was:<string 1>" error. Why is that? In addition...

    Code:
    public void test_3Elements_smallest_at_beginning() {
    		strLog.insert("string 1"); strLog.insert("string 2");
    		strLog.insert("string 3"); 
    		
    		assertEquals(strLog.smallest(), "string 1");
    	}
    When it gets to THAT test, it gives me an out of bounds exception with the number 5 at the end. Why is that? I've tried fixing it, but nothing I did worked, and I have no idea where to go from here.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Where is size() defined?

    Note that log[i++] is going to increment i.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Later on up in the code.

    Code:
    public int size()
      // Returns the number of Strings in this StringLog.
      {
        return (lastIndex + 1);
      }
    lastIndex is set to start out at -1, as an aside.

    And it will? As in, if the loop is running and i starts out at 0, as it should, when it runs into that if condition, it'll increment i and keep it there regardless? I've done code similar to this and that's never been the case before.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    What I mean is that whenever you use the expression i++, that will increment the value of i.

    It looks like what you want to do is compare the next element to the current. But when you use i++, the value of i++ is the current value of i, not the next value of i.

    This can be confusing since you're incrementing the loop control variable inside the body of the loop.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Ah, I see. So...how would I go about fixing it so it actually does that?

    And if that's what i++ is becoming currently, which is the current element rather than the next element, why would that make the test expect null? Wouldn't that make it expect the current element instead?
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    I'm not sure what the elements of your array are, but note that you are using i++ 3 times

    Code:
    if(log[i++] == null) {
    			smallest = log[i];
    		}
    		else if(log[i].compareToIgnoreCase(log[i++]) >0) {
    			smallest = log[i++];
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Yes, I know I am. I don't know what else I would use to see what the next element in the array is. Besides that, I'm confused about why i would increment in 'if' and 'else if' statements, or when assigning the next element to smallest.
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    You would just use i+1
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Oh...now I feel a bit silly. I managed to get it working though! Partially, at least. Now, however, there are still two failures in other tests.

    Code:
    public String smallest() {
    	String smallest = "";
    	int i = 0;
    	while(log[i] != null) {
    		final int nextIndex = i;
    		if(log[i].compareToIgnoreCase(log[nextIndex]) >0) {
    			smallest = log[nextIndex];
    		}
    		else {
    			smallest = log[i];
    		}
    		i++;
    	}
    	return smallest;
    }
    Changed it to a while loop, and made an integer so that I'm not incrementing i multiple times. When I run this, two tests are fine, but two others fail.

    Code:
    public void test_3Elements_smallest_at_beginning() {
    		strLog.insert("string 1"); strLog.insert("string 2");
    		strLog.insert("string 3"); 
    		
    		assertEquals(strLog.smallest(), "string 1");
    	}
    and

    Code:
    public void test_3Elements_smallest_in_middle() {
    		strLog.insert("string 2"); strLog.insert("string 1");
    		strLog.insert("string 3"); 
    		
    		assertEquals(strLog.smallest(), "string 1");
    	}
    both fail. I'll try my former code with a simple i+1 instead of i++ in the meantime and see if that changes anything else.

    EDIT: i+1 still gives me the problems I was running into before, so whatever is causing this isn't that.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I think you should take a break, sit down with a piece of paper and get clear about what you wanna do. Draw a diagram, write down some pseudo code, whatever. When you got a concrete plan and you're sure that it's correct, then you start typing on your keyboard. Not before.

    Don't try to write code with trial-and-error. This sometimes works with very simply programs, but most of the time it doesn't. And it always leads to very poor results.

    Leaving aside all the confusion regarding i++ and i + 1 and nextIndex = i (I guess that should be i + 1): The whole approach simply doesn't work. You cannot find the smallest item by comparing each subsequent pair.

    Have a look at this sequence, for example:

    Code:
    1 5 7
    According to your logic, the smallest item is 5, because

    1 < 5 leads to smallest = 1
    5 < 7 leads to smallest = 5

    I'm not sure what happens when your nextIndex exceeds the array bounds. Your application probably crashes.

    Anyway, it's obvious that the comparison doesn't work out. So take a piece of paper, write down a sequence of numbers and try to come up with an approach to find the smallest number. It's actually very simple.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Actually, comparison DOES seem to be working out.

    Code:
    public String smallest() {
    	
    	String smallest = log[0];
    	int numTimes = 0;
    	
    	for (int i = 1; i < log.length; i++) {	
    		int nextIndex = i+1;
    	    if (log[nextIndex] != null && smallest.compareToIgnoreCase(log[i]) > 0) {
    	        smallest = log[i];
    	    }
    	    else if(log[nextIndex] == null) {
    	    	break;
    	    }
    	    numTimes = i;
    	}
    	
    	if(smallest.compareToIgnoreCase(log[numTimes]) > 0) {
    		smallest = log[numTimes];
    	}
    	return smallest;
    }
    With that method, 3 of the 4 tests succeed. The only one that fails is:

    Code:
    public void test_3Elements_smallest_at_end() {
    		strLog.insert("string 3"); strLog.insert("string 2");
    		strLog.insert("string 1"); 
    		
    		assertEquals(strLog.smallest(), "string 1");
    	}
    It expects, for whatever reason "string 2" and not "string 1", which tells me it's a problem with my code rather than with it not being possible.

    Shouldn't my if statement at the end of the first bit of code make up for the fact that the last element is skipped if the one after it is null? And thus shouldn't "string 1" still get caught and compared to "string 2"?

IMN logo majestic logo threadwatch logo seochat tools logo