11 Replies - 755 Views - Last Post: 07 August 2011 - 03:14 PM Rate Topic: -----

#1 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Getting a null pointer exception. What is my problem?

Posted 06 August 2011 - 07:02 PM

  
   import java.awt.*;                                    
   import java.awt.event.*;  
   import javax.swing.*; 
   import java.io.*; 
   import java.text.*; 

   class AMORTIZATION extends JFrame
   {  
      int yearsArray[] = { 7, 15, 30 };		 
      double InterestArray[] = { 5.35, 5.5, 5.75 };
      public JLabel MonthlyPaymentLabel;
      public JLabel LoanSelectLabel;
      public JLabel LoanInterestLabel;
      public JTextField LoanAmountText;
      public JTextField LoanInterestRate;
      public JTextField Duration;
      public JTextArea MonthlyPayments;
      public JComboBox combo;
      public JRadioButton custom;
      public JRadioButton normal;
   	   
      public AMORTIZATION (){
        
         setSize(555, 520); 
         setTitle("Mortgage Calculator");  
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setResizable(true);  
         MonthlyPaymentLabel = new JLabel("$0.00");
         LoanSelectLabel = new JLabel(" Select Loan  ");
         LoanInterestLabel = new JLabel(" Enter Interest %  ");
       	
         LoanAmountText = new JTextField("200000.00");
         LoanInterestRate = new JTextField("5.5");
         Duration = new JTextField("10");
         MonthlyPayments = new JTextArea(20, 40);
        
         JPanel panel = new JPanel(); 
         JPanel Lpanel = new JPanel();
         JPanel Bpanel = new JPanel();
         JPanel Boxpanel = new JPanel();  
         JPanel Scrpanel = new JPanel();
         JPanel Radpanel = new JPanel(); 
         JPanel MPpanel = new JPanel();  
      
         JRadioButton custom = new JRadioButton ("Custom", false);
         JRadioButton normal = new JRadioButton ("Default", true);
      
         JButton calculateButton = new JButton("Calculate");
         JButton clearButton = new JButton("Clear");
         
         MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
      
         JLabel LoanAmount = new JLabel("Loan Amount");
      
      
         Lpanel.add(LoanAmount);
         Lpanel.add(LoanAmountText);
      
         panel.add(LoanInterestLabel);
         panel.add(LoanInterestRate);
      
         panel.add(Duration);
       
         Bpanel.add(calculateButton);
         Bpanel.add(clearButton);  
        
         Boxpanel.add(combo);  
      
         MPpanel.add(MonthlyPaymentLabel);
                        
         Radpanel.add(custom);
         Radpanel.add(normal);
      
         JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);	
         Scrpanel.add(scrollingArea); 

         custom.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){hidecustom();}});
         normal.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){displaycustom();}});
         String loanOptions[] = {"30 years @ 5.75%","15 years @ 5.5%","7 years @ 5.35%"};
         combo = new JComboBox(loanOptions);
         calculateButton.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){DetermineInt();}});
         clearButton.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){clearAll();}});
  
         setLayout(new FlowLayout(FlowLayout.LEFT));
         getContentPane().add(panel); 
         getContentPane().add(Radpanel); 
         getContentPane().add(Lpanel);
         getContentPane().add(Bpanel);
         getContentPane().add(Boxpanel);
         getContentPane().add(Scrpanel);
         getContentPane().add(MPpanel); 
      
         Duration.setVisible(false);	
         LoanInterestLabel.setVisible(false);	
         LoanInterestRate.setVisible(false);				  
      }   
     
      public void DetermineInt()
      {
         int currentNumber = 0;
         double initialAmount = Double.parseDouble(LoanAmountText.getText());
         double Irate = InterestArray[combo.getSelectedIndex()];
         NumberFormat nf = NumberFormat.getCurrencyInstance();
         if (custom.isSelected()){
            Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
            currentNumber = Integer.parseInt(Duration.getText());
         }
        
         int cn = currentNumber * 12;
         double ir = Irate/12;
         double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
         MonthlyPaymentLabel.setText("Monthly Payment $" + nf.format(monthly_payment));
            
         int index = 1;
         double start = initialAmount;
         double end = initialAmount;
         double MnthInt;
         double totInt = 0.0;
         String printString = "";
         printString += "#" + index + "\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
      				
         for(index = 1; index <= currentNumber * 12; index++){
            double Temp = start;
            double tempInt = 0.0;
            MnthInt = 0.0;
            tempInt = (Temp * (1.00 + ir / 12)) - Temp;
         
            Temp = Temp - monthly_payment;
            Temp += tempInt;
            MnthInt += tempInt;
            end = Temp;
            totInt = totInt + tempInt;
            printString += "" + index + "\t" + nf.format(start) + "\t" + nf.format(end) + "\t" + nf.format(MnthInt) + "\t" + nf.format(totInt) + "\n";
            start = end;
         }
         MonthlyPayments.setText(printString);	
      }
      public void displaycustom(){
         combo.setVisible(false);
         custom.setSelected(true);
         normal.setSelected(false);
         LoanSelectLabel.setText("Enter Duration (years)");
         LoanInterestLabel.setVisible(true);
         Duration.setVisible(true);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(true);	
         LoanInterestRate.setColumns(15);
      }
   
      public void hidecustom(){
         combo.setVisible(true);
         custom.setSelected(false);
         normal.setSelected(true);
         LoanSelectLabel.setText("Select Loan");
         LoanInterestLabel.setVisible(false);
         Duration.setVisible(false);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(false);	
         LoanInterestRate.setColumns(15);
      }
      public void clearAll(){
         MonthlyPayments.setText("");
         MonthlyPaymentLabel.setText("$0.00"); 
         LoanAmountText.setText("$200000.00");
         Duration.setText("10");
         LoanInterestRate.setText("5.35");
         combo.setSelectedIndex(1);
         hidecustom();
      }
    public static void main (String []  args)throws IOException
      
      {   AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
         MyAMORTIZATION.setVisible (true);     
      }
   }



