Results 1 to 3 of 3
  1. #1
    himanshi25 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    hey everyone... i m new to java.. and i am working on Jframe builder. i have encountered some errors in the code given below:
    /************************************************** **************/
    /* checknumber */
    /* */
    /************************************************** **************/
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /**
    * Summary description for checknumber
    *
    */
    public class checknumber extends JFrame
    {
    // Variables declaration
    private JLabel jLabel1;
    private JTextField jTextField1;
    private JButton jButton1;
    private JButton jButton2;
    private JPanel contentPane;
    double i;
    // End of variables declaration


    public checknumber()
    {
    super();
    initializeComponent();
    //
    // TODO: Add any constructor code after initializeComponent call
    //

    this.setVisible(true);
    }

    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always regenerated
    * by the Windows Form Designer. Otherwise, retrieving design might not work properly.
    * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
    * to retrieve your design properly in future, before revising this method.
    */
    private void initializeComponent()
    {
    jLabel1 = new JLabel();
    jTextField1 = new JTextField();
    jButton1 = new JButton();
    contentPane = (JPanel)this.getContentPane();

    //
    // jLabel1
    //
    //
    // jTextField1
    //
    jTextField1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    jTextField1_actionPerformed(e);
    }

    });
    //
    // jButton1
    //
    jButton1.setText("check");
    jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    jButton1_actionPerformed(e);
    }

    });
    jButton2.setText("redo");
    jButton2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    jButton2_actionPerformed(e);
    }

    });
    //
    // contentPane
    //
    contentPane.setLayout(null);
    addComponent(contentPane, jLabel1, 139,306,193,37);
    addComponent(contentPane, jTextField1, 163,78,138,22);
    addComponent(contentPane, jButton1, 189,198,83,28);
    addComponent(contentPane, jButton2, 250,200,83,28);
    //
    // checknumber
    //
    this.setTitle("checknumber - extends JFrame");
    this.setLocation(new Point(0, 0));
    this.setSize(new Dimension(473, 544));
    }

    /** Add Component Without a Layout Manager (Absolute Positioning) */
    private void addComponent(Container container,Component c,int x,int y,int width,int height)
    {
    c.setBounds(x,y,width,height);
    container.add(c);
    }

    //
    // TODO: Add any appropriate code in the following Event Handling Methods
    //
    private void jTextField1_actionPerformed(ActionEvent e)
    {
    System.out.println("\njTextField1_actionPerformed( ActionEvent e) called.");
    // TODO: Add any handling code here

    }

    private void jButton1_actionPerformed(ActionEvent e)
    { i= Double.parseDouble(jTextField1.getText());
    /* if (i<0)
    { jLabel1.setText("Number is a negative integer");
    }*/
    System.out.println("\njButton1_actionPerformed(Act ionEvent e) called.");
    // TODO: Add any handling code here

    }

    private void jButton2_actionPerformed(ActionEvent e)
    { jTextField1.setText(" ");
    System.out.println("\njButton1_actionPerformed(Act ionEvent e) called.");
    }
    //

    // TODO: Add any method code to meet your needs in the following area
    //
































    //============================= Testing ================================//
    //= =//
    //= The following main method is just for testing this class you built.=//
    //= After testing,you may simply delete it. =//
    //================================================== ====================//
    public static void main(String[] args)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    try
    {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsLookAndFeel");
    }
    catch (Exception ex)
    {
    System.out.println("Failed loading L&F: ");
    System.out.println(ex);
    }
    new checknumber();
    }
    //= End of Testing =


    }

    and the errors are :
    init:
    deps-jar:
    compile-single:
    run-single:
    Exception in thread "main" java.lang.NullPointerException
    at checknumber.initializeComponent(checknumber.java:7 3)
    at checknumber.<init>(checknumber.java:27)
    at checknumber.main(checknumber.java:182)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    please help me out with this..:(

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    I see that you initialized jButton1 (jButton1 = new JButton(); ), but what about jButton2 ?

  3. #3
    himanshi25 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  2. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By farooqhussain786 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 07:31 AM
  4. Replies: 1
    Last Post: 02-17-2009, 11:54 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •