hey i have written this code to make a GUI but it compile correctly but gives error at run time.
the error is
exception in thread main "java.lang.NullpointerException"
at GUI.intGUI(GUI.java:22)
at GUI.(init)(GUI.java :75)
at GUI.main (GUI.java:79)

the code is

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GUI
{
 JFrame myFrame;
JTextField tf;
JRadioButton Male,Female;
JLabel label1;
JLabel label2;
JLabel label3;
JPanel panel1;
JPanel panel2;
JPanel panel3;


public void initGUI()
{
myFrame= new JFrame();

panel1.setLayout(new GridLayout(1,2));
panel1.add(label1);
panel1.add(label2);

panel2.setLayout(new GridLayout(1,2));
panel2.add(label3);
panel2.add(tf);



JPanel panel3 = new JPanel();
  ButtonGroup buttonGroup = new ButtonGroup();
  Male = new JRadioButton("Male");
  buttonGroup.add(Male);
  panel3.add(Male);
  Female = new JRadioButton("Female");
  buttonGroup.add(Female);
  panel3.add(Female);
  Male.setSelected(true);


myFrame.add(panel1);
myFrame.add(panel2);
myFrame.add(panel3);


int initValue =60;
int minimum = 40;
int maximum = 80;

JSlider slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setSnapToTicks(true);
slider.setLabelTable(slider.createStandardLabels(10));

 Container c=myFrame.getContentPane();
c.setLayout(new BorderLayout());
c.add(panel1,"NORTH");
c.add(slider,"CENTER");
c.add(panel2,"South");
c.add(panel3,"West");


myFrame.setDefaultCloseOperation(3);
myFrame.setSize(200,100);
myFrame.setVisible(true);
}

public GUI()
{
initGUI();
}
public static void main(String arg[])
{
GUI g=new GUI();
}
}

Missing initialization of panel1 and later panel2 will kick same error once first panel fixed. They both should be declared something along the line of panel3 on line 32 in posted code

i added this code add 21 line to initialize panel 1 and panel 2

JPanel panel1= new JPanel();
panel1.setLayout(new GridLayout(1,2));
panel1.add(label1);
panel1.add(label2);

JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,2));
panel2.add(label3);
panel2.add(tf);

but now there are two more errors

at java.awt.Container.addImpl(container.java:1045)
at java.awt.Container.add(container.java:365)

Not sure what IDE you are using, but my is showing errors similar to issue with panels. Your labels and textfield need same treatment as panels :)

m a beginer so m making programs in notepad then saving them using .java extension n compiling and running them in command prompt

m a beginer so m making programs in notepad then saving them using .java extension n compiling and running them in command prompt

Good effort (I do not see often people to learn this way, I did it too), however you may want to try simple IDEs like JCreator for example. It does basic code colour formatting, but nothing pass that no autosuggestions or autocorrection when you mistype something or if something else wrong with code

still same problem is there

Please repost whole code as you have it now, so I can see it

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GUI
{
 JFrame myFrame;
JTextField tf;
JRadioButton Male,Female;
JLabel label1;
JLabel label2;
JLabel label3;
JPanel panel1;
JPanel panel2;
JPanel panel3;


public void initGUI()
{
myFrame= new JFrame();

JPanel panel1= new JPanel();
panel1.setLayout(new GridLayout(1,2));
panel1.add(label1);
panel1.add(label2);

JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,2));
panel2.add(label3);
panel2.add(tf);



JPanel panel3 = new JPanel();
  ButtonGroup buttonGroup = new ButtonGroup();
  Male = new JRadioButton("Male");
  buttonGroup.add(Male);
  panel3.add(Male);
  Female = new JRadioButton("Female");
  buttonGroup.add(Female);
  panel3.add(Female);
  Male.setSelected(true);


myFrame.add(panel1);
myFrame.add(panel2);
myFrame.add(panel3);


int initValue =60;
int minimum = 40;
int maximum = 80;

JSlider slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setSnapToTicks(true);
slider.setLabelTable(slider.createStandardLabels(10));

JTextField tf= new JTextField();

 Container c=myFrame.getContentPane();
c.setLayout(new BorderLayout());
c.add(panel1,"NORTH");
c.add(slider,"CENTER");
c.add(panel2,"South");
c.add(panel3,"West");


myFrame.setDefaultCloseOperation(3);
myFrame.setSize(200,100);
myFrame.setVisible(true);
}

public GUI()
{
initGUI();
}
public static void main(String arg[])
{
GUI g=new GUI();
}
}

Don't know why you getting that error, sorry. However I did some corrections to your code see bellow. If you have any problems just post your issue. So far you doing well, just hiccups anyone would encounter :)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GUI {
    JFrame myFrame;
    JTextField tf = new JTextField();
    JRadioButton Male, Female;
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    JLabel label3 = new JLabel();
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;


    public void initGUI() {
        myFrame = new JFrame();

        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1, 2));
        panel1.add(label1);
        panel1.add(label2);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1, 2));
        panel2.add(label3);
        panel2.add(tf);


        JPanel panel3 = new JPanel();
        ButtonGroup buttonGroup = new ButtonGroup();
        Male = new JRadioButton("Male");
        buttonGroup.add(Male);
        panel3.add(Male);
        Female = new JRadioButton("Female");
        buttonGroup.add(Female);
        panel3.add(Female);
        Male.setSelected(true);


        myFrame.add(panel1);
        myFrame.add(panel2);
        myFrame.add(panel3);


        int initValue = 60;
        int minimum = 40;
        int maximum = 80;

        JSlider slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
        slider.setMinorTickSpacing(1);
        slider.setMajorTickSpacing(10);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.setSnapToTicks(true);
        slider.setLabelTable(slider.createStandardLabels(10));

        Container c = myFrame.getContentPane();
        c.setLayout(new BorderLayout());
        c.add(panel1, BorderLayout.NORTH);
        c.add(slider, BorderLayout.CENTER);
        c.add(panel2, BorderLayout.SOUTH);
        c.add(panel3, BorderLayout.WEST);


        myFrame.setDefaultCloseOperation(3);
        myFrame.setSize(200, 100);
        myFrame.setVisible(true);
    }

    public GUI() {
        initGUI();
    }

    public static void main(String arg[]) {
        GUI g = new GUI();
    }
}

yeah now its working :-) thanx alot. now i want to add functionality in it can u guide me

You did not mention what functionality...

m trying to make a function that when i move the slider the value of ideal weight is calculated in text box according to wether its man or woman

  • attach listener to slider
  • listener call on some calculate method
  • method will
    • gather data from relevant components
    • does the calculation
    • updates relevant components

Try to do it your self and if you have problems post with your code and error/issue you have

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.