This shows the error message I recieve.

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at AMORTIZATION.<init>(AMORTIZATION.java:91)
at AMORTIZATION.main(AMORTIZATION.java:138)

Is This A Good Question/Topic? 0
  • +

Replies To: Getting a null pointer exception. What is my problem?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: Getting a null pointer exception. What is my problem?

Posted 06 August 2011 - 07:49 PM

line 68 Boxpanel.add(combo); <-- combo is null

line 85 combo = new JComboBox(loanOptions); <-- this should have been done before 68
Was This Post Helpful? 1
  • +
  • -

#3 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 06 August 2011 - 08:22 PM

View PostCasiOo, on 06 August 2011 - 08:49 PM, said:

line 68 Boxpanel.add(combo); <-- combo is null

line 85 combo = new JComboBox(loanOptions); <-- this should have been done before 68

Thank you CasiOo. Your solution is most helpful. I got the GUI to display but I cannot get any output in the scroll area.
Was This Post Helpful? 0
  • +
  • -

#4 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 06 August 2011 - 10:12 PM

   import java.awt.*;                                     
   import java.awt.event.*;  
   import javax.swing.*; 
   import java.io.*; 
   import java.text.*; 


   class AMORTIZATION extends JFrame
   {  
    
      
      int yearsArray[] = { 7, 15, 30 };		 
      double InterestArray[] = { 5.35, 5.5, 5.75 };
      public JLabel MonthlyPaymentLabel;
      public JTextField LoanAmountText;
      public JTextField LoanInterestRate;
      public JTextField Duration;
      public JComboBox combo;
      public JLabel LoanSelectLabel;
      public JLabel LoanInterestLabel;
      public JTextArea MonthlyPayments;
      public JRadioButton custom;
      public JRadioButton normal;
   	
      JPanel panel = new JPanel(); 
      JPanel Lpanel = new JPanel();
      JPanel Bpanel = new JPanel();
      JPanel Boxpanel = new JPanel();  
      JPanel Scrpanel = new JPanel();
      JPanel Radpanel = new JPanel(); 
      JPanel MPpanel = new JPanel(); 
   	   
      public AMORTIZATION (){
        
         setSize(555, 520); 
         setTitle("Mortgage Calculator");  
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setResizable(true);  
         MonthlyPaymentLabel = new JLabel("$0.00");
         LoanAmountText = new JTextField("200000.00");
         LoanInterestRate = new JTextField("5.5");
         Duration = new JTextField("10");
         MonthlyPayments = new JTextArea(20, 40);
         LoanSelectLabel = new JLabel(" Select Loan  ");
         LoanInterestLabel = new JLabel(" Enter Interest %  ");
      
      	
          
      
         JRadioButton custom = new JRadioButton ("Custom", false);
         JRadioButton normal = new JRadioButton ("Default", true);
      
         JButton calculateButton = new JButton("Calculate");
         JButton clearButton = new JButton("Clear");
         
      
      
         MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
      
         JLabel LoanAmount = new JLabel("Loan Amount");
      
      
      
                  
         String loanOptions[] = {"30 years @ 5.75%","15 years @ 5.5%","7 years @ 5.35%"};
         combo = new JComboBox(loanOptions);
         Boxpanel.add(combo);                          
       
         custom.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){hidecustom();}});
         normal.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){displaycustom();}});
         
        
         calculateButton.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){DetermineInt();}});
         clearButton.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){clearAll();}});
       
      
         Lpanel.add(LoanAmount);
         Lpanel.add(LoanAmountText);
      
         panel.add(LoanInterestLabel);
         panel.add(LoanInterestRate);
      
         panel.add(Duration);
       
      
      
         Bpanel.add(calculateButton);
         Bpanel.add(clearButton);  
        
      
      
      
         MPpanel.add(MonthlyPaymentLabel);
                        
         Radpanel.add(custom);
         Radpanel.add(normal);
      
         JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);	
         Scrpanel.add (new JScrollPane(scrollingArea)); 
         Scrpanel.add(MonthlyPayments);
      
      
         setLayout(new FlowLayout(FlowLayout.LEFT));
         getContentPane().add(panel); 
         getContentPane().add(Radpanel); 
         getContentPane().add(Lpanel);
         getContentPane().add(Bpanel);
         getContentPane().add(Boxpanel);
         getContentPane().add(MPpanel); 
         getContentPane().add(Scrpanel);
      
      
         Duration.setVisible(false);	
         LoanInterestLabel.setVisible(false);	
         LoanInterestRate.setVisible(false);				  
      }   
      public static void main (String []  args)throws IOException
      
      {   AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
         MyAMORTIZATION.setVisible (true); 
      
      }
      public void DetermineInt()
      {
         int currentNumber = 0;
         double initialAmount = Double.parseDouble(LoanAmountText.getText());
         double Irate = InterestArray[combo.getSelectedIndex()];
         NumberFormat nf = NumberFormat.getCurrencyInstance();
         if (custom.isSelected()){
            Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
            currentNumber = Integer.parseInt(Duration.getText());
         }
        
         int cn = currentNumber * 12;
         double ir = Irate/12;
         double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
         MonthlyPaymentLabel.setText("Monthly Payment $" + nf.format(monthly_payment));
            
         int index;
         double start = initialAmount;
         double end = initialAmount;
         double MnthInt;
         double totInt = 0.0;
         String displayString = "";
         displayString += "#\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
      				
         for(index = 1; index <= currentNumber * 12; index++){
            double Temp = start;
            double tempInt = 0.0;
            MnthInt = 0.0;
            tempInt = (Temp * (1.00 + ir / 12)) - Temp;
         
            Temp = Temp - monthly_payment;
            Temp += tempInt;
            MnthInt += tempInt;
            end = Temp;
            totInt = totInt + tempInt;
            displayString += "#" + index + "\t" + nf.format(start) + "\t" + nf.format(end) + "\t" + nf.format(MnthInt) + "\t" + nf.format(totInt) + "\n";
            start = end;
         }
         MonthlyPayments.setText(displayString);	
         pack();
      }
      public void displaycustom(){
         combo.setVisible(false);
         custom.setSelected(true);
         normal.setSelected(false);
         LoanSelectLabel.setText("Enter Duration (years)");
         LoanSelectLabel.setVisible(true);
         LoanInterestLabel.setVisible(true);
         Duration.setVisible(true);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(true);	
         LoanInterestRate.setColumns(10);
      }
   
      public void hidecustom(){
         combo.setVisible(true);
         custom.setSelected(false);
         normal.setSelected(true);
         LoanSelectLabel.setText("Select Loan");
         LoanInterestLabel.setVisible(false);
         Duration.setVisible(false);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(false);	
         LoanInterestRate.setColumns(10);
      }
      public void clearAll(){
         MonthlyPayments.setText("");
         MonthlyPaymentLabel.setText("Monthly Payment $0.00"); 
         LoanAmountText.setText("$200000.00");
         Duration.setText("10");
         LoanInterestRate.setText("5.35");
         combo.setSelectedIndex(0);
         hidecustom();
      }
   
   }

