Results 1 to 7 of 7
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] Simple Conversion Calculator

    I have put together a small application converting cm to inches, however I need help resolving 1 error.

    I cannot seem to work out what I have done wrong and would appreciate it if you could take a look, I know it is a simple error but I cannot work it out.

    Thanks in advance for your help

    File attached
    Attached Files Attached Files

  2. #2
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default Simple Conversion Calculator

    Just incase you dont want to download the file, the formula is in a class

    Class - Units
    GUI - Application

    Netbeans 6.5 was used

    code that is causing me the problem is

    Java Code:
                              package conversion;
                                  import java.text.*;
    /**
     *
     * @author Admin
     */
                              public class Application extends javax.swing.JFrame {
    
        /** Creates new form Application */
                             public Application() {
                             initComponents();
                             }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
    
                               private void cmdConvertActionPerformed           (java.awt.event.ActionEvent evt) {                                           
            // Code for converting.   CM TO INCHES -  txtInch    txtCm
                               double inch;
                               double cm;   
                               double outputInches;
                               boolean ok;
                               ok = true;
                               String outinch;
    
            // try catch for numeric data only
                    try {
                    cm = Double.parseDouble(txtCm.getText());//expect any numerical input
                   }
                   catch (NumberFormatException n)  {//to catch none numeric input
                    Optpane.showMessageDialog(this,"Enter numbers only please",
                    "please re-enter",Optpane.ERROR_MESSAGE); // Optpane kicks in on error
                    ok = false; //boolean to trigger Optpane on alpha entry
                   }
    
    
    
    // start of formula to convert cm to inches and inches to cm - uses class file Units.java
                    
                    cm=Double.parseDouble(txtCm.getText());
    
    
                    [COLOR="Green"]outputInches = Units.conversion(cm,inch);[/COLOR]
                  
    
                    txtInch.setText(outinch=conversions.format(outputInches));
    // End of formula
    The line causng the problem is highlighted in green.

  3. #3
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    What error do you get?
    I would guess a NullPointerExption because Units is not created.

  4. #4
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    I think the problem is your last statement, a part of which is 'conversions.format(...'

    What is 'conversions?'

    @bubbless
    I don't think so, because Units is a reference to that class, not an object. He just has to ensure that the conversion method in Units is static.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is that Java is case sensitive. Conversion is not the same as conversion. I'd change the case of the method in the Units class to be all lower-case.

    In other words, change this:
    Java Code:
    public static double Conversion(double cm, double inch)
    {
      //.... some code
    }
    to this:
    Java Code:
    public static double conversion(double cm, double inch)
    {
      //.... some code
    }
    Edit: also, what are you doing with the inches parameter in your conversion method, and the inches variable in your Application class. They both seem superfluous.
    Last edited by Fubarable; 03-19-2009 at 11:24 PM.

  6. #6
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Thumbs up Calculator

    I managed to solve the issue then I also put a calculator as a seperate package together converting the other way round inches to cm

    Thanks for your replies.

    See attached
    Attached Files Attached Files

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please do not cross-post questions in multiple forums. This will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders.

Similar Threads

  1. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 02:43 PM
  2. [SOLVED] Simple Calculator Applet Weird Error
    By sari in forum New To Java
    Replies: 5
    Last Post: 01-28-2009, 05:57 AM
  3. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM
  4. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •