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

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Is the AutoButtonListener class public and external? If so the new statement should be create an instance of it.

    The posted code shows it private and internal.
    You could write a method that creates an instance of the class and returns it.


    Please post the full text of the error messages.
    Ok I made the AutoButtonListener public and that eliminated my error in the AutoCalc class file, but the autoPmt Button still is giving me an error like it can't find the AutoButtonListener. Here is my error:

    Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	AutoButtonListener cannot be resolved to a type
    
    	at financialCalc.FinancialCalc.buildWelcomeMenu(FinancialCalc.java:57)
    	at financialCalc.FinancialCalc.<init>(FinancialCalc.java:38)
    	at financialCalc.FinancialCalc.main(FinancialCalc.java:466)
  2. #17
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you post the compiler's error message. What you posted looks like a message from the IDE.

    AutoButtonListener cannot be resolved to a type
    That sounds like the definition for the class can't be found.
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Can you post the compiler's error message. What you posted looks like a message from the IDE.


    That sounds like the definition for the class can't be found.
    I can't seem to find where to get the compiler code?? But yes you are right, I don't think it's finding the class? What do I need to do to link them together for the buttonListener??
  6. #19
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Try the other idea. Leave the class as private and inner
    and add a method to the AutoCalc class that creates an instance of the listener and returns it.
    you'll need to use the instance in autoPanel to call it.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Try the other idea. Leave the class as private and inner
    and add a method to the AutoCalc class that creates an instance of the listener and returns it.
    you'll need to use the instance in autoPanel to call it.
    I can't really figure out how to do what you're saying. I don't think I'm advanced enough. I'm trying to learn to do it. I've started trying something else now instead. See what you think, but I'm still getting a different problem now:

    Code:
    package financialCalc;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    //import java.text.DecimalFormat;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FinancialCalc extends JFrame
    {
    	double balance, minimumPayment;
    	String payment, fixedPayment;
    	private JPanel welcomePanel, greetingPanel, buttonPanel;//, minPmtButtonPanel, minPmtPanel;
    	private JLabel message;//, Amt, Term, Rate, minPmt, fixedPmt;
    	//private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
    	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit;//, minPmtButton, calcPmts;
    	private JFrame mainFrame;//, mortPmtFrame, minPmtFrame;
    	private Dimension d = new Dimension(400,400);
    	//private DecimalFormat dlr = new DecimalFormat("#0.00");
    	//private DecimalFormat pct = new DecimalFormat("#0");
    	private AutoCalc autoPanel;	
    	
    	
    	public FinancialCalc()
    	{
    		
    		mainFrame = new JFrame("Financial Calculator Application");		
    		
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    		
    		mainFrame.setPreferredSize(d);
    		mainFrame.setLayout(new BorderLayout());
    		mainFrame.setLocationRelativeTo(null);
    		
    		autoPanel = new AutoCalc();
    		buildWelcomeMenu();
    		
    		mainFrame.add(greetingPanel, BorderLayout.NORTH);
    		mainFrame.add(welcomePanel, BorderLayout.CENTER);
    		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
    		
    		mainFrame.pack();
    		mainFrame.setVisible(true);
    	}
    		
    	
    	public void buildWelcomeMenu()
    	{
    		welcomePanel = new JPanel();
    		welcomePanel.setLayout(new GridLayout(4,1));
    		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
    		
    		autoPmt = new JButton("Auto loan payment calculator");
    		autoPmt.setMnemonic(KeyEvent.VK_A);
    		autoPmt.addActionListener(new AutoButtonListener());
    		
    		
    		mortPmt = new JButton("Mortgage payment calculator");
    		mortPmt.setMnemonic(KeyEvent.VK_M);
    		//mortPmt.addActionListener(new MortButtonListener());
    		
    		homeAfford = new JButton("How much home can you afford?");
    		homeAfford.setMnemonic(KeyEvent.VK_H);
    		//homeAfford.addActionListener(new HomeButtonListener());
    		
    		costOfMin = new JButton("True cost of paying the minimum payment");
    		costOfMin.setMnemonic(KeyEvent.VK_C);
    		//costOfMin.addActionListener(new CostMinButtonListener());
    		
    		buttonPanel = new JPanel();
    		exit = new JButton("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    		buttonPanel.add(exit);
    		
    		welcomePanel.add(autoPmt);
    		welcomePanel.add(mortPmt);
    		welcomePanel.add(homeAfford);
    		welcomePanel.add(costOfMin);
    	
    		
    		greetingPanel = new JPanel();
    		message = new JLabel("Welcome to the Financial Calculations Application");
    		greetingPanel.add(message);
    		
    		
    	}
    	
    	private class AutoButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			autoPanel.buildAutoCalc();		
    		}
    	
    	}
    
    //THIS IS WHERE THE FinancialCalc class file ends and the AutoCalc class file begins:
    
    
    package financialCalc;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    
    public class AutoCalc extends JPanel
    {
    	private JLabel Amt, Term, Rate;
    	private JTextField AmtF, TermF, RateF;//, minPmtF, fixedPmtF;
    	private JFrame autoPmtFrame;
    	private DecimalFormat dlr = new DecimalFormat("#0.00");
    	private DecimalFormat pct = new DecimalFormat("#0");
    	private Dimension d = new Dimension(400,400);
    	JPanel autoPmtPanel, autoButtonPanel;
    
    	
    	public AutoCalc()
    	{
    			
    			autoPmtFrame = new JFrame();
    			autoPmtFrame = new JFrame("Auto Payment Calculator");			
    			autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			autoPmtFrame.setLayout(new GridLayout(2,1));
    			autoPmtFrame.setPreferredSize(d);
    			autoPmtFrame.setLocationRelativeTo(null);
    
    			autoPmtFrame.add(autoPmtPanel);
    			autoPmtFrame.add(autoButtonPanel);
    			
    			autoPmtFrame.pack();
    			autoPmtFrame.setVisible(true);
    	}
    	
    	
    	public void buildAutoCalc()
    	{
    		
    		autoPmtPanel = new JPanel();
    		autoPmtPanel.setLayout(new GridLayout(4,2));
    		autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    		
    		
    		autoButtonPanel = new JPanel();
    		//add = new JCheckBox("Compare another loan");
    		//add.addActionListener(new AddButtonListener());
    		
    					
    		JButton calculate = new JButton("Calculate");
    		JButton exit = new JButton("Exit");
    		calculate.addActionListener(new AutoCalcButtonListener());
    		exit.addActionListener(new ExitButtonListener());
    		
    		Amt = new JLabel("Loan Amount");
    		Term = new JLabel("Term in months");
    		Rate = new JLabel("Interest Rate");
    		
    		
    		AmtF = new JTextField(5);
    		TermF = new JTextField(5);
    		RateF = new JTextField(5);
    		
    		autoPmtPanel.add(Amt);
    		autoPmtPanel.add(AmtF);
    		autoPmtPanel.add(Term);
    		autoPmtPanel.add(TermF);
    		autoPmtPanel.add(Rate);
    		autoPmtPanel.add(RateF);
    		
    		autoButtonPanel.add(calculate);
    		autoButtonPanel.add(exit);
    		
    		
    	}
    	
    	
    	private class AutoCalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String str;
    			double amt, n, rate, denominator;
    			double monthlyPmt, totalInt, totalAmtPaid;
    			
    			str = AmtF.getText();
    			amt = Double.parseDouble(str);
    			
    			str = TermF.getText();
    			n = Double.parseDouble(str);
    			
    			str = RateF.getText();
    			rate = Double.parseDouble(str);
    			
    			rate = rate / 100 / 12;
    			denominator = 1- Math.pow(1 + rate, -n);
    			
    			monthlyPmt = amt * (rate / denominator);
    			
    			totalAmtPaid = monthlyPmt * n;
    			
    			totalInt = totalAmtPaid - amt;
    			
    			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
    					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
    					"Total Interest Paid: $" + dlr.format(totalInt), 
    					"Total", JOptionPane.PLAIN_MESSAGE);
    			
    			
    		}
    	}
    	
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);			
    		}
    	
    	}
    	
    	
    	
    	
    }
    But I get this error in doing it:
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.awt.Container.addImpl(Container.java:1086)
    	at java.awt.Container.add(Container.java:998)
    	at javax.swing.JFrame.addImpl(JFrame.java:562)
    	at java.awt.Container.add(Container.java:410)
    	at financialCalc.AutoCalc.<init>(AutoCalc.java:32)
    	at financialCalc.FinancialCalc.<init>(FinancialCalc.java:36)
    	at financialCalc.FinancialCalc.main(FinancialCalc.java:474)
  10. #21
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Where is the method in the AutoCalc class that creates and returns the listener?

    at financialCalc.AutoCalc.<init>(AutoCalc.java:32)
    What variable on line 32 has a null value? Check why it does not have a valid value.
    Last edited by NormR; December 9th, 2012 at 04:41 PM.
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Where is the method in the AutoCalc class that creates and returns the listener?



    What variable on line 32 has a null value? Check why it does not have a valid value.
    Ok how do you return a listener? I hasn't done that. Can u show me an example?
  14. #23
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Define the return type of the method to be the type of the Listener.
    Create the listener and return it:
    Code:
    public SomeListener getListener() {
      return new SomeListener();
    }
  16. #24
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Define the return type of the method to be the type of the Listener.
    Create the listener and return it:
    Code:
    public SomeListener getListener() {
      return new SomeListener();
    }
    Oh ok. So does that kind of defeat my purpose of adding that buildAutoCalc method an calling it from the financialCalc class. I was just using that as a way to get over to the AutoCalc class file. Back to the earlier example then. Why won't my autoPmt button call my autoButtonListener from the AutoCalc class file?! Any example you can show me of how to call a buttonListener from another class file?
  18. #25
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You've changed the code since post#14. Go back to that code and add the method I mentioned and use it in the call to the addListener() method.

    how to call a buttonListener from another class file?
    If the buttonListener is a method, you would call it the same way you call any method in a class: get a reference to the class and use it to call the method
    aRefToClass.buttonlistener();

    I don't know why you would want to call a butonlistener method. Normally they are called when there is an event at the button.
    Last edited by NormR; December 9th, 2012 at 05:01 PM.
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    You've changed the code since post#14. Go back to that code and add the method I mentioned and use it in the call to the addListener() method.



    If the buttonListener is a method, you would call it the same way you call any method in a class: get a reference to the class and use it to call the method
    aRefToClass.buttonlistener();

    I don't know why you would want to call a butonlistener method. Normally they are called when there is an event at the button.
    That's what I'm trying to do is to get it to call the autoButtonListener() when the autoPmt button is clicked. It's just that I was the autoButtonListener in a different class file
  22. #27
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The new code has messed it up. Go back and Start with the code in post#14.

    I'm not sure I understand what you are trying to do.
    When the autoPmt button is clicked you want to add another listener to another button?

    Why not add the correct listener to the autoPmt button in the beginning?
    Last edited by NormR; December 9th, 2012 at 05:16 PM.
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    The new code has messed it up. Go back and Start with the code in post#14.

    I'm not sure I understand what you are trying to do.
    When the autoPmt button is clicked you want to add another listener to another button?

    Why not add the correct listener to the autoPmt button in the beginning?
    Ok this is what I currently have:

    Code:
    package financialCalc;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    //import java.text.DecimalFormat;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FinancialCalc extends JFrame
    {
    	double balance, minimumPayment;
    	String payment, fixedPayment;
    	private JPanel welcomePanel, greetingPanel, buttonPanel;//, minPmtButtonPanel, minPmtPanel;
    	private JLabel message;//, Amt, Term, Rate, minPmt, fixedPmt;
    	//private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
    	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit;//, minPmtButton, calcPmts;
    	private JFrame mainFrame;//, mortPmtFrame, minPmtFrame;
    	private Dimension d = new Dimension(400,400);
    	//private DecimalFormat dlr = new DecimalFormat("#0.00");
    	//private DecimalFormat pct = new DecimalFormat("#0");
    	//private AutoCalc autoPanel;	
    	
    	
    	public FinancialCalc()
    	{
    		
    		mainFrame = new JFrame("Financial Calculator Application");		
    		
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    		
    		mainFrame.setPreferredSize(d);
    		mainFrame.setLayout(new BorderLayout());
    		mainFrame.setLocationRelativeTo(null);
    		
    		//autoPanel = new AutoCalc();
    		buildWelcomeMenu();
    		
    		mainFrame.add(greetingPanel, BorderLayout.NORTH);
    		mainFrame.add(welcomePanel, BorderLayout.CENTER);
    		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
    		
    		mainFrame.pack();
    		mainFrame.setVisible(true);
    	}
    		
    	
    	public void buildWelcomeMenu()
    	{
    		welcomePanel = new JPanel();
    		welcomePanel.setLayout(new GridLayout(4,1));
    		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
    		
    		autoPmt = new JButton("Auto loan payment calculator");
    		autoPmt.setMnemonic(KeyEvent.VK_A);
    		autoPmt.addActionListener(new AutoButtonListener());
    		
    		
    		mortPmt = new JButton("Mortgage payment calculator");
    		mortPmt.setMnemonic(KeyEvent.VK_M);
    		//mortPmt.addActionListener(new MortButtonListener());
    		
    		homeAfford = new JButton("How much home can you afford?");
    		homeAfford.setMnemonic(KeyEvent.VK_H);
    		//homeAfford.addActionListener(new HomeButtonListener());
    		
    		costOfMin = new JButton("True cost of paying the minimum payment");
    		costOfMin.setMnemonic(KeyEvent.VK_C);
    		//costOfMin.addActionListener(new CostMinButtonListener());
    		
    		buttonPanel = new JPanel();
    		exit = new JButton("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    		buttonPanel.add(exit);
    		
    		welcomePanel.add(autoPmt);
    		welcomePanel.add(mortPmt);
    		welcomePanel.add(homeAfford);
    		welcomePanel.add(costOfMin);
    	
    		
    		greetingPanel = new JPanel();
    		message = new JLabel("Welcome to the Financial Calculations Application");
    		greetingPanel.add(message);
    		
    		
    	}
    
    
    //HERE IS WHERE I END THE FINANCIALCALC CLASS FILE, //AND BEGIN THE AUTOCALC CLASS FILE. 
    
    package financialCalc;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    
    public class AutoCalc extends JPanel
    {
    	private JLabel Amt, Term, Rate;
    	private JTextField AmtF, TermF, RateF;//, minPmtF, fixedPmtF;
    	private JFrame autoPmtFrame;
    	private DecimalFormat dlr = new DecimalFormat("#0.00");
    	private DecimalFormat pct = new DecimalFormat("#0");
    	private Dimension d = new Dimension(400,400);
    	JPanel autoPmtPanel, autoButtonPanel;
    
    
    	
    		public class AutoButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				autoPmtFrame = new JFrame();
    				autoPmtFrame = new JFrame("Auto Payment Calculator");			
    				autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				autoPmtFrame.setLayout(new GridLayout(2,1));
    				autoPmtFrame.setPreferredSize(d);
    				autoPmtFrame.setLocationRelativeTo(null);
    
    				autoPmtFrame.add(autoPmtPanel);
    				autoPmtFrame.add(autoButtonPanel);
    			
    				autoPmtFrame.pack();
    				autoPmtFrame.setVisible(true);
    			
    				buildAutoCalc();			
    			
    			}
    	
    		}
    	
    	
    	
    	
    	public void buildAutoCalc()
    	{
    		
    		autoPmtPanel = new JPanel();
    		autoPmtPanel.setLayout(new GridLayout(4,2));
    		autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    		
    		
    		autoButtonPanel = new JPanel();		
    					
    		JButton calculate = new JButton("Calculate");
    		JButton exit = new JButton("Exit");
    		calculate.addActionListener(new AutoCalcButtonListener());
    		exit.addActionListener(new ExitButtonListener());
    		
    		Amt = new JLabel("Loan Amount");
    		Term = new JLabel("Term in months");
    		Rate = new JLabel("Interest Rate");
    		
    		
    		AmtF = new JTextField(5);
    		TermF = new JTextField(5);
    		RateF = new JTextField(5);
    		
    		autoPmtPanel.add(Amt);
    		autoPmtPanel.add(AmtF);
    		autoPmtPanel.add(Term);
    		autoPmtPanel.add(TermF);
    		autoPmtPanel.add(Rate);
    		autoPmtPanel.add(RateF);
    		
    		autoButtonPanel.add(calculate);
    		autoButtonPanel.add(exit);
    		
    		
    	}
    	
    	
    	private class AutoCalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String str;
    			double amt, n, rate, denominator;
    			double monthlyPmt, totalInt, totalAmtPaid;
    			
    			str = AmtF.getText();
    			amt = Double.parseDouble(str);
    			
    			str = TermF.getText();
    			n = Double.parseDouble(str);
    			
    			str = RateF.getText();
    			rate = Double.parseDouble(str);
    			
    			rate = rate / 100 / 12;
    			denominator = 1- Math.pow(1 + rate, -n);
    			
    			monthlyPmt = amt * (rate / denominator);
    			
    			totalAmtPaid = monthlyPmt * n;
    			
    			totalInt = totalAmtPaid - amt;
    			
    			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
    					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
    					"Total Interest Paid: $" + dlr.format(totalInt), 
    					"Total", JOptionPane.PLAIN_MESSAGE);
    			
    			
    		}
    	}
    	
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);			
    		}
    	
    	}
    	
    	
    	
    	
    }
    But when the autoPmt button is clicked, I want it to call the buttonListener that is in the other class file. Which is the only buttonListener for that button now. Here is the error I'm getting with the: autoPmt.addActionListener(new AutoButtonListener());

    ( the "AutoButtonListener() is underlined in red with the errors

    It's telling me to create the class "AutoButtonListener" but it is created in the AutoCalc class file. How do I get it to recognize that??
  26. #29
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    By using the technique I talked about in post #19
    Replace
    Code:
    autoPmt.addActionListener(new AutoButtonListener());
    with
    Code:
    autoPmt.addActionListener(autoPanel.getAutoButtonListener());
    See post #23 for how to code the get method
  28. #30
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    By using the technique I talked about in post #19
    Replace
    Code:
    autoPmt.addActionListener(new AutoButtonListener());
    with
    Code:
    autoPmt.addActionListener(autoPanel.getAutoButtonListener());
    See post #23 for how to code the get method
    Ok here is my latest, it works until I click the autoPmt button, and then my program crashes.

    Code:
    package financialCalc;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    //import java.text.DecimalFormat;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FinancialCalc extends JFrame
    {
    	double balance, minimumPayment;
    	String payment, fixedPayment;
    	private JPanel welcomePanel, greetingPanel, buttonPanel;//, minPmtButtonPanel, minPmtPanel;
    	private JLabel message;//, Amt, Term, Rate, minPmt, fixedPmt;
    	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit;//, minPmtButton, calcPmts;
    	private JFrame mainFrame;//, mortPmtFrame, minPmtFrame;
    	private Dimension d = new Dimension(400,400);
    	private AutoCalc autoPanel;	
    	//private DecimalFormat dlr = new DecimalFormat("#0.00");
    	//private DecimalFormat pct = new DecimalFormat("#0");
    	//private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
    	
    	public FinancialCalc()
    	{
    		
    		mainFrame = new JFrame("Financial Calculator Application");		
    		
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    		
    		mainFrame.setPreferredSize(d);
    		mainFrame.setLayout(new BorderLayout());
    		mainFrame.setLocationRelativeTo(null);
    		
    		autoPanel = new AutoCalc();
    
    		buildWelcomeMenu();
    		
    		mainFrame.add(greetingPanel, BorderLayout.NORTH);
    		mainFrame.add(welcomePanel, BorderLayout.CENTER);
    		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
    		
    		mainFrame.pack();
    		mainFrame.setVisible(true);
    	}
    		
    	
    	public void buildWelcomeMenu()
    	{
    		welcomePanel = new JPanel();
    		welcomePanel.setLayout(new GridLayout(4,1));
    		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
    		
    		autoPmt = new JButton("Auto loan payment calculator");
    		autoPmt.setMnemonic(KeyEvent.VK_A);
    		autoPmt.addActionListener(autoPanel.getAutoButtonListener());		
    		
    		mortPmt = new JButton("Mortgage payment calculator");
    		mortPmt.setMnemonic(KeyEvent.VK_M);
    		//mortPmt.addActionListener(new MortButtonListener());
    		
    		homeAfford = new JButton("How much home can you afford?");
    		homeAfford.setMnemonic(KeyEvent.VK_H);
    		//homeAfford.addActionListener(new HomeButtonListener());
    		
    		costOfMin = new JButton("True cost of paying the minimum payment");
    		costOfMin.setMnemonic(KeyEvent.VK_C);
    		//costOfMin.addActionListener(new CostMinButtonListener());
    		
    		buttonPanel = new JPanel();
    		exit = new JButton("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    		buttonPanel.add(exit);
    		
    		welcomePanel.add(autoPmt);
    		welcomePanel.add(mortPmt);
    		welcomePanel.add(homeAfford);
    		welcomePanel.add(costOfMin);
    	
    		
    		greetingPanel = new JPanel();
    		message = new JLabel("Welcome to the Financial Calculations Application");
    		greetingPanel.add(message);
    		
    		
    	}
    	
    
    //HERE IS THE BEGINNING OF MY AUTOCALC CLASS FILE
    
    package financialCalc;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    
    public class AutoCalc extends JPanel
    {
    	private JLabel Amt, Term, Rate;
    	private JTextField AmtF, TermF, RateF;//, minPmtF, fixedPmtF;
    	private JFrame autoPmtFrame;
    	private DecimalFormat dlr = new DecimalFormat("#0.00");
    	private DecimalFormat pct = new DecimalFormat("#0");
    	private Dimension d = new Dimension(400,400);
    	JPanel autoPmtPanel, autoButtonPanel;
    
    	
    	
    		public class AutoButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				autoPmtFrame = new JFrame();
    				autoPmtFrame = new JFrame("Auto Payment Calculator");			
    				autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				autoPmtFrame.setLayout(new GridLayout(2,1));
    				autoPmtFrame.setPreferredSize(d);
    				autoPmtFrame.setLocationRelativeTo(null);
    
    				autoPmtFrame.add(autoPmtPanel);
    				autoPmtFrame.add(autoButtonPanel);
    			
    				autoPmtFrame.pack();
    				autoPmtFrame.setVisible(true);
    			
    				buildAutoCalc();			
    			
    			}
    	
    		}
    	
    	
    	
    	
    	public void buildAutoCalc()
    	{
    		
    		autoPmtPanel = new JPanel();
    		autoPmtPanel.setLayout(new GridLayout(4,2));
    		autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    		
    		
    		autoButtonPanel = new JPanel();		
    					
    		JButton calculate = new JButton("Calculate");
    		JButton exit = new JButton("Exit");
    		calculate.addActionListener(new AutoCalcButtonListener());
    		exit.addActionListener(new ExitButtonListener());
    		
    		Amt = new JLabel("Loan Amount");
    		Term = new JLabel("Term in months");
    		Rate = new JLabel("Interest Rate");
    		
    		
    		AmtF = new JTextField(5);
    		TermF = new JTextField(5);
    		RateF = new JTextField(5);
    		
    		autoPmtPanel.add(Amt);
    		autoPmtPanel.add(AmtF);
    		autoPmtPanel.add(Term);
    		autoPmtPanel.add(TermF);
    		autoPmtPanel.add(Rate);
    		autoPmtPanel.add(RateF);
    		
    		autoButtonPanel.add(calculate);
    		autoButtonPanel.add(exit);
    		
    		
    	}
    	
    	
    	private class AutoCalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String str;
    			double amt, n, rate, denominator;
    			double monthlyPmt, totalInt, totalAmtPaid;
    			
    			str = AmtF.getText();
    			amt = Double.parseDouble(str);
    			
    			str = TermF.getText();
    			n = Double.parseDouble(str);
    			
    			str = RateF.getText();
    			rate = Double.parseDouble(str);
    			
    			rate = rate / 100 / 12;
    			denominator = 1- Math.pow(1 + rate, -n);
    			
    			monthlyPmt = amt * (rate / denominator);
    			
    			totalAmtPaid = monthlyPmt * n;
    			
    			totalInt = totalAmtPaid - amt;
    			
    			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
    					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
    					"Total Interest Paid: $" + dlr.format(totalInt), 
    					"Total", JOptionPane.PLAIN_MESSAGE);
    			
    			
    		}
    	}
    	
    	
    	public AutoButtonListener getAutoButtonListener() 
    	{
    		return new AutoButtonListener();
    	}
    	
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);			
    		}
    	
    	}
    
    
    
    
    
    	
    	
    	
    	
    }
    and here are the errors I get when I click on the autoPmt button

    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.awt.Container.addImpl(Container.java:1086)
    	at java.awt.Container.add(Container.java:998)
    	at javax.swing.JFrame.addImpl(JFrame.java:562)
    	at java.awt.Container.add(Container.java:410)
    	at financialCalc.AutoCalc$AutoButtonListener.actionPerformed(AutoCalc.java:34)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:696)
    	at java.awt.EventQueue$4.run(EventQueue.java:694)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

IMN logo majestic logo threadwatch logo seochat tools logo