the program will run but does not dsiplay the strings
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Getting a null pointer exception. What is my problem?

Posted 06 August 2011 - 10:16 PM

Duplicate threads merged. Please avoid duplicate posting.

I just ran your code and got a NullPointerException. You didn't initialize this.custom, so in DetermineInt(), you get a NullPointerException when determining if this.custom.isSelected().

Why do you re-declare custom in the constructor? This refers to a local variable, not the instance field.
JRadioButton custom = new JRadioButton ("Custom", false);


Was This Post Helpful? 1
  • +
  • -

#6 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 04:55 AM

   import java.awt.*;                                     
   import java.awt.event.*;  
   import javax.swing.*; 
   import java.io.*; 
   import java.text.*; 


   class AMORTIZATION extends JFrame
   {  
    
      
      int yearsArray[] = { 7, 15, 30 };		 
      double InterestArray[] = { 5.35, 5.5, 5.75 };
      public JLabel MonthlyPaymentLabel;
      public JTextField LoanAmountText;
      public JTextField LoanInterestRate;
      public JTextField Duration;
      public JComboBox combo;
      public JLabel LoanSelectLabel;
      public JLabel LoanInterestLabel;
      public JTextArea MonthlyPayments;
      public JRadioButton custom;
      public JRadioButton normal;
   	
      JPanel panel = new JPanel(); 
      JPanel Lpanel = new JPanel();
      JPanel Bpanel = new JPanel();
      JPanel Boxpanel = new JPanel();  
      JPanel Scrpanel = new JPanel();
      JPanel Radpanel = new JPanel(); 
      JPanel MPpanel = new JPanel(); 
   	   
      public AMORTIZATION (){
        
         setSize(555, 520); 
         setTitle("Mortgage Calculator");  
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setResizable(true);  
      
         LoanAmountText = new JTextField("200000.00");
         LoanInterestRate = new JTextField("5.5");
         Duration = new JTextField("10");
         MonthlyPayments = new JTextArea(20, 40);
         LoanSelectLabel = new JLabel(" Select Loan  ");
         LoanInterestLabel = new JLabel(" Enter Interest %  ");
      
      	
          
      
         JRadioButton custom = new JRadioButton ("Custom", false);
         JRadioButton normal = new JRadioButton ("Default", true);
      
         JButton calculateButton = new JButton("Calculate");
         JButton clearButton = new JButton("Clear");
         
      
      
         MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
      
         JLabel LoanAmount = new JLabel("Loan Amount");
      
      
      
                  
         String loanOptions[] = {"30 years @ 5.75%","15 years @ 5.5%","7 years @ 5.35%"};
         combo = new JComboBox(loanOptions);
         Boxpanel.add(combo);                          
       
         normal.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){hidecustom();}});
      	
         custom.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){displaycustom();}});
        
          
         Lpanel.add(LoanAmount);
         Lpanel.add(LoanAmountText);
      
         panel.add(LoanInterestLabel);
         panel.add(LoanInterestRate);
      
         panel.add(Duration);
       
      
      
         Bpanel.add(calculateButton);
         Bpanel.add(clearButton);  
        
      
      
      
         MPpanel.add(MonthlyPaymentLabel);
                        
         Radpanel.add(custom);
         Radpanel.add(normal);
      
        
      
         setLayout(new FlowLayout(FlowLayout.LEFT));
         getContentPane().add(panel); 
         getContentPane().add(Radpanel); 
         getContentPane().add(Lpanel);
         getContentPane().add(Bpanel);
         getContentPane().add(Boxpanel);
         getContentPane().add(MPpanel); 
         getContentPane().add(Scrpanel);
      
      
         Duration.setVisible(false);	
         LoanInterestLabel.setVisible(false);	
         LoanInterestRate.setVisible(false);		
      	 
         calculateButton.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){DetermineInt();}});
         clearButton.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){clearAll();}});
      				
         JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);	
         Scrpanel.add (new JScrollPane(scrollingArea)); 
         Scrpanel.add(MonthlyPayments);
      			
       
        
      }   
      public static void main (String []  args)throws IOException
      
      {   AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
         MyAMORTIZATION.setVisible (true); 
      
      }
      public void DetermineInt()
      {
         int currentNumber = 0;
         double initialAmount = Double.parseDouble(LoanAmountText.getText());
         double Irate = InterestArray[combo.getSelectedIndex()];
         NumberFormat nf = NumberFormat.getCurrencyInstance();
         if (custom.isSelected()){
            Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
            currentNumber = Integer.parseInt(Duration.getText());
         }
        
         int cn = currentNumber * 12;
         double ir = Irate/12;
         double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
         MonthlyPaymentLabel.setText("Monthly Payment $" + nf.format(monthly_payment));
            
         int index;
         double start = initialAmount;
         double end = initialAmount;
         double MnthInt;
         double totInt = 0.0;
         String displayString = "";
         displayString += "#\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
      				
         for(index = 1; index <= currentNumber * 12; index++){
            double Temp = start;
            double tempInt = 0.0;
            MnthInt = 0.0;
            tempInt = (Temp * (1.00 + ir / 12)) - Temp;
         
            Temp = Temp - monthly_payment;
            Temp += tempInt;
            MnthInt += tempInt;
            end = Temp;
            totInt = totInt + tempInt;
            displayString += "#" + index + "\t" + nf.format(start) + "\t" + nf.format(end) + "\t" + nf.format(MnthInt) + "\t" + nf.format(totInt) + "\n";
            start = end;
         }
         MonthlyPayments.setText(displayString);	
         
      }
      public void displaycustom(){
         combo.setVisible(false);
         custom.setSelected(true);
         normal.setSelected(false);
         LoanSelectLabel.setText("Enter Duration (years)");
         LoanSelectLabel.setVisible(true);
         LoanInterestLabel.setVisible(true);
         Duration.setVisible(true);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(true);	
         LoanInterestRate.setColumns(10);
      }
   
      public void hidecustom(){
         combo.setVisible(true);
         custom.setSelected(false);
         normal.setSelected(true);
         LoanSelectLabel.setText("Select Loan");
         LoanInterestLabel.setVisible(false);
         Duration.setVisible(false);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(false);	
         LoanInterestRate.setColumns(10);
      }
      public void clearAll(){
         MonthlyPayments.setText("");
         MonthlyPaymentLabel.setText("Monthly Payment $0.00"); 
         LoanAmountText.setText("$200000.00");
         Duration.setText("10");
         LoanInterestRate.setText("5.35");
         combo.setSelectedIndex(0);
         hidecustom();
      }
   
   }

