Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: mortcal

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default mortcal

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.border.Border;
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
    import java.text.*;
     
    public class newMortgageCalculator1 extends JFrame
                                        implements PropertyChangeListener, ActionListener {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	public class String {
     
    	}
    	private static final Object[] String = null;
    	//*input for each field
        private double amount = 200000;
        private double rate = .0575;  //5.75%
        private int numPeriods = 30; 
     
        //*Lets you know what the fields are
        private JLabel amountLabel;
        private JLabel rateLabel;
        private JLabel numPeriodsLabel;
        private JLabel paymentLabel;
     
        //*Strings for each input
        private static java.lang.String amountString = "Loan Amount: ";
        private static java.lang.String rateString = "APR (%): ";
        private static java.lang.String numPeriodsString = "Years: ";
        private static java.lang.String paymentString = "Monthly Payment: ";
     
    	private static newMortgageCalculator1 newMortgageCalculator;
     
        //*input fields
        private JFormattedTextField amountField;
        private JFormattedTextField rateField;
        private JFormattedTextField numPeriodsField;
        private JFormattedTextField paymentField;
     
        //*Formats for the calculation
        private NumberFormat amountFormat;
        private NumberFormat percentFormat;
        private NumberFormat paymentFormat;
     
    	//*Adds buttons to the field
    	public JButton CalculateButton;
    	public JButton ClearButton;
    	public JButton ExitButton;
    	private Container content;
    	public JLabel temp;
     
     
     
    	public newMortgageCalculator1() {
            super();
            setSize(500, 400);
            setUpFormats();
     
            double payment = calculatePayment(amount, rate, numPeriods);
            add(CalculateButton);
            add(ClearButton);
            add(ExitButton);
            CalculateButton = new JButton("Calculate");
            ClearButton = new JButton("Clear");
            ExitButton = new JButton("Exit");
            CalculateButton.addActionListener(this);
            ClearButton.addActionListener(this);
            ExitButton.addActionListener(this);
     
     
            //*Makes the labels.
            amountLabel = new JLabel(amountString);
            rateLabel = new JLabel(rateString);
            numPeriodsLabel = new JLabel(numPeriodsString);
            paymentLabel = new JLabel(paymentString);
     
            //*Makes text fields.
            amountField = new JFormattedTextField(amountFormat);
            amountField.setValue(new Double(amount));
            amountField.setColumns(10);
            amountField.addPropertyChangeListener("value", (PropertyChangeListener) this);
     
            rateField = new JFormattedTextField(percentFormat);
            rateField.setValue(new Double(rate));
            rateField.setColumns(10);
            rateField.addPropertyChangeListener("value", this);
     
            numPeriodsField = new JFormattedTextField();
            numPeriodsField.setValue(new Integer(numPeriods));
            numPeriodsField.setColumns(10);
            numPeriodsField.addPropertyChangeListener("value", this);
     
            paymentField = new JFormattedTextField(paymentFormat);
            paymentField.setValue(new Double(payment));
            paymentField.setColumns(10);
            paymentField.setEditable(false);
            paymentField.setForeground(Color.red);
     
            //*Information on the labels
            amountLabel.setLabelFor(amountField);
            rateLabel.setLabelFor(rateField);
            numPeriodsLabel.setLabelFor(numPeriodsField);
            paymentLabel.setLabelFor(paymentField);
     
            //*arranges labels in the panel.
            JPanel labelPane = new JPanel(new GridLayout(0,1));
            labelPane.add(amountLabel);
            labelPane.add(rateLabel);
            labelPane.add(numPeriodsLabel);
            labelPane.add(paymentLabel);
     
            //*arranges the text labels in the panel .
            JPanel fieldPane = new JPanel(new GridLayout(0,1));
            fieldPane.add(amountField);
            fieldPane.add(rateField);
            fieldPane.add(numPeriodsField);
            fieldPane.add(paymentField);
     
            //*Put the labels on left.
            //*text fields on right.
            setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            add(labelPane, BorderLayout.CENTER);
            add(fieldPane, BorderLayout.LINE_END);
     
     
    		//* places content in frame.
            content.add(CalculateButton);
            content.add(ClearButton);
            content.add(ExitButton);
     
           //* controls the calculation button actions
            CalculateHandler handlerC = new CalculateHandler();
            CalculateButton.addActionListener((ActionListener) handlerC);
     
            //*controls the clear button actions
            ClearHandler  handlerD = new ClearHandler();
            ClearButton.addActionListener((ActionListener) handlerD);
     
     
        }
    		private double calculatePayment(double amount2, double rate2,
    			int numPeriods2) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    		private void add(JPanel labelPane, java.lang.String center) {
    		// TODO Auto-generated method stub
     
    	}
    		private void setUpFormats() {
    		// TODO Auto-generated method stub
     
    	}
    		private void setBorder(Border createEmptyBorder) {
    		// TODO Auto-generated method stub
     
    	}
    		private void add(JButton calculate2) {
    		// TODO Auto-generated method stub
     
    	}
     
    	//*Makes button perform
    	public void actionPerformed(ActionEvent e){
    		if (e.getSource() == CalculateButton){
    			}
    		else if (e.getSource() == ClearButton){
    			}
    		else if (e.getSource() == ExitButton){
    			System.exit(0);
    		}
    	}
        /** Used when a field's "value" property changes. */
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            if (source == amountField) {
                amount = ((Number)amountField.getValue()).doubleValue();
            } else if (source == rateField) {
                rate = ((Number)rateField.getValue()).doubleValue();
            } else if (source == numPeriodsField) {
                numPeriods = ((Number)numPeriodsField.getValue()).intValue();
            }
     
     
            intValue();} private void intValue() {
    		// TODO Auto-generated method stub
     
    	}
     
     
        int args() {
    		return 0;
    	} 
    	newMortgageCalculator1 (String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
     
                }});
            }
     
     
     
    	//*Calculates the monthly payment based on the loan amount,
        //*Interest rate, and length of loan.
        public static double calculatePayment1(double principle, double annRate, int years){
        	double monthlyInt = annRate / 12;
        	double monthlyPayment = (principle * monthlyInt)
        	          / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Calculates 1 monthly payment times by 12 for yearly cost. 
        	return format(monthlyPayment, 2);
        	}
     
     
     
     
        	public static double format(double amount, int mortgage) {
        	double temp = amount; 
        	temp = temp * Math.pow(10, mortgage);
        	temp = Math.round(temp);
        	temp = temp/Math.pow(10, mortgage);
        	return temp;
        	}
     
     
        	@SuppressWarnings("unused")
    		private void newMortgageCalculator11(){
            amountFormat = NumberFormat.getNumberInstance();
            percentFormat = NumberFormat.getNumberInstance();
            percentFormat.setMinimumFractionDigits(3);
            paymentFormat = NumberFormat.getCurrencyInstance();
     
        	}
     
            public void mortcal(String[] args){
    	 		newMortgageCalculator = new newMortgageCalculator1();
    	 		newMortgageCalculator.setVisible1(true);
        }
    		private void setVisible1(boolean b) {
    			// TODO Auto-generated method stub
     
    		}
     
     
    	public void setVisible(boolean b) {
    		// TODO Auto-generated method stub
     
    	}
    	public static Object[] getString() {
    		return String;
    	}
        }
    Last edited by JavaPF; September 29th, 2011 at 10:25 AM. Reason: Added highlight tags. Please see my signature

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    Please edit your post and wrap the code in code tags. Use the Go Advanced button, select your code and press the #icon.
    See: BB Code List - Java Programming Forums

  3. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: mortcal

    And if you have a question, please ask it, and be detailed.

  5. The Following User Says Thank You to copeg For This Useful Post:

    riclin (September 30th, 2011)

  6. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    I am sorry I thought I asked a Question I want to know what is wrong with this coding it was working before I added the buttons and action listener and now all I get is an error message that says that a warning of Fatal Exception occurred program will exit. No frame works now and I cannot figure out what is causing it. I have examined the coding over and over and still I cannot find the problem is there any way some one can help me figure this out. I am new to Java and I think that one problem that I am having is placing the buttons and action listener in the right place .
    What I was suppose to do was add three button that would allow a user to use a clear button that would clear the calculation and start over, add a calculate button that would calculate the payments and then add a exit button that would stop the program and exit it without allowing it to contiunue.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    I get is an error message
    Please copy and paste the full text of the error message here.

    The code does not compile because of missing class definitions.
    Last edited by Norm; September 29th, 2011 at 09:13 PM. Reason: added: Missing classes

  8. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  9. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    So how do I fix the missing class definition?

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    You need to find the definitions for the missing classes and add them to your code.

    Or you need to create classes for those that are not defined.
    Last edited by Norm; September 30th, 2011 at 07:33 AM.

  11. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  12. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    Thanks I Find that missing class and added that class to it now I am having different errors here is the new coding:
    import java.awt.*;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
     
    import java.text.*;
    import java.util.logging.ConsoleHandler;
     
    public class MortgageCalculator{
     
     
     
    public static Container container;
     
    public static class week2MortCal extends JPanel
                                        implements PropertyChangeListener {
     
    	private static final long serialVersionUID = 1L;
    	//*input for each field
        private double amount = 200000;
        private double rate = .0575;  //5.75%
        private int numPeriods = 30; 
     
        //*Lets you know what the fields are
        private JLabel amountLabel;
        private JLabel rateLabel;
        private JLabel numPeriodsLabel;
        private JLabel paymentLabel;
     
        //*Strings for each input
        private static String amountString = "Loan Amount: ";
        private static String rateString = "APR (%): ";
        private static String numPeriodsString = "Years: ";
        private static String paymentString = "Monthly Payment: ";
     
        //*input fields
        private JFormattedTextField amountField;
        private JFormattedTextField rateField;
        private JFormattedTextField numPeriodsField;
        private JFormattedTextField paymentField;
     
        //*Formats for the calculation
        private NumberFormat amountFormat;
        private NumberFormat percentFormat;
        private NumberFormat paymentFormat;
    	private double temp1;
     
    	//*Adds buttons to the field
    		public static JButton CalculateButton;
    		public static JButton ClearButton;
    		public static JButton ExitButton;
    		private Container content;
    		public JLabel temp;
    		public week2MortCal() {
            super(new BorderLayout());
            setUpFormats();
            double payment = calculatePayment(amount, rate, numPeriods);
     
     
            //*Makes the labels.
            amountLabel = new JLabel(amountString);
            rateLabel = new JLabel(rateString);
            numPeriodsLabel = new JLabel(numPeriodsString);
            paymentLabel = new JLabel(paymentString);
     
            //*Makes text fields.
            amountField = new JFormattedTextField(amountFormat);
            amountField.setValue(new Double(amount));
            amountField.setColumns(10);
            amountField.addPropertyChangeListener("value", this);
     
            rateField = new JFormattedTextField(percentFormat);
            rateField.setValue(new Double(rate));
            rateField.setColumns(10);
            rateField.addPropertyChangeListener("value", this);
     
            numPeriodsField = new JFormattedTextField();
            numPeriodsField.setValue(new Integer(numPeriods));
            numPeriodsField.setColumns(10);
            numPeriodsField.addPropertyChangeListener("value", this);
     
            paymentField = new JFormattedTextField(paymentFormat);
            paymentField.setValue(new Double(payment));
            paymentField.setColumns(10);
            paymentField.setEditable(false);
            paymentField.setForeground(Color.blue);
     
            //*Information on the labels
            amountLabel.setLabelFor(amountField);
            rateLabel.setLabelFor(rateField);
            numPeriodsLabel.setLabelFor(numPeriodsField);
            paymentLabel.setLabelFor(paymentField);
     
            //*arranges labels in the panel.
            JPanel labelPane = new JPanel(new GridLayout(0,1));
            labelPane.add(amountLabel);
            labelPane.add(rateLabel);
            labelPane.add(numPeriodsLabel);
            labelPane.add(paymentLabel);
     
            //*arranges the text labels in the panel .
            JPanel fieldPane = new JPanel(new GridLayout(0,1));
            fieldPane.add(amountField);
            fieldPane.add(rateField);
            fieldPane.add(numPeriodsField);
            fieldPane.add(paymentField);
     
            //*Put the labels on left,
            //*text fields on right.
            setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
            add(labelPane, BorderLayout.CENTER);
            add(fieldPane, BorderLayout.LINE_END); 
     
          //* places content in frame.
            content.add(CalculateButton);
            content.add(ClearButton);
            content.add(ExitButton);
     
            //* controls the calculation button actions
            ConsoleHandler handlerC = new ConsoleHandler();
            CalculateButton.addActionListener((ActionListener) handlerC);
     
            //*controls the clear button actions
            ClearHandler  handlerD = new ClearHandler();
            ClearButton.addActionListener((ActionListener) handlerD);
        }
     
     
        /** Used when a field's "value" property changes. */
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            if (source == amountField) {
                amount = ((Number)amountField.getValue()).doubleValue();
            } else if (source == rateField) {
                rate = ((Number)rateField.getValue()).doubleValue();
            } else if (source == numPeriodsField) {
                numPeriods = ((Number)numPeriodsField.getValue()).intValue();
            }
     
            double payment = calculatePayment(amount, rate, numPeriods);
            paymentField.setValue(new Double(payment));
        }
     
     
        private static void createAndShowGUI() {
            //*Makes the window.
            JFrame frame = new JFrame("Mortgage Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //*Allows you to add input in window
            frame.add(new week2MortCal());
            CalculateButton = new JButton("Calculate");
            ClearButton = new JButton("Clear");
            ExitButton = new JButton("Exit");
            CalculateButton.addActionListener(null);
            ClearButton.addActionListener(null);
            ExitButton.addActionListener(null);
            //*Shows the window
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
     
    	        UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                }
            });
        }
     
        //*Calculates the monthly payment based on the loan amount,
        //*Interest rate, and length of loan.
        public static double calculatePayment(double principle, double annRate, int years){
        	double monthlyInt = annRate / 12;
        	double monthlyPayment = (principle * monthlyInt)
        	          / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Calculates 1 monthly payment times by 12 for yearly cost. 
        	return format(monthlyPayment, 2);
        	}
        	public static double format(double amount, int mortgage) {
        	double temp = amount; 
        	temp = temp * Math.pow(10, mortgage);
        	temp = Math.round(temp);
        	temp = temp/Math.pow(10, mortgage);
        	return temp;
        	}
     
     
        private void setUpFormats() {
            amountFormat = NumberFormat.getNumberInstance();
     
            percentFormat = NumberFormat.getNumberInstance();
            percentFormat.setMinimumFractionDigits(3);
     
            paymentFormat = NumberFormat.getCurrencyInstance();
        }
     
    	public double getTemp() {
    		return temp1;
    	}
     
    	public void setTemp(double temp) {
    		this.temp1 = temp;
    		}
    	}
    };

    These are the error messages that I am getting:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MortgageCalculator$week2MortCal.<init>(MortgageCal culator.java:118)
    at MortgageCalculator$week2MortCal.createAndShowGUI(M ortgageCalculator.java:154)
    at MortgageCalculator$week2MortCal.access$0(MortgageC alculator.java:148)
    at MortgageCalculator$week2MortCal$1.run(MortgageCalc ulator.java:171)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Last edited by riclin; September 30th, 2011 at 10:01 AM.

  13. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    You left off the code tags.
    see: http://www.javaprogrammingforums.com...do=bbcode#code

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MortgageCalculator$week2MortCal.<init>(MortgageCal culator.java:118)
    There is a variable on line 118 that has a null value. Look at line 118 and find what variable has a null value and backtrack in your code to see why that variable does not have a valid value.

  14. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  15. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    Okay now I backtrack it and fixed that problem and found another piece of code that I forgot to add and add that but now this is the error I am getting with it:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
     
    import java.text.*;
    import java.util.logging.ConsoleHandler;
     
    public class MortgageCalculator{
     
     
     
    public Container container;
     
    public  class week2MortCal extends JPanel
                                        implements PropertyChangeListener {
     
    	private  final long serialVersionUID = 1L;
    	//*input for each field
        private double amount = 200000;
        private double rate = .0575;  //5.75%
        private int numPeriods = 30; 
     
        //*Lets you know what the fields are
        private JLabel amountLabel;
        private JLabel rateLabel;
        private JLabel numPeriodsLabel;
        private JLabel paymentLabel;
     
        //*Strings for each input
        private String amountString = "Loan Amount: ";
        private  String rateString = "APR (%): ";
        private  String numPeriodsString = "Years: ";
        private  String paymentString = "Monthly Payment: ";
     
        //*input fields
        private JFormattedTextField amountField;
        private JFormattedTextField rateField;
        private JFormattedTextField numPeriodsField;
        private JFormattedTextField paymentField;
     
        //*Formats for the calculation
        private NumberFormat amountFormat;
        private NumberFormat percentFormat;
        private NumberFormat paymentFormat;
    	private double temp1;
     
    	//*Adds buttons to the field
    		public  JButton CalculateButton;
    		public  JButton ClearButton;
    		public  JButton ExitButton;
    		private Container content;
    		public JLabel temp;
    		public week2MortCal() {
            super(new BorderLayout());
            setUpFormats();
            double payment = calculatePayment(amount, rate, numPeriods);
     
     
            //*Makes the labels.
            amountLabel = new JLabel(amountString);
            rateLabel = new JLabel(rateString);
            numPeriodsLabel = new JLabel(numPeriodsString);
            paymentLabel = new JLabel(paymentString);
     
            //*Makes text fields.
            amountField = new JFormattedTextField(amountFormat);
            amountField.setValue(new Double(amount));
            amountField.setColumns(10);
            amountField.addPropertyChangeListener("value", this);
     
            rateField = new JFormattedTextField(percentFormat);
            rateField.setValue(new Double(rate));
            rateField.setColumns(10);
            rateField.addPropertyChangeListener("value", this);
     
            numPeriodsField = new JFormattedTextField();
            numPeriodsField.setValue(new Integer(numPeriods));
            numPeriodsField.setColumns(10);
            numPeriodsField.addPropertyChangeListener("value", this);
     
            paymentField = new JFormattedTextField(paymentFormat);
            paymentField.setValue(new Double(payment));
            paymentField.setColumns(10);
            paymentField.setEditable(false);
            paymentField.setForeground(Color.blue);
     
            //*Information on the labels
            amountLabel.setLabelFor(amountField);
            rateLabel.setLabelFor(rateField);
            numPeriodsLabel.setLabelFor(numPeriodsField);
            paymentLabel.setLabelFor(paymentField);
     
            //*arranges labels in the panel.
            JPanel labelPane = new JPanel(new GridLayout(0,1));
            labelPane.add(amountLabel);
            labelPane.add(rateLabel);
            labelPane.add(numPeriodsLabel);
            labelPane.add(paymentLabel);
     
            //*arranges the text labels in the panel .
            JPanel fieldPane = new JPanel(new GridLayout(0,1));
            fieldPane.add(amountField);
            fieldPane.add(rateField);
            fieldPane.add(numPeriodsField);
            fieldPane.add(paymentField);
     
            //*Put the labels on left,
            //*text fields on right.
            setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
            add(labelPane, BorderLayout.CENTER);
            add(fieldPane, BorderLayout.LINE_END); 
     
          //* places content in frame.
            content.add(CalculateButton);
            content.add(ClearButton);
            content.add(ExitButton);
     
            //* controls the calculation button actions
            ConsoleHandler handlerC = new ConsoleHandler();
            CalculateButton.addActionListener((ActionListener) handlerC);
     
            //*controls the clear button actions
            ClearHandler  handlerD = new ClearHandler();
            ClearButton.addActionListener((ActionListener) handlerD);
        }
     
    		//*Makes button perform
    		public void actionPerformed(ActionEvent e){
    			if (e.getSource() == CalculateButton){
    				}
    			else if (e.getSource() == ClearButton){
    				}
    			else if (e.getSource() == ExitButton){
    				System.exit(0);
    				}
     
        /** Used when a field's "value" property changes. */
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            if (source == amountField) {
                amount = ((Number)amountField.getValue()).doubleValue();
            } else if (source == rateField) {
                rate = ((Number)rateField.getValue()).doubleValue();
            } else if (source == numPeriodsField) {
                numPeriods = ((Number)numPeriodsField.getValue()).intValue();
            }
     
            double payment = calculatePayment(amount, rate, numPeriods);
            paymentField.setValue(new Double(payment));
        }
     
     
        private void createAndShowGUI() {
            //*Makes the window.
            JFrame frame = new JFrame("Mortgage Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //*Allows you to add input in window
            frame.add(new week2MortCal());
            CalculateButton = new JButton("Calculate");
            ClearButton = new JButton("Clear");
            ExitButton = new JButton("Exit");
            CalculateButton.addActionListener((ActionListener) this);
            ClearButton.addActionListener((ActionListener) this);
            ExitButton.addActionListener((ActionListener) this);
            //*Shows the window
            frame.pack();
            frame.setVisible(true);
        }
     
        public  void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
     
    	        UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                }
            });
        }
     
        //*Calculates the monthly payment based on the loan amount,
        //*Interest rate, and length of loan.
        public  double calculatePayment(double principle, double annRate, int years){
        	double monthlyInt = annRate / 12;
        	double monthlyPayment = (principle * monthlyInt)
        	          / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Calculates 1 monthly payment times by 12 for yearly cost. 
        	return format(monthlyPayment, 2);
        	}
        	public  double format(double amount, int mortgage) {
        	double temp = amount; 
        	temp = temp * Math.pow(10, mortgage);
        	temp = Math.round(temp);
        	temp = temp/Math.pow(10, mortgage);
        	return temp;
        	}
     
     
        private void setUpFormats() {
            amountFormat = NumberFormat.getNumberInstance();
     
            percentFormat = NumberFormat.getNumberInstance();
            percentFormat.setMinimumFractionDigits(3);
     
            paymentFormat = NumberFormat.getCurrencyInstance();
        }
     
    	public double getTemp() {
    		return temp1;
    	}
     
    	public void setTemp(double temp) {
    		this.temp1 = temp;
    		}
    	}
    };
    java.lang.NoSuchMethodError: main
    Exception in thread "main"

  16. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    That's strange. How did you execute the code before when you got the NullPointerException?
    What did you change in the previous code? That code did start executing because you would not get the execution error without it.
    Look at anything you did to the main() method.

  17. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    I dont have a main method unless your talking about from the orginal program that it the only place the main method is but how do I get that to run in with this code?

  18. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    I dont have a main method
    That is what this error message is complaining about:
    java.lang.NoSuchMethodError: main

    The class that you pass to the java command must have a main method:
    java.exe ThisClassMustHaveMainMethod

    The class that you pass to the java command MUST have a main method.

  19. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  20. #14
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    so this here public class week2MortCal extends JPanel
    implements PropertyChangeListener {
    should be changed to public static void main(String[] args)
    public class week2Mortcal extends Jpanel
    implements PropertyChangeListener{ Right?

  21. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    No, do not change the class definition line.
    Add a main method to the class.

    Look at post #8 That code executed. It has a valid main() method.

  22. #16
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    I do not see the main method in post #8, are yout talking about the public class MortgageCalculator? Is that considered the main?

  23. #17
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    You said that post #8 executed, you got it to work? because on my end it still shows me the main method error everytime I try to run it.

  24. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    The main() method is spelled this way: main
    It is a method.
    You need to consider the difference between method and classes. The following line defines a class:
    public class MortgageCalculator ....

    I do not see the main method in post #8,
    It's there, look again. Its about a third of the way up from the bottom.

  25. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  26. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mortcal

    In post #8 you show a stack trace that comes if you have a problem executing the code.

    Here is what you posted. To get this, the code had to execute.
    These are the error messages that I am getting:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MortgageCalculator$week2MortCal.<init>(MortgageCal culator.java:118)
    at MortgageCalculator$week2MortCal.createAndShowGUI(M ortgageCalculator.java:154)
    at MortgageCalculator$week2MortCal.access$0(MortgageC alculator.java:148)
    at MortgageCalculator$week2MortCal$1.run(MortgageCalc ulator.java:171)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  27. The Following User Says Thank You to Norm For This Useful Post:

    riclin (September 30th, 2011)

  28. #20
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    The main() method is spelled this way: main
    It is a method.
    You need to consider the difference between method and classes. The following line defines a class:
    public class MortgageCalculator ....
    How do I consider the difference between method and class? Sorry I am so confused and you have been so kind to help me thank you so much for all this help.
    Last edited by riclin; September 30th, 2011 at 03:10 PM.

  29. #21
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: mortcal

    Quote Originally Posted by Norm View Post
    In post #8 you show a stack trace that comes if you have a problem executing the code.

    Here is what you posted. To get this, the code had to execute.
    I now understand what you are saying here