Java people, please help!!!

Discussion in 'OT Technology' started by nietsni3, Nov 16, 2004.

  1. nietsni3

    nietsni3 Guest

    Code:
    import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       
     //write a GUI program that will accept three integer input values, say x, y, z and then verify whether or not x*x + y * y = z * z
       public class Pythagorean extends JFrame
       {
       	private JLabel xLabel;
       	private JLabel yLabel;
       	private JLabel zLabel;
       	private JLabel resultLabel;
       
       	private JTextField xTF;
       	private JTextField yTF;
       	private JTextField zTF;
       	private JTextField resultTF;
       
       	private JButton testB;
       	private JButton exitB;
       
       	private TestButtonHandler tbHandler;
       	private ExitButtonHandler ebHandler;
       
       	private static final int WIDTH = 500;
       	private static final int HEIGHT = 400;
       
       	public Pythagorean()
       	{
       		setTitle("Pythagorean Test");
       		Container c = getContentPane();
       		c.setLayout(new GridLayout(2,5));
       
       		xLabel = new JLabel("x = ", SwingConstants.RIGHT);
       		yLabel = new JLabel("y = ", SwingConstants.RIGHT);
       		zLabel = new JLabel("z = ", SwingConstants.RIGHT);
       		resultLabel = new JLabel("Result: ", SwingConstants.RIGHT);
       
       		xTF = new JTextField(10);
       		yTF = new JTextField(10);
       		zTF = new JTextField(10);
       		resultTF = new JTextField(50);
       
       		c.add(xLabel);
       		c.add(xTF);
       		c.add(yLabel);
       		c.add(yTF);
       		c.add(zLabel);
       		c.add(zTF);
       		c.add(resultLabel);
       		c.add(resultTF);
       		c.add(testB);
       		c.add(exitB);
       
       		testB = new JButton("Test");
       		tbHandler = new TestButtonHandler();
       		xTF.addActionListener(tbHandler);
       		yTF.addActionListener(tbHandler);
       		zTF.addActionListener(tbHandler);
       		resultTF.addActionListener(tbHandler);
       
       		exitB = new JButton("Exit");
       		ebHandler = new ExitButtonHandler();
       		exitB.addActionListener(ebHandler);
       
       		setSize(WIDTH, HEIGHT);
       		setVisible(true);
       		setDefaultCloseOperation(EXIT_ON_CLOSE);
       	}
       
       	private class TestButtonHandler implements ActionListener
       	{
       		public void actionPerformed(ActionEvent e)
       		{
       			int x, y, z, sumSqr, zSqr;
       			String result;
       
       			x = Integer.parseInt(xTF.getText());
       			y = Integer.parseInt(yTF.getText());
       			z = Integer.parseInt(zTF.getText());
       
       			sumSqr = x*x + y*y;
       			zSqr = z*z;
       
       			if (sumSqr == zSqr) result = "is";
       			else result = "is not";
       
     			resultTF.setText("(x*y + y*y) " + result + " equal z*z");
       
       		}
       	}
       
       	private class ExitButtonHandler implements ActionListener
       	{
       		public void actionPerformed(ActionEvent e)
       		{
       			System.exit(0);
       		}
       	}
       
       	public static void main ( String[] args)
       	{
       		Pythagorean pythagorean = new Pythagorean();
       	}
       }


    i wrote this program. it compiles with no problem. but whnenn i run it, it shows this error:
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:621)
    at java.awt.Container.add(Container.java:307)
    at Pythagorean.<init>(Pythagorean.java:56)
    at Pythagorean.main(Pythagorean.java:107)
    Press any key to continue . . .
     
  2. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    change this:
    Code:
       		c.add(xLabel);
       		c.add(xTF);
       		c.add(yLabel);
       		c.add(yTF);
       		c.add(zLabel);
       		c.add(zTF);
       		c.add(resultLabel);
       		c.add(resultTF);
       		c.add(testB);
       		c.add(exitB);
    
    
       		tbHandler = new TestButtonHandler();
       		xTF.addActionListener(tbHandler);
       		yTF.addActionListener(tbHandler);
       		zTF.addActionListener(tbHandler);
       		resultTF.addActionListener(tbHandler);
    
       		exitB = new JButton("Exit");
       		ebHandler = new ExitButtonHandler();
       		exitB.addActionListener(ebHandler);
    
    to this:
    Code:
       		testB = new JButton("Test");
       		exitB = new JButton("Exit");
       		c.add(xLabel);
       		c.add(xTF);
       		c.add(yLabel);
       		c.add(yTF);
       		c.add(zLabel);
       		c.add(zTF);
       		c.add(resultLabel);
       		c.add(resultTF);
       		c.add(testB);
       		c.add(exitB);
    
    
       		tbHandler = new TestButtonHandler();
       		xTF.addActionListener(tbHandler);
       		yTF.addActionListener(tbHandler);
       		zTF.addActionListener(tbHandler);
       		resultTF.addActionListener(tbHandler);
    
    
       		ebHandler = new ExitButtonHandler();
       		exitB.addActionListener(ebHandler);
    
    I dont think you have the GUI set up the way you want it to, but you can probably figure it out.
     
  3. nietsni3

    nietsni3 Guest

    yeah, changing the GridLayout(2,5) to GridLayout(5,2) fixes the GUI problem.
    but thanks for the help emlla
    but now the Test button is not working :(((
     
  4. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    thats because you never added an actionListener to the test button, just add the line testB.addActionListener(tbHandler); and it works.
     
  5. nietsni3

    nietsni3 Guest

    woooaa, it is workign now. me happy

    /me wanna buy emlla a drink :big grin:
     

Share This Page