Some of my fields are missing and the JTextarea will not display the string.
Was This Post Helpful? 0
  • +
  • -

#7 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 06:17 AM

I made some changes and some issues are resolved, but the output will not show in the JText area. Any suggestions would be appreciated.

   import java.awt.*;                                     
   import java.awt.event.*;  
   import javax.swing.*; 
   import java.io.*; 
   import java.text.*; 


   class AMORTIZATION extends JFrame
   {  
    
      
      int yearsArray[] = { 7, 15, 30 };		 
      double InterestArray[] = { 5.35, 5.5, 5.75 };
      public JLabel MonthlyPaymentLabel;
      public JTextField LoanAmountText;
      public JTextField LoanInterestRate;
      public JTextField Duration;
      public JComboBox combo;
      public JLabel LoanSelectLabel;
      public JLabel EnterDuration;
      public JLabel LoanInterestLabel;
      public JTextArea MonthlyPayments;
      public JRadioButton custom;
      public JRadioButton normal;
   	
      JPanel panel = new JPanel(); 
      JPanel Lpanel = new JPanel();
      JPanel Bpanel = new JPanel();
      JPanel Boxpanel = new JPanel();  
      JPanel Scrpanel = new JPanel();
      JPanel Radpanel = new JPanel(); 
      JPanel MPpanel = new JPanel(); 
   	   
      public AMORTIZATION (){
        
         setSize(555, 620); 
         setTitle("Mortgage Calculator");  
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setResizable(true);  
      
         LoanAmountText = new JTextField("200000.00");
         LoanInterestRate = new JTextField("       5.5");
        
         MonthlyPayments = new JTextArea(20, 40);
         LoanSelectLabel = new JLabel("  Select Loan  ");
         LoanInterestLabel = new JLabel("                Enter Interest %  ");
         EnterDuration = new JLabel ("Enter Number of Years");
         Duration = new JTextField("             10");
      	
          
      
         JRadioButton custom = new JRadioButton ("Custom", false);
         JRadioButton normal = new JRadioButton ("Default", true);
      
                       
      
      
         MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
      
         JLabel LoanAmount = new JLabel("Loan Amount");
      
      
      
                  
         String loanOptions[] = {"30 years @ 5.75%","15 years @ 5.5%","7 years @ 5.35%"};
         combo = new JComboBox(loanOptions);
         Boxpanel.add(combo);                          
       
         normal.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){hidecustom();}});
      	
         custom.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){displaycustom();}});
        
          
         Lpanel.add(LoanAmount);
         Lpanel.add(LoanAmountText);
         Lpanel.add(Duration);
         Lpanel.add(EnterDuration);
      
         panel.add(LoanInterestLabel);
         panel.add(LoanInterestRate);
         panel.add(custom);
         panel.add(normal);
      
        
         Boxpanel.add(LoanSelectLabel);
       
      
      
         
        
      
      
      
         MPpanel.add(MonthlyPaymentLabel);
                        
        
      
        
      
         setLayout(new FlowLayout(FlowLayout.LEFT));
         getContentPane().add(panel); 
         getContentPane().add(Radpanel); 
         getContentPane().add(Lpanel);
         getContentPane().add(Bpanel);
         getContentPane().add(Boxpanel);
         getContentPane().add(MPpanel); 
         getContentPane().add(Scrpanel);
      
      
        
         JButton calculateButton = new JButton("Calculate");
      
         calculateButton.addActionListener(
               new ActionListener(){
                  @Override
                  public void actionPerformed(ActionEvent event){DetermineInt();}});
         JButton clearButton = new JButton("Clear");
      
         clearButton.addActionListener(
               new ActionListener(){
                  @Override
                  public void actionPerformed(ActionEvent event){clearAll();}});
                  
      				
         Bpanel.add(calculateButton);
         Bpanel.add(clearButton); 
         			
         JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);	
         Scrpanel.add (scrollingArea); 
         Scrpanel.add(MonthlyPayments);
      			
       
        
      }   
      public static void main (String []  args)throws IOException
      
      {   AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
         MyAMORTIZATION.setVisible (true); 
      
      }
      public void DetermineInt()
      {
         int currentNumber = 0;
         double initialAmount = Double.parseDouble(LoanAmountText.getText());
         double Irate = InterestArray[combo.getSelectedIndex()];
        DecimalFormat twoDigits = new DecimalFormat("0.00");
         if (custom.isSelected()){
            Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
            currentNumber = Integer.parseInt(Duration.getText());
         }
        
         int cn = currentNumber * 12;
         double ir = Irate/12;
         double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
         MonthlyPaymentLabel.setText("Monthly Payment $" + twoDigits.format(monthly_payment));
            
         int index;
         double start = initialAmount;
         double end = initialAmount;
         double MnthInt;
         double totInt = 0.0;
         String displayString = "";
         displayString += "#\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
      				
         for(index = 1; index <= currentNumber * 12; index++){
            double Temp = start;
            double tempInt = 0.0;
            MnthInt = 0.0;
            tempInt = (Temp * (1.00 + ir / 12)) - Temp;
         
            Temp = Temp - monthly_payment;
            Temp += tempInt;
            MnthInt += tempInt;
            end = Temp;
            totInt = totInt + tempInt;
            displayString += "#" + index + "\t" + twoDigits.format(start) + "\t" + twoDigits.format(end) + "\t" +twoDigits.format(MnthInt) + "\t" + twoDigits.format(totInt) + "\n";
            start = end;
         }
         MonthlyPayments.setText(displayString);	
         Duration.setVisible(false);	
         LoanInterestLabel.setVisible(false);	
         LoanInterestRate.setVisible(false);		
      }
      public void displaycustom(){
         combo.setVisible(false);
         custom.setSelected(true);
         normal.setSelected(false);
         LoanSelectLabel.setText("Enter Duration (years)");
         LoanSelectLabel.setVisible(true);
         LoanInterestLabel.setVisible(true);
         Duration.setVisible(true);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(true);	
         LoanInterestRate.setColumns(10);
      }
   
      public void hidecustom(){
         combo.setVisible(true);
         custom.setSelected(false);
         normal.setSelected(true);
         LoanSelectLabel.setText("Select Loan");
         LoanSelectLabel.setVisible(false);
         LoanInterestLabel.setVisible(false);
         Duration.setVisible(false);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(false);	
         LoanInterestRate.setColumns(10);
      }
      public void clearAll(){
         MonthlyPayments.setText("");
         MonthlyPaymentLabel.setText("Monthly Payment $0.00"); 
         LoanAmountText.setText("$200000.00");
         Duration.setText("10");
         LoanInterestRate.setText("5.35");
         combo.setSelectedIndex(0);
         hidecustom();
      }
   
   }

