BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-26-2008, 07:25 PM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2007
Location: montreal
Model: 8100
PIN: N/A
Carrier: rogers
Posts: 70
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default There's gotta be a simple solution to this array problem!

Please Login to Remove!

I have an EditField array which I use to get 4 different numeric inputs from the user. I use a for loop to initialize:

Code:
for (int i=0; i<4; i++)
   {
      myArray[i] = new EditField("", "0", 4, EditField.EDITABLE);          
    }
As you can see the default value is 0. I convert whatever the user enters to a double value and then do some calculations which works just fine. That is unless the user enters a null value or just deletes the default 0 value. Then I get an exception thrown because the array does not contain a valid number.

To avoid this I added the following code to check for a null value:

Code:
if(myArray[0].getText()=="") myArray[0].setText("0");
However I get this error now:
Uncaught exception thrown - NumberFormatException
It seems to me that the if statement is not triggering the then statement to set it back to the default 0 value.

I'm sure I'm making a very basic java mistake. Can anyone help me?

Thanks
Offline  
Old 08-26-2008, 08:19 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

Well the way your code is written you are only protecting the first EditField (myArray[0]) from bad user input.

It is very difficult to debug Java exceptions unless one has at least the code in which the exception is thrown, and usually a good chunk of the (logically) preceding code as well.
Offline  
Old 08-26-2008, 09:09 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2007
Location: montreal
Model: 8100
PIN: N/A
Carrier: rogers
Posts: 70
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm only showing the first array. The other 3 are tested also using the same statement. Besides, when I test the application I only change myArray[0] to null; the others remain 0.

When they're all 0 I get no errors and that's how I isolated the problem to the null value.

The arrays are not used at all between the two statements I posted so I didn't think it necessary to show the extra code.

For what its worth the calculation performed is:

Code:
totals = totals + Double.valueOf(myArray[i].getText()).doubleValue();
Variable totals is declared as a double.

Thanks hrbuckley...
Offline  
Old 08-26-2008, 09:20 PM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2007
Location: montreal
Model: 8100
PIN: N/A
Carrier: rogers
Posts: 70
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well I found a workaround for those interested.

I replaced the test for null with this:

Code:
if(String.valueOf(myArray[0].getText()).length()==0) myArray[0].setText("0");
Works great.

hrbuckley, thanks again for your input.
Offline  
Old 08-26-2008, 10:51 PM   #5 (permalink)
New Member
 
Join Date: Oct 2007
Model: BOLD
PIN: N/A
Carrier: Rogers
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by dynot View Post
Code:
if(myArray[0].getText()=="") myArray[0].setText("0");
1.string is compared with equals

Code:
"".equals(myArray[0].getText());
2. "" is not the same as null
Offline  
Closed Thread


Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.