Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 969163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:36:06+00:00 2026-05-16T02:36:06+00:00

Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;

  • 0

Consider the following code

public class sqldetails extends JFrame implements ActionListener{

static String username=null;
static String password=null;
JButton button;
JTextField utext,ptext;

public static void main(String[] args){
new sqldetails();
}

sqldetails() {
    this.setTitle("Sql details");
    JPanel panel=new JPanel();
    panel.setLayout(new GridBagLayout());

    JLabel ulabel=new JLabel("Enter your Sql username");
    panel.add(ulabel);
    addItem(panel,ulabel,0,0,1,1,GridBagConstraints.WEST);

    JTextField utext=new JTextField(9);
    panel.add(utext);
    addItem(panel,utext,1,0,1,1,GridBagConstraints.WEST);

    JLabel plabel=new JLabel("Enter your Sql password");
    panel.add(plabel);
    addItem(panel,plabel,0,1,1,1,GridBagConstraints.WEST);

    JTextField ptext=new JTextField(9);
    panel.add(ptext);
    addItem(panel,ptext,1,1,1,1,GridBagConstraints.WEST);

    button =new JButton("enter");
    button.addActionListener(this);
    panel.add(button);
    addItem(panel,button,0,2,1,1,GridBagConstraints.CENTER);
    this.add(panel);
    this.pack();
    this.setVisible(true);
}

private void addItem(JPanel panel, JComponent c, int i, int j,
        int k, int l, int align) {
    GridBagConstraints calc=new GridBagConstraints();
    calc.gridx=i;
    calc.gridy=j;
    calc.gridwidth=k;
    calc.gridheight=l;
    calc.weightx=100.0;
    calc.weighty=100.0;
    calc.insets=new Insets(5,5,5,5);
    calc.anchor=align;
    calc.fill=GridBagConstraints.NONE;
    panel.add(c,calc);      

}


public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource()==button){
        username +=utext.getText();
        utext.setText(null);
        password +=ptext.getText();
        ptext.setText(null);
        new sqlconnection();
        new gui();
        new first();
    }
}

However wen i compile it, i get java.lang.NullPointerException in the gettext() line.
Help would be appreciated 🙂

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:36:07+00:00Added an answer on May 16, 2026 at 2:36 am

    Thats because variables utext and ptext inside constructor sqldetails() shadow the instance variables of the same name

    sqldetails() {
        .....
    
        // JTextField utext=new JTextField(9); //====> shadows the instance variable utext
        this.utext=new JTextField(9); // This is the correct use
        panel.add(utext);
        addItem(panel,utext,1,0,1,1,GridBagConstraints.WEST);
    
        .......
    
        // JTextField ptext=new JTextField(9); //=====> shadows the instance variable ptext
        this.ptext = new JtextField();  // This is the correct use
    
        .......
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Consider the following code: public partial class TeacherControlPanel : System.Web.UI.Page { protected string username
Consider the following code : public class Main implements Vehicle, Car { public static
Please consider the following code: public class MyClass extends javax.swing.JFrame { private JTree jTree;
Consider the following code : public class LIMSGrid extends ClientEventSource implements Focusable, FramingBlockWrapper {
Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public
Consider the following code: [Serializable] public class Human { public string Name { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.