Was This Post Helpful? 0
  • +
  • -

#8 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 07:35 AM

When I select a radio button, the state of the other does not change. When I click calculate there is no output in the JTextarea. It should display the doubles that have been converted to string.

This post has been edited by wizardcloak: 07 August 2011 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 08:26 AM

Duplicate threads merged again. Stop opening duplicate topics.
Was This Post Helpful? 1
  • +
  • -

#10 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 08:38 AM

Thank you for your reply macosxnerd101. I posted a new topic because my code and issues were different. I apologize for appearing surreptitious. The issue that you noted I corrected earlier. I am still getting Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 10"when the calculate button is clicked, however, I am getting the static string to appear, where before I got nothing. Thanks again.
   import java.awt.*;                                     
   import java.awt.event.*;  
   import javax.swing.*; 
   import java.io.*; 
   import java.text.*; 


   class AMORTIZATION extends JFrame
   {  
    
      
      int yearsArray[] = { 7, 15, 30 };		 
      double InterestArray[] = { 5.35, 5.5, 5.75 };
      public JLabel MonthlyPaymentLabel;
      public JTextField LoanAmountText;
      public JTextField LoanInterestRate;
      public JTextField Duration;
      public JComboBox combo;
      public JLabel LoanSelectLabel;
      public JLabel EnterDuration;
      public JLabel LoanInterestLabel;
      public JTextArea MonthlyPayments;
      public JRadioButton custom;
      public JRadioButton normal;
   	
      JPanel panel = new JPanel(); 
      JPanel Lpanel = new JPanel();
      JPanel Bpanel = new JPanel();
      JPanel Boxpanel = new JPanel();  
      JPanel Scrpanel = new JPanel();
      JPanel Radpanel = new JPanel(); 
      JPanel MPpanel = new JPanel(); 
   	   
      public AMORTIZATION (){
        
         setSize(555, 620); 
         setTitle("Mortgage Calculator");  
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setResizable(true);  
      
         LoanAmountText = new JTextField("200000.00");
         LoanInterestRate = new JTextField("       5.5");
        
       
         LoanSelectLabel = new JLabel("  Select Loan  ");
         LoanInterestLabel = new JLabel("                Enter Interest %  ");
         EnterDuration = new JLabel ("Enter Number of Years");
         Duration = new JTextField("             10");
         MonthlyPayments = new JTextArea(20, 40);
          
      
         custom = new JRadioButton ("Custom", false);
         normal = new JRadioButton ("Default", true);
      
                       
      
      
         MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
      
         JLabel LoanAmount = new JLabel("Loan Amount");
      
      
      
                  
         String loanOptions[] = {"30 years @ 5.75%","15 years @ 5.5%","7 years @ 5.35%"};
         combo = new JComboBox(loanOptions);
         Boxpanel.add(combo);                          
       
         normal.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){hidecustom();}});
      	
         custom.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){displaycustom();}});
        
          
         Lpanel.add(LoanAmount);
         Lpanel.add(LoanAmountText);
         Lpanel.add(Duration);
         Lpanel.add(EnterDuration);
      
         panel.add(LoanInterestLabel);
         panel.add(LoanInterestRate);
         panel.add(custom);
         panel.add(normal);
      
        
         Boxpanel.add(LoanSelectLabel);
       
      
      
         
        
      
      
      
         MPpanel.add(MonthlyPaymentLabel);
                        
        
      
        
      
         setLayout(new FlowLayout(FlowLayout.LEFT));
         getContentPane().add(panel); 
         getContentPane().add(Radpanel); 
         getContentPane().add(Lpanel);
         getContentPane().add(Bpanel);
         getContentPane().add(Boxpanel);
         getContentPane().add(MPpanel); 
         getContentPane().add(Scrpanel);
      
      
        
         JButton calculateButton = new JButton("Calculate");
      
         calculateButton.addActionListener(
               new ActionListener(){
                  @Override
                  public void actionPerformed(ActionEvent event){DetermineInt();}});
         JButton clearButton = new JButton("Clear");
      
         clearButton.addActionListener(
               new ActionListener(){
                  @Override
                  public void actionPerformed(ActionEvent event){clearAll();}});
                  
      				
         Bpanel.add(calculateButton);
         Bpanel.add(clearButton); 
         MonthlyPayments.setEditable(false);			
         JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);	
         Scrpanel.add (scrollingArea); 
       
      			
       
        
      }   
      public static void main (String []  args)throws IOException
      
      {   AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
         MyAMORTIZATION.setVisible (true); 
      
      }
      public void DetermineInt()
      {
         int currentNumber = 0;
         double initialAmount = Double.parseDouble(LoanAmountText.getText());
         double Irate = InterestArray[combo.getSelectedIndex()];
         DecimalFormat twoDigits = new DecimalFormat("0.00");
         if (custom.isSelected()){
            Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
            currentNumber = Integer.parseInt(Duration.getText());
         }
        
         int cn = currentNumber * 12;
         double ir = Irate/12;
         double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
         MonthlyPaymentLabel.setText("Monthly Payment $" + twoDigits.format(monthly_payment));
            
         int index;
         double start = initialAmount;
         double end = initialAmount;
         double MnthInt;
         double totInt = 0.0;
         String displayString = "";
         displayString += "#\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
      				
         for(index = 1; index <= currentNumber * 12; index++){
            double Temp = start;
            double tempInt = 0.0;
            MnthInt = 0.0;
            tempInt = (Temp * (1.00 + ir / 12)) - Temp;
         
            Temp = Temp - monthly_payment;
            Temp += tempInt;
            MnthInt += tempInt;
            end = Temp;
            totInt = totInt + tempInt;
            displayString += "#" + index + "\t" + twoDigits.format(start) + "\t" + twoDigits.format(end) + "\t" +twoDigits.format(MnthInt) + "\t" + twoDigits.format(totInt) + "\n";
            start = end;
         }
         MonthlyPayments.setText(displayString);	
         Duration.setVisible(false);	
         LoanInterestLabel.setVisible(false);	
         LoanInterestRate.setVisible(false);		
      }
      public void displaycustom(){
         combo.setVisible(false);
         custom.setSelected(true);
         normal.setSelected(false);
         LoanSelectLabel.setText("Enter Duration (years)");
         LoanSelectLabel.setVisible(true);
         LoanInterestLabel.setVisible(true);
         Duration.setVisible(true);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(true);	
         LoanInterestRate.setColumns(10);
      }
   
      public void hidecustom(){
         combo.setVisible(true);
         custom.setSelected(false);
         normal.setSelected(true);
         LoanSelectLabel.setText("Select Loan");
         LoanSelectLabel.setVisible(false);
         LoanInterestLabel.setVisible(false);
         Duration.setVisible(false);
         Duration.setColumns(15);
         LoanInterestRate.setVisible(false);	
         LoanInterestRate.setColumns(10);
      }
      public void clearAll(){
         MonthlyPayments.setText("");
         MonthlyPaymentLabel.setText("Monthly Payment $0.00"); 
         LoanAmountText.setText("$200000.00");
         Duration.setText("10");
         LoanInterestRate.setText("5.35");
         combo.setSelectedIndex(0);
         hidecustom();
      }
   
   }

