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

Thread: need help with my java program

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Virginia.
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with my java program

    I'm writing a program to calculate the final yield of a person investing a initial amount of money over a set amount of time with a set interest rate. I get the following error when compiling the code:

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at calculator.<init>(calculator.java:62)
    at calculator.main(calculator.java:92)

    Here is the code for my java program:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class calculator extends JFrame{
    	private JButton calcButton, clearButton;
    	private JLabel originalDeposit, interestRate, timeInvested;
    	private JLabel resultDisplay;
    	private JTextField originalField, interestField, timeField;
    	private JPanel panel1, panel2, panel3,panel4,panel5;
    	double deposit,interest,time,newBalance;
     
    	public calculator(){
    		super("App to calculate the yield of an invesment");
    		setSize(400,400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new GridLayout(6,1));
    		originalDeposit = new JLabel("Enter amount of original deposit");
    		originalField = new JTextField(7);
    		interestRate = new JLabel("Enter the annual interest rate");
    		interestField = new JTextField(5);
    		timeInvested = new JLabel("Enter the years of invesment as a whole number");
    		calcButton = new JButton("Calculate");
    		clearButton = new JButton("Clear");
    		resultDisplay = new JLabel("The new balance in the investment account will be" + newBalance);
     
    		calcButton.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
     
    			String caption = e.getActionCommand();
    			if(e.getSource() instanceof JButton){
    				Object String;
    				if("Calculate".equals(caption))
    					getNewBalance();
    				resultDisplay.setText("The new balance in the investment account will be" + newBalance);
    				}
    			}
    		});
     
    		clearButton.addActionListener(new ActionListener(){
            	public void actionPerformed(ActionEvent e){
     
    			String caption = e.getActionCommand();
    			if(e.getSource() instanceof JButton){
    				if("Clear".equals(caption))
    					clearFields();
    				}
            	}
            });
     
     
     
    		panel1 = new JPanel();
    		panel1.add(originalDeposit);
    		panel1.add(originalField);
    		panel2 = new JPanel();
    		panel2.add(interestRate);
    		panel2.add(interestField);
    		panel3 = new JPanel();
    		panel3.add(timeInvested);
    		panel3.add(timeField);
    		panel4 = new JPanel();
    		panel4.add(calcButton);
    		panel4.add(clearButton);
    		panel5 = new JPanel();
    		panel5.add(resultDisplay);
    		add(panel1);
    		add(panel2);
    		add(panel3);
    		add(panel4);
    		add(panel5);
    	}
     
    	public double getNewBalance(){
    		deposit = Double.parseDouble(originalField.getText().trim());
    		interest = Double.parseDouble(interestField.getText().trim());
    		time = Double.parseDouble(timeField.getText().trim());
     
    		newBalance = deposit*(1+(interest/1));
     
    		return newBalance;
    	}
     
    	public void clearFields(){
    		originalField.setText(" ");
    		interestField.setText(" ");
    		timeField.setText(" ");
    	}
     
    	public static void main(String args[]){
    		calculator cal = new calculator();
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with my java program

    Something is being added to your user interface that is null. Make sure all your variables are initialized (hint: where is timeField constructed?)

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Virginia.
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with my java program

    I initialized the variables the following way:

    	private JButton calcButton, clearButton;
    	private JLabel originalDeposit, interestRate, timeInvested;
    	private JLabel resultDisplay;
    	private JTextField originalField, interestField, timeField;
    	private JPanel panel1, panel2, panel3,panel4,panel5;
    	double deposit=0.0,interest=0.0,time=0.0,newBalance=0.0;

    compared to how it was before:

    private JButton calcButton, clearButton;
    	private JLabel originalDeposit, interestRate, timeInvested;
    	private JLabel resultDisplay;
    	private JTextField originalField, interestField, timeField;
    	private JPanel panel1, panel2, panel3,panel4,panel5;
    	double deposit,interest,time,newBalance;

    I still get the following error message:


    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at calculator.<init>(calculator.java:62)
    at calculator.main(calculator.java:92)

  4. #4
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: need help with my java program

    Quote Originally Posted by omegared33 View Post
    I initialized the variables the following way:

    private JButton calcButton, clearButton;
    	private JLabel originalDeposit, interestRate, timeInvested;
    	private JLabel resultDisplay;
    	private JTextField originalField, interestField, timeField;
    	private JPanel panel1, panel2, panel3,panel4,panel5;
    	double deposit,interest,time,newBalance;

    I still get the following error message:


    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at calculator.<init>(calculator.java:62)
    at calculator.main(calculator.java:92)
    the error tells you where you have not initialized the variable Line 62 see below
    timeField = new JTextField();

  5. #5
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: need help with my java program

    don't forget to

    public static void main(String args[]){
    		calculator cal = new calculator();
            cal.setVisible(true);
    	}

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Location
    Virginia.
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with my java program

    I implemented the change in the main method; however I'm not sure how you are asking for line 62 to be edited.

    the original syntax is:

    panel3.add(timeField);

  7. #7
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: need help with my java program

    Quote Originally Posted by omegared33 View Post
    I implemented the change in the main method; however I'm not sure how you are asking for line 62 to be edited.

    the original syntax is:

    panel3.add(timeField);
    exactly, timefield is a variable needs to be initialized
     timeField = new JTextField()
    some where. Its not your trying to use a null JTextField

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with my java program

    Quote Originally Posted by omegared33 View Post
    I initialized the variables the following way:

    	private JButton calcButton, clearButton;
    	private JLabel originalDeposit, interestRate, timeInvested;
    	private JLabel resultDisplay;
    	private JTextField originalField, interestField, timeField;
    	private JPanel panel1, panel2, panel3,panel4,panel5;
    	double deposit=0.0,interest=0.0,time=0.0,newBalance=0.0;
    Perhaps you initialized your double variables, but what about your object variables? They must be instantiated prior to use (for example "myTextField = new JTextField()" ), and while many have been there is at least one that has not been.

  9. #9
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: need help with my java program

    all i did was add this in your code and ran it
    //your code
                    originalDeposit = new JLabel("Enter amount of original deposit");
    		originalField = new JTextField(7);// <----- here you did
    		interestRate = new JLabel("Enter the annual interest rate");
    		interestField = new JTextField(5); //<---- here you did
    		timeInvested = new JLabel("Enter the years of invesment as a whole number");
    		calcButton = new JButton("Calculate");
    		clearButton = new JButton("Clear");
    		resultDisplay = new JLabel("The new balance in the investment account will be" + newBalance);
     
            //added textField variable initialized
            timeField = new JTextField(10);  //<--- you missed this guy

    works fine for me.

Similar Threads

  1. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  2. My first Java Program
    By Creeeds in forum Java Theory & Questions
    Replies: 1
    Last Post: March 15th, 2011, 08:36 AM
  3. Help with this java program
    By Nemphiz in forum Object Oriented Programming
    Replies: 3
    Last Post: April 13th, 2010, 03:09 AM
  4. How do you add GUI to this java program?
    By leyla in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2009, 01:32 PM
  5. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM