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

    Join Date
    Feb 2005
    Rep Power

    Post java.lang.IllegalArgumentException: Cannot format given Object as a Number

    hey ppl i have a JTable and i want to format the content of cells, the contents is stored as an object... i keep getting this error when i click on a particular cell and the decimal format is also lost in the process can nebody help

    private DecimalFormat m_numberFormat = null;
    protected void setValue (Object value)
    String retValue = null;
    retValue = m_numberFormat.format(value);

    sorry i cannot send u the whole code as its very large...
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Chicago, IL
    Rep Power
    First, m_numberFormat is null given the code you've provided, so you should be getting a NullPointerException. Needless to say, you probably initialize that reference variable somewhere; it's generally helpful to post that code.

    Second, your setValue() method effectively does nothing. You initialize retValue to null. This is not necessary, as you immediately reassign retValue to the result of your formatting. However, nothing is then done with retValue.

    Third if the Object value is not something DecimalFormat can handle, you'll get the error you receive. In other words, it's not getting a recognizable number. You evidently require the value to be something numeric, so why not change the method parameter to some kind of number; e.g. BigDecimal, double, etc.?

    Finally, if the whole code is so large that you can't send the relevant portions of your problem, that should be an instant red flag that you should look at refactoring that class/method into something more manageable. Take a look at this: Refactoring: The key to unraveling spaghetti code
    class Sig{public static void main(String...args){\u0066or(int

IMN logo majestic logo threadwatch logo seochat tools logo