Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   String compare (http://www.programmingforums.org/showthread.php?t=12991)

paulchwd Apr 14th, 2007 1:09 AM

String compare
 
Hi All,

This gives me a null pointer exception:

:

  System.out.println("\nThere are " + polyInstance.signChanges(tempCoefsArray,  noCoefs) + " positive real roots in the equation you entered\n");

......other non important code here.....

public int signChanges(String tempCoefsArray[], int noCoefs)
        {

                int signChanges=0;         
   
                        for(int i=0; i<=tempCoefsArray.length-2; i++)
                        {
                              if(tempCoefsArray[i].charAt(0) != tempCoefsArray[i+1].charAt(0))//null ptr here
                                {

                                        signChanges++;
                                }
                               
                        }
                return signChanges;
        }


PhilBon Apr 14th, 2007 1:48 AM

I'm not sure if it is the same with Chars as it is with strings, but I know with strings, you have to do
:

tempCoefsArray[i].charAt(0).equals
or something like it. You have to have .equals (not sure about case) after the var, but with the .charAt, i'm not sure if it goes before or after.

titaniumdecoy Apr 14th, 2007 3:01 AM

@PhilBon: The OP is correctly comparing chars with the != operator; however you are correct that in most cases Strings should be compared with the equals method.

@paulchwd: It appears one of the elements in the tempCoefsArray is null. If you did not intend this to be the case you should review the code in which the argument is generated. However, if you accept the possibility that one of the elements of tempCoefsArray might be null, you should check for that before comparing each value.

Also, the second argument to the signChanges method is unused within the method, and if it represents what its name would indicate (the length of the array) the information it provides is redundant.


All times are GMT -5. The time now is 2:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC