Link to home
Start Free TrialLog in
Avatar of neonate928
neonate928

asked on

The program builds but will not calculate

I am new to java and am taking a java class, where I have to make a mortgage calculator allowing the user to either enter all info, principal, term and interest or enter the principal and then use a predefined term and interest rate. Thus far I have the layout and have begun to work on the "predefined" variables. I am using a combo box to allow the user to select the term which will then select the interest. However, when I choose the term through the combo box I recieve an error a bunch of errors in the netbeans IDE. But I cannot figure them out. Can someone please revice the code and tell me what I have done wrong.

Thx
/*
 * CalculatorView.java
 */
 
package calculator;
 
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
 
/**
 * The application's main frame.
 */
public class CalculatorView extends FrameView {
 
    public CalculatorView(SingleFrameApplication app) {
        super(app);
 
        initComponents();
 
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);
 
        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = CalculatorApp.getApplication().getMainFrame();
            aboutBox = new CalculatorAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        CalculatorApp.getApplication().show(aboutBox);
    }
 
    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
 
        mainPanel = new javax.swing.JPanel();
        bx_term = new javax.swing.JComboBox(terms);
        txtfld_term1 = new javax.swing.JLabel();
        label_interestauto = new javax.swing.JLabel();
        label_yearsauto = new javax.swing.JLabel();
        txtfld_principal = new javax.swing.JTextField();
        Principal = new javax.swing.JLabel();
        term = new javax.swing.JLabel();
        txtfld_term = new javax.swing.JTextField();
        label_interest = new javax.swing.JLabel();
        txtfld_interest = new javax.swing.JTextField();
        Principal1 = new javax.swing.JLabel();
        txtfld_principal1 = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
 
        mainPanel.setName("mainPanel"); // NOI18N
 
        bx_term.setName("bx_term"); // NOI18N
 
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(calculator.CalculatorApp.class).getContext().getResourceMap(CalculatorView.class);
        txtfld_term1.setText(resourceMap.getString("txtfld_term1.text")); // NOI18N
        txtfld_term1.setName("txtfld_term1"); // NOI18N
 
        label_interestauto.setText(resourceMap.getString("label_interestauto.text")); // NOI18N
        label_interestauto.setName("label_interestauto"); // NOI18N
 
        label_yearsauto.setText(resourceMap.getString("label_yearsauto.text")); // NOI18N
        label_yearsauto.setName("label_yearsauto"); // NOI18N
 
        txtfld_principal.setText(resourceMap.getString("txtfld_principal.text")); // NOI18N
        txtfld_principal.setName("txtfld_principal"); // NOI18N
 
        Principal.setText(resourceMap.getString("Principal.text")); // NOI18N
        Principal.setName("Principal"); // NOI18N
 
        term.setText(resourceMap.getString("term.text")); // NOI18N
        term.setName("term"); // NOI18N
 
        txtfld_term.setText(resourceMap.getString("txtfld_term.text")); // NOI18N
        txtfld_term.setName("txtfld_term"); // NOI18N
 
        label_interest.setText(resourceMap.getString("label_interest.text")); // NOI18N
        label_interest.setName("label_interest"); // NOI18N
 
        txtfld_interest.setText(resourceMap.getString("txtfld_interest.text")); // NOI18N
        txtfld_interest.setName("txtfld_interest"); // NOI18N
 
        Principal1.setText(resourceMap.getString("Principal1.text")); // NOI18N
        Principal1.setName("Principal1"); // NOI18N
 
        txtfld_principal1.setName("txtfld_principal1"); // NOI18N
 
        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label_yearsauto)
                    .addComponent(label_interestauto)
                    .addComponent(bx_term, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtfld_term1)
                    .addComponent(Principal1)
                    .addComponent(txtfld_principal1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addGap(121, 121, 121)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtfld_principal, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Principal)
                    .addComponent(term)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtfld_term, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtfld_interest, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(label_interest, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Principal1)
                    .addComponent(Principal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtfld_principal1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtfld_principal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtfld_term1)
                    .addComponent(term))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bx_term, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtfld_term, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_interestauto, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label_interest))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_yearsauto)
                    .addComponent(txtfld_interest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(150, Short.MAX_VALUE))
        );
 
        /** create our event handler for our combo box **/
        bx_term.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HandleComboBox(evt);
            }
        });
 
        menuBar.setName("menuBar"); // NOI18N
 
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N
 
        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(calculator.CalculatorApp.class).getContext().getActionMap(CalculatorView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);
 
        menuBar.add(fileMenu);
 
        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N
 
        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);
 
        menuBar.add(helpMenu);
 
        statusPanel.setName("statusPanel"); // NOI18N
 
        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
 
        statusMessageLabel.setName("statusMessageLabel"); // NOI18N
 
        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
 
        progressBar.setName("progressBar"); // NOI18N
 
        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 249, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );
 
        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>
    
    //HandleComboBox is called by the event handle for our combo box
    //Creates a temporary combo box to help set the arrays and to set the correct interest rates into the interest text field
    private void HandleComboBox(java.awt.event.ActionEvent evt)
    {
	//creating a temporary combo box
	//the source of our event is our combo box (bx_term)
	javax.swing.JComboBox temp_cb = (javax.swing.JComboBox)evt.getSource();
 
	//Creating a temporary String to be used to assign text for our txtfld_term.
	String temp_term = (String)temp_cb.getSelectedItem();
	txtfld_termauto.setText(temp_term);
	label_yearsauto.setText(temp_term + " years");
		
	//setting the index to our interest array
	int index = 0;
	switch((int)(Integer.parseInt(temp_term)))
	{
		case 7 :
        		index = 0;
                	break;
		case 15 :
			index = 1;
			break;
		case 30 :
			index = 2;
			break;
	}
	//using the index for our interest array
	txtfld_interestauto.setText(interest[index]+"");
	label_interestauto.setText("Interest Rate: " + interest[index] * 100 +"%");
    }
 
    // Variables declaration - do not modify
    private javax.swing.JLabel Principal;
    private javax.swing.JLabel Principal1;
    private javax.swing.JComboBox bx_term;
    private javax.swing.JLabel label_interest;
    private javax.swing.JLabel label_interestauto;
    private javax.swing.JLabel label_yearsauto;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JLabel term;
    private javax.swing.JTextField txtfld_interest;
    private javax.swing.JTextField txtfld_principal;
    private javax.swing.JTextField txtfld_principal1;
    private javax.swing.JTextField txtfld_term;
    private javax.swing.JLabel txtfld_term1;
    // End of variables declaration
    //arrays
    private String[] terms    = { "7", "15", "30" };
    private double[] interest = { 0.0535, 0.055, 0.0575 };
    
    //used for the auto calculations
    private javax.swing.JTextField txtfld_principalauto;
    private javax.swing.JTextField txtfld_termauto;
    private javax.swing.JTextField txtfld_interestauto;
    
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
 
    private JDialog aboutBox;
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>I recieve an error a bunch of errors in the netbeans IDE.

Please post them here
Did u kept appframework-1.0.3.jar and swingx-0.9.1.jar in classpath?

if so then post the errors you are getting.
Avatar of neonate928
neonate928

ASKER

These are the exception errors I recieve in the back ground in the Netbeans IDE:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at calculator.CalculatorView.HandleComboBox(CalculatorView.java:297)
        at calculator.CalculatorView.access$800(CalculatorView.java:22)
        at calculator.CalculatorView$4.actionPerformed(CalculatorView.java:221)
        at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1242)
        at javax.swing.JComboBox.setSelectedItem(JComboBox.java:569)
        at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:605)
        at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:814)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
        at java.awt.Component.processMouseEvent(Component.java:6041)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:480)
        at java.awt.Component.processEvent(Component.java:5806)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4413)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2440)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Make sure that the selecteditem is not null and that the components have been initialized in HandleComboBox
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!!!