Results 1 to 11 of 11

Thread: check this out

  1. #1
    RajeshReddy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default check this out

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at SwingTutorialPackage.SamplePanel.createEntryFields (SamplePanel.java:124)
    at SwingTutorialPackage.SamplePanel.<init>(SamplePane l.java:27)
    at SwingTutorialPackage.SamplePanel.createAndShowGUI( SamplePanel.java:150)
    at SwingTutorialPackage.SamplePanel.access$0(SamplePa nel.java:144)
    at SwingTutorialPackage.SamplePanel$2.run(SamplePanel .java:163)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    getting this error help me out
    below is the code for the generated error


    Java Code:
    package SwingTutorialPackage;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class SamplePanel extends JPanel implements ActionListener,FocusListener
    
    {
    JTextField Name,JDBCDriver,JDBCURL,ServerAddress,Database,Use rname,Password;
    Font regularFont, italicFont;
    final static int GAP = 1;
    
    public SamplePanel() {
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    JPanel testpanel = new JPanel(){
    //Don't allow us to stretch vertically.
    public Dimension getMaximumSize() {
    Dimension pref = getPreferredSize();
    return new Dimension(Integer.MAX_VALUE,
    pref.height);
    }
    };;
    testpanel.setLayout(new BoxLayout(testpanel,
    BoxLayout.PAGE_AXIS));
    testpanel.add(createEntryFields());
    testpanel.add(createButtons());}
    
    protected JComponent createButtons() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    
    JButton button = new JButton("Wizard");
    button.addActionListener(this);
    button.setActionCommand("wizard");
    panel.add(button);
    
    button = new JButton("Test");
    button.addActionListener(this);
    button.setActionCommand("Test");
    panel.add(button);
    
    button = new JButton("Save");
    button.addActionListener(this);
    button.setActionCommand("Save");
    panel.add(button);
    
    button = new JButton("Cancel");
    button.addActionListener(this);
    button.setActionCommand("Cancel");
    panel.add(button);
    
    panel.setBorder(BorderFactory.createEmptyBorder(0, 0,
    GAP-5, GAP-5));
    return panel;
    }
    
    public void actionPerformed(ActionEvent e) {
    if ("wizard".equals(e.getActionCommand()))
    {
    Name.setText("");
    }else {
    JDBCDriver.setText("");
    }
    }
    
    
    
    public void focusGained(FocusEvent e) {
    Component c = e.getComponent();
    if(c instanceof JTextField) {
    ((JTextField)c).selectAll();
    }
    }
    //Needed for FocusListener interface.
    public void focusLost(FocusEvent e) { } //ignore
    
    
    protected JComponent createEntryFields() {
    JPanel panel = new JPanel(new SpringLayout());
    
    String[] labelStrings = {
    "Name: ",
    "JDBC Driver: ",
    "JDBC URL: ",
    "Server Address: ",
    "Database:",
    "Username:",
    "Password:",
    
    };
    JLabel[] labels = new JLabel[labelStrings.length];
    JComponent[] fields = new JComponent[labelStrings.length];
    int fieldNum = 0;
    
    //Create the text field and set it up.
    Name = new JTextField();
    Name.setColumns(20);
    fields[fieldNum++] = Name;
    
    JDBCDriver = new JTextField();
    JDBCDriver.setColumns(20);
    fields[fieldNum++] = JDBCDriver;
    
    JDBCURL = new JTextField();
    JDBCURL.setColumns(20);
    fields[fieldNum++] = JDBCURL;
    
    ServerAddress = new JTextField();
    ServerAddress.setColumns(20);
    fields[fieldNum++] = ServerAddress;
    
    Database = new JTextField();
    Database.setColumns(20);
    fields[fieldNum++] = Database;
    
    //Associate label/field pairs, add everything,
    //and lay it out.
    for (int i = 0; i < labelStrings.length; i++) {
    labels[i] = new JLabel(labelStrings[i],
    JLabel.TRAILING);
    labels[i].setLabelFor(fields[i]);
    panel.add(labels[i]);
    panel.add(fields[i]);
    
    //Add listeners to each field.
    JTextField tf = null;
    tf=(JTextField)fields[i];
    tf.addActionListener(this);
    tf.addFocusListener(this);
    }
    SpringUtilities.makeCompactGrid(panel,
    labelStrings.length, 2,
    GAP, GAP, //init x,y
    GAP, GAP/2);//xpad, ypad
    return panel;
    }
    
    /**
    * Create the GUI and show it. For thread safety,
    * this method should be invoked from the
    * event dispatch thread.
    */
    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("WelcomePage");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    
    //Add contents to the window.
    frame.add(new SamplePanel());
    
    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }
    public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    //Turn off metal's use of bold fonts
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    createAndShowGUI();
    }
    });}
    
    
    
    
    }
    Last edited by RajeshReddy; 07-22-2011 at 07:10 AM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Where is line 124, and please use code tags when posting code.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    Is there a reason you've posted this question three times, each with a different uninformative title?

    Please see the link in my signature on asking smart questions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    You should test that fieldNum == labelStrings.length

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    @OP: I closed your other two identical threads.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    RajeshReddy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    now please check out

  7. #7
    RajeshReddy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    can u be clear,there's no such fieldNum == labelStrings.length

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    I checked it out and wasn't impressed. Next!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What is the value of fieldNum?
    How many elements are there in the labelStrings array?

  10. #10
    RajeshReddy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What is the value of fieldNum?
    How many elements are there in the labelStrings array?
    its frst initialised to 0,array size is 7

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    And how many fields do you initialize?

    Honestly, if you can't see where you have messed up, you have no business dealing with GUIs, let alone JDBC. Go back to something simpler until you understand arrays.

    db

Similar Threads

  1. Can someone check this over?
    By Phyxed in forum New To Java
    Replies: 2
    Last Post: 03-29-2011, 09:07 AM
  2. Don't know what to check for
    By Chasingxsuns in forum New To Java
    Replies: 8
    Last Post: 08-26-2009, 05:59 AM
  3. pls check it
    By aRTx in forum New To Java
    Replies: 0
    Last Post: 03-24-2009, 02:35 PM
  4. Check for null int
    By SnarfSnarf in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 11:12 PM
  5. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 02:37 PM

Posting Permissions

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