• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot format given Object as a Number

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hopefully this is a simple one. I'm new to Java so it wouldn't surprise me if I'm just missing something obvious. I've got a JFormattedTextField and a NumberFormat defined as follows:



When this code runs, I get the following exception:

Exception in thread "Win32SerialPort Notification thread" java.lang.IllegalArgum
entException: Cannot format given Object as a Number

The exception is being thrown when the following line is executed:



value is a String. I've tried different values, from 0 to 100 to 150 and they all cause the exception.

I just want a simple field to edit and display a value from 0 to 999999. I thought that using NumberFormat.getIntegerInstance would get me that, but it doesn't seem too happy. Any ideas?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dig the example from the API Documentation:


For example, in the following code an appropriate AbstractFormatterFactory and AbstractFormatter will be created to handle formatting of numbers:

JFormattedTextField tf = new JFormattedTextField();
tf.setValue(new Number(100));



It's expecting a numeric object, a subclass of java.lang.Number, and you are passing it a String.
 
Jeff Allison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's expecting a numeric object, a subclass of java.lang.Number, and you are passing it a String.



Thanks for the reply, Joe, but I'm still having trouble. I can pass an int to the method and get the same problem. I can remove the format from the text field and it works with either a String or an int. In other words, when I have this:



things work fine, although the displayed field looks bad. Perhaps there is some other way to control the appearance of the field. But I'd like to understand what is going on with the format and what is expected in terms of an argument to setValue().
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Allison:

I can remove the format from the text field and it works with either a String or an int.


You aren't removing the formatter. If you don't set it it will be determined from the type of argument you pass to setValue() (again, read the API doc above). If you pass a String first it probably just invokes toString() on the argument to subsequent setValue() calls.



I'd like to understand what is going on with the format and what is expected in terms of an argument to setValue().


I guess the question is: what do you expect the formatter to do? This code works fine for me:

Field appears as: 1,234,567
 
Jeff Allison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll RTFM and figure it out. Thanks.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the Java Tutorial article How To Use Formatted Text Fields for the bigger picture.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic