Results 1 to 8 of 8
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default catch the number format exception, where to place try

    I have a gui in the view package which has the functionality of a mini calculator. Beside of that in the model package i have a Calculator class

    It works but when i enter a string in number1Textfield i get a NumberFormatException. Thats correct because i want to insert a try/catch statement, but when i insert it, it doens't catch the fault

    I think i have to insert the try catch where i work with number1 and number2.
    but i have tried it now on three places, but i won't work, i still get the exception:

    Class:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package model;
    
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Andre
     */
    public class Calculator {
    
            private int number1;
            private int number2;
            private int sum;
            private int mod;
            private int multi;
    
        public Calculator(){
    
        }
    
        public int setNumber1(int _number1)
        {
              try {
                   this.number1 = _number1;
              }
              catch (NullPointerException e) {
                    JOptionPane.showMessageDialog(null, "test" + e.getMessage());
               }
    
               return number1;
    
        }
    
        public int setNumber2(int _number2)
        {
            try {
                   this.number2 = _number2;
              }
              catch (NullPointerException e) {
                    JOptionPane.showMessageDialog(null, "test" + e.getMessage());
               }
            return number2;
        }
    
         public int setSum()
        {
            try{
             this.sum = setNumber1(number1) + setNumber2(number2);
            }
              catch (NullPointerException e) {
                    JOptionPane.showMessageDialog(null, "test" + e.getMessage());
               }
            return sum;
        }
        public int setModulo()
        {
            this.mod = setNumber1(number1) % setNumber2(number2);
            return mod;
        }
        public int setMultiply()
        {
            this.multi = setNumber1(number1) * setNumber2(number2);
            return multi;
        }
        
        public String getSum()
        {
        
            setSum();
            String strSum = Integer.toString(mod);
            return strSum;
                   
        }
        public String getMod()
        {
            setModulo();
            String modulo = Integer.toString(mod);
            return modulo;
        }
        public String getMulti()
        {
            setMultiply();
            String multiply = Integer.toString(multi);
            return multiply;
        }
    
    
    }

    Form code

    Java Code:
     private void sumButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
            int number1 = Integer.parseInt(number1TextField.getText());
            rekenen.setNumber1(number1);
            int number2 = Integer.parseInt(number2TextField.getText());
            rekenen.setNumber2(number2);
    
            answerTextField.setText(rekenen.getSum());
    
        }                                         
    
        private void modulusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
            answerTextField.setText(rekenen.getMod());
        }                                             
    
        private void multiplyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
            answerTextField.setText(rekenen.getMulti());
        }
    I don't want the try and catch block in the view (gui) because i work via the model/view model.

  2. #2
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    I have changed this Nullpointer exception to NumberFormatException.
    And setters don't have a return value so that i changed to.

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Java Code:
    this.number1 = _number1;
    Will not throw any exception, you are just assigning a 'int' variable with a 'int' value.
    So
    Java Code:
    this.sum = setNumber1(number1) + setNumber2(number2);
    will neither throw any exception.

    But
    Java Code:
    int number2 = Integer.parseInt(number2TextField.getText());
    will throw NumberFormatException if the String does not contain a parsable int.



    By the way you can rename _number1 to number1:

    Java Code:
    public int setNumber1(int number1)
    {
        this.number1 = number1;
        //[---]
    }
    your _number1 will then be accessed by typing number1 and this.number1 till be access by this.number1.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi Hybernate,

    But this is code behind my gui (view controller).
    How do i catch this exception? Because i cannot use try/catch in my view controller (design code behind gui) if i stand by the MVC concept.

    Kind regards,

    André

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Well i see that in Java you can use the catch behind the gui frame file.
    I will try that, because i see in the error your abolutly right about where the exception starts.

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    If you do not what to catch exceptions in your listeners, you can
    catch them where you wrote the in your submitted code, pass the
    raw (String) input data to them and parse there instead of in the listeners.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    It works, thanks Hybernate

  8. #8
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

Similar Threads

  1. try catch exception
    By soxfan714 in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 11:11 AM
  2. Replies: 16
    Last Post: 09-04-2010, 04:03 AM
  3. Date format exception
    By chaudhas in forum New To Java
    Replies: 7
    Last Post: 06-25-2010, 09:31 AM
  4. Why can't I catch an exception?
    By glhansen in forum New To Java
    Replies: 5
    Last Post: 03-17-2009, 06:48 PM
  5. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 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
  •