This post has been edited by wizardcloak: 07 August 2011 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

#11 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 01:47 PM

My code is working perfectly except for one issue. I cannot get the interest right in the output. I am getting a real low number for interest initially and I know it should be much higher for the beginning of an amortization schedule. The monthly payment is correct and the GUI functions exactly as I need it to. Any suggestions would be appreciated.


   import java.awt.*;                                     
   import java.awt.event.*;  
   import javax.swing.*; 
   import java.io.*; 
   import java.text.*; 


   class AMORTIZATION extends JFrame
   {  
    
      
      private int yearsArray[] = { 7, 15, 30 };		 
      private double InterestArray[] = { 0.0535, 0.055, 0.0575 };
      public JLabel MonthlyPaymentLabel;
      public JTextField LoanAmountText;
      public JTextField LoanInterestRate;
      public JTextField Duration;
      public JComboBox combo;
      public JLabel LoanSelectLabel;
      public JLabel EnterDuration;
      public JLabel LoanInterestLabel;
      public JLabel LoanAmount;
      public JTextArea MonthlyPayments;
      public JRadioButton custom;
      public JRadioButton normal;
   	
      JPanel panel = new JPanel(); 
      JPanel Lpanel = new JPanel();
      JPanel Bpanel = new JPanel();
      JPanel Boxpanel = new JPanel();  
      JPanel Scrpanel = new JPanel();
      JPanel Radpanel = new JPanel(); 
      JPanel MPpanel = new JPanel(); 
   	   
      public AMORTIZATION (){
        
         setSize(755, 620); 
         setTitle("Mortgage Calculator");  
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
         setResizable(true);  
      
         LoanAmountText = new JTextField("200000");
         LoanInterestRate = new JTextField("5.75");
        
         MonthlyPaymentLabel = new JLabel("Monthly Payment $0.00");
         LoanSelectLabel = new JLabel("Select Loan");
         LoanInterestLabel = new JLabel("Enter Interest");
         Duration = new JTextField("30");
         MonthlyPayments = new JTextArea(20, 60);
         
         custom = new JRadioButton ("Custom", false);
         normal = new JRadioButton ("Default", true);
      
         LoanAmount = new JLabel("Loan Amount");
            
         String loanOptions[] = { "7 Years @ 5.35%", "15 Years @ 5.5%", "30 Years @ 5.75%" };
         combo = new JComboBox(loanOptions);
         Boxpanel.add(combo);                          
       
         normal.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){hidecustom();}});
      	
         custom.addActionListener(
               new ActionListener(){
                  public void actionPerformed(ActionEvent event){displaycustom();}});
       
         Lpanel.add(LoanAmount);
         Lpanel.add(LoanAmountText);
         Lpanel.add(Duration);
       
      
         panel.add(LoanInterestLabel);
         panel.add(LoanInterestRate);
         panel.add(custom);
         panel.add(normal);
      
         MPpanel.add(MonthlyPaymentLabel);
         MPpanel.add(LoanSelectLabel);
                     
      
         setLayout(new FlowLayout(FlowLayout.LEFT));
         getContentPane().add(panel); 
         getContentPane().add(Radpanel); 
         getContentPane().add(Lpanel);
         getContentPane().add(Bpanel);
         getContentPane().add(Boxpanel);
         getContentPane().add(MPpanel); 
         getContentPane().add(Scrpanel);
      
      
         JButton calculateButton = new JButton("Calculate");
      
         calculateButton.addActionListener(
               new ActionListener(){
                  @Override
                  public void actionPerformed(ActionEvent event){DetermineInt();}});
         JButton clearButton = new JButton("Clear");
      
         clearButton.addActionListener(
               new ActionListener(){
                  @Override
                  public void actionPerformed(ActionEvent event){clearAll();}});
                  
      	
         Duration.setVisible(false);
         LoanInterestRate.setVisible(false);
         LoanInterestLabel.setVisible(false);			
         Bpanel.add(calculateButton);
         Bpanel.add(clearButton); 
         MonthlyPayments.setEditable(false);			
         JScrollPane scrollingArea = new JScrollPane(MonthlyPayments);	
         Scrpanel.add (scrollingArea); 
      	  
      }   
    
      public void DetermineInt()
      {
         int currentNumber = 0;
         double initialAmount = Double.parseDouble(LoanAmountText.getText());
         double Irate = InterestArray[combo.getSelectedIndex()];
         DecimalFormat twoDigits = new DecimalFormat("0.00");
         currentNumber = yearsArray[combo.getSelectedIndex()];
      	
         if (custom.isSelected()){
            Irate = Double.parseDouble(LoanInterestRate.getText()) / 100.0;
            currentNumber = Integer.parseInt(Duration.getText());
         }
         double ir = Irate / 12;
         int cn = currentNumber * 12;
         double monthly_payment = initialAmount * ((ir * Math.pow((1 + ir), cn)) / (Math.pow((1 + ir), cn) - 1));
      
         MonthlyPaymentLabel.setText("Monthly Payment $" + twoDigits.format(monthly_payment));
            
         int index;
         double start = initialAmount;
         double end = initialAmount;
         double MnthInt;
         double totInt = 0.0;
         String displayString = "";
         displayString += "#\tLoan Balance\t\tEnding Balance\t\tInterest this month\tTotal Interest\n";
      				
         for(index = 1; index <= currentNumber * 12; index++){
            double Temp = start;
            double tempInt = 0.0;
            MnthInt = 0.0;
            tempInt = (Temp * (1.00 + ir / 12)) - Temp;
         
            Temp = Temp - monthly_payment;
            Temp += tempInt;
            MnthInt += tempInt;
            end = Temp;
            totInt = totInt + tempInt;
            displayString += "#" + index + "\t" + twoDigits.format(start) + "\t\t" + twoDigits.format(end) + "\t\t" +twoDigits.format(MnthInt) + "\t\t" + twoDigits.format(totInt) + "\n";
            start = end;
         }
      	
         MonthlyPayments.setText(displayString);	
        	
      }
      public void displaycustom(){
         combo.setVisible(false);
         custom.setSelected(true);
         normal.setSelected(false);
         LoanSelectLabel.setText("Enter number of years");
         LoanSelectLabel.setVisible(true);
         LoanInterestLabel.setVisible(true);
         Duration.setVisible(true);
         Duration.setColumns(30);
         LoanInterestRate.setVisible(true);	
         LoanInterestRate.setColumns(30);
      }
   
      public void hidecustom(){
         combo.setVisible(true);
         custom.setSelected(false);
         normal.setSelected(true);
         LoanSelectLabel.setText("Select Loan");
         LoanSelectLabel.setVisible(false);
         LoanInterestLabel.setVisible(false);
         Duration.setVisible(false);
         Duration.setColumns(30);
         LoanInterestRate.setVisible(false);	
         LoanInterestRate.setColumns(30);
      }
      public void clearAll(){
         MonthlyPayments.setText("");
         MonthlyPaymentLabel.setText("Monthly Payment" + "$0.00"); 
         LoanAmountText.setText("200000.00");
         Duration.setText("7");
         LoanInterestRate.setText("5.35");
         combo.setSelectedIndex(0);
         hidecustom();
      }
      public static void main (String []  args)throws IOException
      
      {   AMORTIZATION MyAMORTIZATION = new AMORTIZATION();
         MyAMORTIZATION.setVisible (true); 
      
      }
   }

Was This Post Helpful? 0
  • +
  • -

#12 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Getting a null pointer exception. What is my problem?

Posted 07 August 2011 - 03:14 PM

Thanks for the help D.I.C.. I reached my solution, with your assistance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1