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

Thread: How to solve this exception ????

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to solve this exception ????

    Hello every body I'm a new member in this very good website

    I have just started to write java calculator and i face this run time exception

    Exception in thread "main" java.lang.NullPointerException
    at Calculater.<init>(Calculater.java:34)
    at Calculater.main(Calculater.java:81)

    and here is my code
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
     
    public class Calculater extends JFrame
    {
    	public static final int WIDTH = 450;
    	public static final int HIGHT = 250;
     
    	public Calculater()
    	{
    		super("TextArea Calculater");
    		setSize(WIDTH,HIGHT);
    		setLocation(200, 200);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		setResizable(false);
    		setLayout(new BorderLayout(5, 5));
     
    		//Three Panel to orgenize the frame
    		//leftPanel for numbers and operations
    		JPanel leftPanel = new JPanel();
    		leftPanel.setLayout(new GridLayout(4, 4, 5, 5));
    		//rightPanel for the text area and the rest clear buttons
    		JPanel rightPanel = new JPanel();
    		rightPanel.setLayout(new BorderLayout(5, 5));
    		//textAreaPanel for textArea
    		JPanel textAreaPanel = new JPanel();
    		textAreaPanel.setLayout(new GridLayout(1, 1, 5, 5));
    		//clearsPanel for the all kind of clear buttons
    		JPanel clearsPanel = new JPanel();
    		clearsPanel.setLayout(new GridLayout(1, 3, 5, 5));
     
     
    		//decleare buttons and add them to leftPanel
    		JButton b1 = new JButton("7");
    		leftPanel.add(b1);
    		JButton b2 = new JButton("8");
    		leftPanel.add(b2);
    		JButton b3 = new JButton("9");
    		leftPanel.add(b3);
    		JButton b4 = new JButton("/");
    		leftPanel.add(b4);
    		JButton b5 = new JButton("4");
    		leftPanel.add(b5);
    		JButton b6 = new JButton("5");
    		leftPanel.add(b6);
    		JButton b7 = new JButton("6");
    		leftPanel.add(b7);
    		JButton b8 = new JButton("x");
    		leftPanel.add(b8);
    		JButton b9 = new JButton("1");
    		leftPanel.add(b9);
    		JButton b10 = new JButton("2");
    		leftPanel.add(b10);
    		JButton b11 = new JButton("3");
    		leftPanel.add(b11);
    		JButton b12 = new JButton("-");
    		leftPanel.add(b12);
    		JButton b13 = new JButton("0");
    		leftPanel.add(b13);
    		JButton b14 = new JButton(".");
    		leftPanel.add(b14);
    		JButton b15 = new JButton("=");
    		leftPanel.add(b15);
    		JButton b16 = new JButton("+");
    		leftPanel.add(b16);
     
    		//add leftPanel to the frame
    		add(leftPanel, BorderLayout.CENTER);
     
    		//decleare clears buttons and add them to clearPanel
    		JButton b17 = new JButton("C");
    		clearsPanel.add(b17);
    		JButton b18 = new JButton("CE");
    		clearsPanel.add(b18);
    		JButton b19 = new JButton("Clear");
    		clearsPanel.add(b19);
     
    		//add clearPanel to rightPanel
    		rightPanel.add(clearsPanel, BorderLayout.SOUTH);
     
    		//declear text area and add it to textAreaPanel
    		JTextArea myArea = new JTextArea();
    		textAreaPanel.add(myArea);
     
    		//add textAreaPanel to rightPanel
    		rightPanel.add(textAreaPanel, BorderLayout.CENTER);
     
    		//add rightPanel to the frame
    		add(rightPanel, BorderLayout.EAST);
     
     
     
    	}
    	public static void main(String[] args)
    	{
    		Calculater myCalc = new Calculater();
    	}
    }
    could any one help

    System.out.println("Thank you.");
    Last edited by newProgramer; March 27th, 2012 at 11:38 AM. Reason: use the highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,286
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: How to solve this exception ????

    When posting code, please use the highlight tags. Unformatted code is very hard to read.

    What line is that Exception occurring on? What are you trying to dereference? What is its value?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to solve this exception ????

    thank you KevinWorkman

    the exception in this line
    content.setLayout(new BorderLayout(5, 5));

    since i want to put content JPanel in to JFrame
    and i want to pot it in the right side of the fame this is the calculator i want to do
    Demo.jpg

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,286
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: How to solve this exception ????

    When posting code, please use the highlight tags. You can edit your original post to add them.

    If that's the line, then it looks like content must be null. When do you declare and instantiate it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to solve this exception ????

    what do you mean by please use the highlight tags. ????

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,286
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: How to solve this exception ????

    There's a link in my signature explaining it, or you could look at the many other posts here that use them. But the point is, when posting code, you should surround it with highlight tags to preserve formatting. Unformatted code makes programmers' eyes bleed.

    System.out.println("see how pretty this is?");
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Tjstretch (March 27th, 2012)

Similar Threads

  1. Please can anyone help solve this ?
    By jennyb in forum Java Theory & Questions
    Replies: 1
    Last Post: January 6th, 2012, 08:45 PM
  2. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  3. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  4. Solve Them Please
    By omath in forum Java Theory & Questions
    Replies: 1
    Last Post: December 25th, 2010, 03:26 PM
  5. solve it plz
    By tillu in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 12:45 PM