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 608105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:25:43+00:00 2026-05-13T17:25:43+00:00

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; public class

  • 0
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

public class MainForm extends JFrame{

    private JPanel p;
    private JButton clear;
    private JLabel nameLabel;
    private JTextField nameText;
    private JLabel genderLabel;
    private ButtonGroup genderButtonGroup;
    private JTextField courseText;

    public MainForm() {
        super("Some application");
        p = new JPanel();
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel nameLabel = new JLabel("Student Name");
        c.gridx=0;
        c.gridy=0;
        c.gridwidth=1;
        c.gridheight=1;
        c.weightx=0.0;
        c.weighty=0.0;
        c.fill = GridBagConstraints.VERTICAL;
        c.insets= new Insets(4,4,4,4);
        this.getContentPane().add(nameLabel,c);

        JTextField nameText = new JTextField(20);
        c.gridx=1;
        c.gridy=0;
        c.gridwidth=1;
        c.gridheight=1;
        c.weightx=0.0;
        c.weighty=0.0;
        c.fill = GridBagConstraints.VERTICAL;
        c.insets= new Insets(4,4,4,4);
        this.getContentPane().add(nameText,c);
        nameText.setText("fsdf"); //works fine

        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clearMainForm(); } });
        c.gridx=0;
        c.gridy=8;
        c.gridwidth=1;
        c.gridheight=1;
        c.weightx=0.0;
        c.weighty=0.0;
        c.fill = GridBagConstraints.VERTICAL;
        c.insets= new Insets(4,4,4,4);
        this.getContentPane().add(clearButton,c);

    }



    public void clearMainForm() {
        System.out.println("clearing");
        nameText.setText(""); // causes exception
    }



}

changing nameText just after it is created works fine, but trying it in clearMainFOrm, after the clear button is pressed causes an exception.

  • 1 1 Answer
  • 0 Views
  • 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-13T17:25:43+00:00Added an answer on May 13, 2026 at 5:25 pm

    1) It helps to actually say what the exception is.

    2) This line:

    JTextField nameText = new JTextField(20);
    

    sets a local variable, not the class variable. Change it to:

    nameText = new JTextField(20);
    

    and it’ll work.

    3) You’re not setting any of your class variables. You’re very shortly going to have more problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class displayFullScreen extends JFrame { private JLabel
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame { private JFrame
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Sheet extends JFrame{ private String[] line
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Thread; public class Lthread extends JPanel{ private
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class File { private JFrame frame1; private
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class GridBagLayoutDemo extends JFrame {
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Stuff extends Applet implements
import java.awt.event.*; import javax.swing.*; public class PlannerMain { JFrame frame; JButton makeMap; JPanel panel;
import java.io.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.text.DecimalFormat; public class Final extends

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.