Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 14th, 2007, 12:09 AM   #1
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Exclamation 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;
	}

Last edited by paulchwd; Apr 14th, 2007 at 12:20 AM.
paulchwd is offline   Reply With Quote
Old Apr 14th, 2007, 12:48 AM   #2
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 171
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
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.
PhilBon is offline   Reply With Quote
Old Apr 14th, 2007, 2:01 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
@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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 5:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 10:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:11 AM.

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