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 5 of 5

Thread: I do not know how to fix this.

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I do not know how to fix this.

    I am having a proble with my program. It compiles fine, but when I try to run, I ge tthe following:

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at MortCalcGUI.<init>(MtgCalc.java:293)
    at MtgCalc.main(MtgCalc.java:13)


    Here is the code:
    import java.text.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class MtgCalc{
     
     
       public static void main (String[]args){
     
     
    	      JFrame window = new MortCalcGUI();
    		  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		  window.setVisible(true);
    	}
     
    }

     class MortCalcGUI extends JFrame implements ActionListener{
     
          /*declare variables for GUI functions*/
            protected JButton bCal7,bCal15,bCal30, bClear, bExit;
    		protected JLabel labelPayment, labelLoanAmount, labelTitle, labelInstructions, labelAmTable;
    		protected JTextField fieldPayment, fieldLoanAmount;
    		protected JTextArea areaAmTable;
    		private static final long serialVersionUID = 1L;
     
     
    		/*declare loan array data  and variables and initialize*/
    		double intPaid, prinPaid,balance, mthlyPayment, loanAmount;
    		int[]loanTerm = {7,15,30};
    		double[] intRate = {5.35,5.50,5.75};
    		double loanAmt = loanAmount;
     
    		public void actionPerformed (ActionEvent e){
     
    		  /*action taken depending on what button is selected*/
    		  if ("Calculate7".equals(e.getActionCommand())){
     
     
     
    				NumberFormat currency = NumberFormat.getCurrencyInstance();
     
    				double paymentAmount = CalculatePayment7();
     
    				fieldPayment.setText(""+ (currency.format(paymentAmount)));
     
    				areaAmTable.append("Payment # \t Remaining Balance \t Interest Paid\n");	
     
    		      /*set loan amount */
    				loanAmt = Double.parseDouble(fieldLoanAmount.getText());
     
    				for (int x = 1; x <= (loanTerm[0]*12); x++){
    				/*this loops monthly payments for loans*/
     
    				       balance=loanAmount;
    						 /* sets Balance to the amount of the loan*/
     
    						 /*monthly payment calculations*/
     
    						 intPaid = (((intRate[0]/100.0)/12.0)*balance);
    						 prinPaid = (paymentAmount -intPaid);
    						 loanAmt =(balance-prinPaid);
     
    						 areaAmTable.append(x + "\t" +currency.format(loanAmt) + " \t\t " + currency.format(intPaid) + "\n");
    						 }
     
    						 if (balance <= 0){ /*Until Balance is zero*/
     
    						          areaAmTable.append ("\tRemaining balance =$0.00");
     
    						}
     
    			}	 
     
     
    			else if ("Calculate15".equals(e.getActionCommand())){
     
     
    				NumberFormat currency = NumberFormat.getCurrencyInstance();
     
    				double paymentAmount = CalculatePayment15();
     
    				fieldPayment.setText(""+ (currency.format(paymentAmount)));
     
    				areaAmTable.append("Payment # \t Remaining Balance \t Interest Paid\n");	
     
    		      /*set loan amount */
    				loanAmt = Double.parseDouble(fieldLoanAmount.getText());
     
    				for (int x = 1; x <= (loanTerm[1]*12); x++){
    				/*this loops monthly payments for loans*/
     
    				       balance=loanAmount;
    						 /* sets Balance to the amount of the loan*/
     
    						 /*montlhy payment calculations*/
     
    						 intPaid = (((intRate[1]/100.0)/12.0)*balance);
    						 prinPaid = (paymentAmount -intPaid);
    						 loanAmt =(balance-prinPaid);
     
    						 areaAmTable.append(x + "\t" +currency.format(loanAmt) + " \t\t " + currency.format(intPaid) + "\n");
    						 }
     
    						 if (balance <= 0){ /*Until Balance is zero*/
     
    						          areaAmTable.append ("\tRemaining balance =$0.00");
     
    						}
     
    			}	 
     
     
     
             else if ("Calculate30".equals(e.getActionCommand())){
     
     
    				    NumberFormat currency = NumberFormat.getCurrencyInstance();
     
    				    double paymentAmount = CalculatePayment30();
     
    				    fieldPayment.setText(""+ (currency.format(paymentAmount)));
     
    				    areaAmTable.append("Payment # \t Remaining Balance \t Interest Paid\n");	
     
    		          loanAmt = Double.parseDouble(fieldLoanAmount.getText());
     
     
    					 for (int x = 1; x <= (loanTerm[2]*12); x++){
     
     
    				           balance=loanAmount;
     
     
    						     intPaid = (((intRate[2]/100.0)/12.0)*balance);
    						     prinPaid = (paymentAmount -intPaid);
    						     loanAmt =(balance-prinPaid);
     
    						     areaAmTable.append(x + "\t" +currency.format(loanAmt) + " \t\t " + currency.format(intPaid) + "\n");
    						 }
     
    						 if (balance <= 0){ /*Until Balance is zero*/
     
    						          areaAmTable.append ("\tRemaining balance =$0.00");
     
    						}
     
    			}	 
     
     
                     else if ("Clear".equals(e.getActionCommand())){
     
    					          ClearFields ();
    					  }
     
    					  else {
     
    					  System.exit (0);
     
    					  }
     
    		 }
     
     
    		 public double CalculatePayment7 (){
     
    		 /*checks input for incorrect input type*/
     
    		    try {
    		          /*If input is corect,then perform calculation*/
    		          fieldPayment.setText ("");
    				    double Payment = (loanAmount() * (intRate[0]/100/12))/1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12);
    				    return Payment;
    			     }
     
    			 catch (NumberFormatException event){
     
     
    			        /*error displayed for incorrect input type*/
    					  JOptionPane.showMessageDialog(null, "Incorrect Input.\nEnter numeric values.", "ERROR", JOptionPane.ERROR_MESSAGE);
    				     return 0;
     
    				  }
     
     
    		}
     
     
    		public double CalculatePayment15 (){
     
    		 /*checks input for incorrect input type*/
     
    		    try {
    		          /*If input is coorect,then perform calculation*/
    		          fieldPayment.setText ("");
    				    double Payment = (loanAmount() * (intRate [1]/100/12))/1 - Math.pow(1/(1 + (intRate[1]/100/12)), loanTerm[1]*12);
    				    return Payment;
    			     }
     
    			 catch (NumberFormatException event){
     
     
    			        /*error displayed for incorrect input type*/
    					  JOptionPane.showMessageDialog(null, "Incorrect Input.\nEnter numeric values.", "ERROR", JOptionPane.ERROR_MESSAGE);
    				     return 0;
     
    				  }
     
     
    		}
     
     
          public double CalculatePayment30 (){
     
    		 /*checks input for incorrect input type*/
     
    		    try {
    		          /*If input is coorect,then perform calculation*/
    		          fieldPayment.setText ("");
    				    double Payment = (loanAmount() * (intRate[2]/100/12))/1 - Math.pow(1/(1 + (intRate[2]/100/12)), loanTerm[2]*12);
    				    return Payment;
    			 }
     
    			 catch (NumberFormatException event){
     
     
    			        /*error displayed for incorrect input type*/
    					  JOptionPane.showMessageDialog(null, "Incorrect Input.\nEnter numeric values.", "ERROR", JOptionPane.ERROR_MESSAGE);
    				     return 0;
     
    		 }
     
     
    	}
     
     
     
    	  public void ClearFields (){
     
    	       /*make all fields blank*/
    			 fieldPayment.setText ("");
    			 fieldLoanAmount.setText ("");
    			 areaAmTable.setText ("");
     
    	}		 
     
     
    	public MortCalcGUI (){
     
    	       /*sets and initialize buttons*/
    			 bCal7 = new JButton ("7 Year, 5.35%");
    			 bCal7.setActionCommand ("Calculate7");
    	         bCal15 = new JButton ("15 Year, 5.50%");
    			 bCal15.setActionCommand ("Calculate15");
    	         bCal30 = new JButton ("30 Year, 5.75%");
    			 bCal30.setActionCommand ("Calculate30");
    			 bClear = new JButton ("Reset all Fields");
    			 bClear.setActionCommand ("Reset");
    			 bExit = new JButton ("Exit Calculator");
    			 bExit.setActionCommand ("Exit");
     
     
    			 /*sets labels and field sizes for GUI*/
    			 labelTitle = new JLabel ("Mortgage Calculator");
    			 labelInstructions = new JLabel ("Enter loan amount and choose rate and term");
    			 labelPayment = new JLabel ("Monthly Payment");
    			 labelLoanAmount = new JLabel ("Amount of Loan");
    			 labelAmTable = new JLabel ("Amortization Table");
    			 areaAmTable = new JTextArea (10,  30);
     
    			 JScrollPane scrollPane = new JScrollPane (areaAmTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     
    			 /*set listeners for buttons*/
    			 bCal7.addActionListener(this);
    			 bCal15.addActionListener(this);
                 bCal30.addActionListener(this);
                 bClear.addActionListener(this);
                 bExit.addActionListener(this);
     
    			 /*GUI constructor and set layout*/
    			 JPanel mine = new JPanel ();
    			 mine.setLayout (null);
     
    			 mine.add (labelTitle);
    			 labelTitle.setBounds (110, 30, 700, 15);
     
    			 mine.add (labelInstructions);
    			 labelInstructions.setBounds ( 30, 70, 450, 15);
     
    			 mine.add (labelLoanAmount);
    			 labelLoanAmount.setBounds (130,  110, 100, 25);
     
    			 mine.add (fieldLoanAmount);
    			 fieldLoanAmount.setBounds (240, 110, 100, 25);
     
     
    			 mine.add (bCal7);
    			 bCal7.setBounds (40, 150, 125, 30);
     
    			 mine.add (bCal15);
    			 bCal15.setBounds (180, 150, 125, 30);
     
    			 mine.add (bCal30);
    			 bCal30.setBounds (320, 200, 100, 25);
     
    			 mine.add (labelPayment);
    			 labelPayment.setBounds (130, 200, 100, 25);
     
    			 mine.add (fieldPayment);
    			 fieldPayment.setBounds (240, 200, 100, 25);
    			 fieldPayment.setEditable (false);
     
    			 mine.add(labelAmTable);
    			 labelAmTable.setBounds (180, 250, 300, 25);
     
    			 /*add scrollpane and set bounds of scrollpane*/
     
    			 mine.add(scrollPane);
    			 scrollPane.setBounds (50, 280, 400, 270);
    			 areaAmTable.setEditable (false);
     
    			 mine.add(bClear);
    			 bClear.setBounds (250, 570, 125, 25);
     
    			 mine.add (bExit);
    			 bExit.setBounds (250, 570, 125, 30);
     
    			 this.setContentPane(mine);
    			 this.pack();
    			 this.setTitle ("Mortgage Calculator");
     
    			 /*set calculator window size*/
     
     
    			 }
     
    	 public double loanAmount (){
     
    	         double loanAmount = Double.parseDouble(fieldLoanAmount.getText());
    	         return loanAmount;
    	 }	
     
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I do not know how to fix this.

    Most people won't wade through elebenty gazillion lines of unformatted code. Please indicate on which line the error occurs.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I do not know how to fix this.

    The lines I am receiving errors on are in bold.

    JFrame window = new MortCalcGUI();

    mine.add (fieldLoanAmount);

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I do not know how to fix this.

    JFrame window = new MortCalcGUI();
    This line is not causing the error. It is just included in the stack trace.
    mine.add (fieldLoanAmount);
    This line has two likely culprits: mine and fieldLoanAmount. You can place print statements before this line to see which variable is null. Once you know which one then look through your code to find out why it is null.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I do not know how to fix this.

    Thank you. I will see if that works.