Skip to main content

AWT-EventQueue-0" java.lang.NullPointerException

2 replies [Last post]
jlnickymaster
Offline
Joined: 2007-05-02

I have this error when I tried to run program. I understand that it is pointing to a
NULL value but I cannot find it. I think once you look at your code 5000 times you
should automatically get new eyeballs.

the error is listing:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mort.initComponents(mort.java:44)
at mort.(mort.java:33)
at mort$2.run(mort.java:141)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)

Here is my program for Mortgage Loan Calculations. I have to use Swing and It
has to calculate the monthly payments and the remianing principle. I am using
Eclipes. I dont get any errors on the code itself but once I run it...KAPOW
Batman. If someone could take a look and see if they could find the issue.
Thanks

<br />
import javax.swing.GroupLayout.SequentialGroup;</p>
<p>	/**<br />
	 Develop an application that will use Swing to calculate<br />
	 the monthly payments of a mortgage. Allow the user to type<br />
	 in the following information:</p>
<p>		Initial mortgage amount.<br />
		Rate.<br />
		Number of years. </p>
<p>	 Once this information is entered, the application provide the<br />
	 user with two pieces of information:</p>
<p>		Amount of monthly payment.<br />
		How much is left on the principle. </p>
<p>	 Note that you will need to research how these rates<br />
	 need to be calculated. Selecting an appropriate integrated<br />
	 development environment (IDE) for the application is an<br />
	 important part of the assignment. Include your rationale<br />
	 for the IDE you select in the header comments for the .java files.</p>
<p>	 Submit this lab to your instructor as a zip file named<br />
	 U03A1_Yourname.zip. Include a file that indicates how to<br />
	 build and run the exercise. Follow good documenting and<br />
	 commenting practices.<br />
	 */</p>
<p>public class mort extends javax.swing.JFrame{</p>
<p>	public mort() {<br />
        initComponents();<br />
    }</p>
<p>    private void initComponents() {<br />
        tempTextField1 = new javax.swing.JTextField();<br />
        amtLabel = new javax.swing.JLabel();<br />
        tempTextField2 = new javax.swing.JTextField();<br />
        rateLabel = new javax.swing.JLabel();<br />
        tempTextField3 = new javax.swing.JTextField();<br />
        yearLabel = new javax.swing.JLabel();<br />
        submitButton = new javax.swing.JButton();<br />
        amtLeftLabel.setText("Loan Amount Left");<br />
        principleLeftLabel.setText("Principle Amount Left");</p>
<p>        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);<br />
        setTitle("Mortgage Payment Calculator");</p>
<p>        amtLabel.setText("Initial Mortgage Amount");<br />
        rateLabel.setText("Interest Rate");<br />
        yearLabel.setText("Number of Years");</p>
<p>        submitButton.setText("Submit");<br />
        submitButton.addActionListener(new java.awt.event.ActionListener() {<br />
            public void actionPerformed(java.awt.event.ActionEvent evt) {<br />
                submitButtonActionPerformed(evt);<br />
            }<br />
        });</p>
<p>        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());<br />
        getContentPane().setLayout(layout);<br />
        layout.setHorizontalGroup(<br />
            (layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
            .addGroup(layout.createSequentialGroup()<br />
                .addContainerGap()<br />
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
                    .addGroup(layout.createSequentialGroup()<br />
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                        .addComponent(amtLabel))<br />
                    .addGroup(layout.createSequentialGroup()<br />
                        .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                        .addComponent(rateLabel))<br />
                    .addGroup(layout.createSequentialGroup()<br />
                        .addComponent(tempTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                        .addComponent(yearLabel))<br />
                    .addGroup(layout.createSequentialGroup()<br />
                        .addComponent(submitButton)<br />
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                        .addComponent(amtLeftLabel))<br />
                        .addComponent(principleLeftLabel)))));</p>
<p>        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {submitButton, tempTextField1, tempTextField2, tempTextField3});</p>
<p>        layout.setVerticalGroup(<br />
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
                .addGroup(layout.createSequentialGroup()<br />
                    .addContainerGap()<br />
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                        .addComponent(amtLabel))<br />
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                        .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                        .addComponent(rateLabel))<br />
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                        .addComponent(tempTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                        .addComponent(yearLabel))<br />
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                        .addComponent(submitButton)<br />
                        .addComponent(amtLeftLabel)<br />
                        .addComponent(principleLeftLabel))<br />
                    .addContainerGap(21, Short.MAX_VALUE))<br />
            );<br />
        pack();<br />
    }// //GEN-END:initComponents</p>
<p>    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed<br />
    	//create calculation of mortgate payment from initial info</p>
<p>    	        double P = (double)((Double.parseDouble(tempTextField1.getText()))); //Principle<br />
    	        double I = (double)((Double.parseDouble(tempTextField2.getText()))); //annual interest rate from 1 to 100<br />
    	        double L= (double)((Double.parseDouble(tempTextField3.getText()))); //length, the length (in years) of the loan, or at least the length over which the loan is amortized<br />
    	        double J = I/(12 * 100); //monthly interest in decimal form = I / (12 x 100)<br />
    	        double N = L * 12; //number of months over which loan is amortized </p>
<p>    	        double M = (P * J) / (1 - Math.pow(1/ (1 + I), L * 12)); //monthly payment</p>
<p>    	        //getting the remaining principle<br />
    	        double H = P * J; //current monthly interest<br />
    	        double C = M - H; //monthly payment - monthly interest<br />
    	        double Q = P - C; //new balance of principle on loan</p>
<p>    	        //the labels to fill with M and Q<br />
    	        amtLeftLabel.setText( M + " Is The Loan Amount Left");<br />
    	        principleLeftLabel.setText(Q + " Principle Amount Left");<br />
    	    }//GEN-LAST:event_convertButtonActionPerformed</p>
<p>    	    /**<br />
    	     * @param args the command line arguments<br />
    	     */<br />
    	    public static void main(String args[]) {<br />
    	        java.awt.EventQueue.invokeLater(new Runnable() {<br />
    	            public void run() {<br />
    	                new mort().setVisible(true);<br />
    	            }<br />
    	        });<br />
    	    }</p>
<p>    	    // Variables declaration - do not modify//GEN-BEGIN:variables<br />
    	    private javax.swing.JTextField tempTextField1;<br />
    	    private javax.swing.JLabel amtLabel;<br />
    	    private javax.swing.JTextField tempTextField2;<br />
    	    private javax.swing.JLabel rateLabel;<br />
    	    private javax.swing.JTextField tempTextField3;<br />
    	    private javax.swing.JLabel yearLabel;<br />
    	    private javax.swing.JButton submitButton;<br />
    	    private javax.swing.JLabel amtLeftLabel;<br />
    	    private javax.swing.JLabel principleLeftLabel;<br />
    	    // End of variables declaration//GEN-END:variables</p>
<p>    	}</p>
<p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jlnickymaster
Offline
Joined: 2007-05-02

great answer

tarbo
Offline
Joined: 2006-12-18

As far as I can tell, amtLeftLabel and principleLeftLabel are never initialised. The call to setText(String) on either field will provoke a NullPointerException.