well guys, i wanted to try somethng fun in java, so i made a simple form type navigation program.. i can compile this file, bt i cant run it..NOTE: i have no issues in my java software..

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

public class Nav2 extends JFrame implements ActionListener
{
JTextField t1,t2,t3,t4;//Source,Destination,Day or night time 

JLabel l1,l2,l3,l4,l5,sl,dl,l6;//Enter source,Enter Destination,When do u want to travel,Mode of transport

JRadioButton rb1,rb2,rb3,rb4,rb5,rb6;//Train,Bus,Auto,Taxi 

JButton b1,b2,b3;//Time or Money
JPanel p1,p2,p3,panel;
ButtonGroup rbg,rbng;


    public Nav2()
    {
        Container con= getContentPane();
        con.setLayout(new FlowLayout());

        l1=new JLabel("Enter Source:");
        l2=new JLabel("Enter Destination:");
        l3=new JLabel("When do you like to travel:");
        l6=new JLabel("Mode of Transport:");
        sl=new JLabel("Invalid Entry!! Enter proper source (eg: Khar station)");
        dl=new JLabel("Invalid Entry!! Enter proper destination (eg: Bandra station)");
        p1=new JPanel();


        t1=new JTextField(30);
        t2=new JTextField(30);
        t3=new JTextField(30);

        rbg=new ButtonGroup();
        rb1=new JRadioButton("Day",false);
        rb2=new JRadioButton("Night",false);

        rbng=new ButtonGroup();
        rb3=new JRadioButton("Train",false);
        rb4=new JRadioButton("Bus",false);
        rb5=new JRadioButton("Auto",false);
        rb6=new JRadioButton("Taxi",false);

        b1=new JButton("Time");
        b2=new JButton("Money");
        b3=new JButton("Clear");

        con.add(l1);
        con.add(t1);
        con.add(sl);

        con.add(l2);
        con.add(t2);
        con.add(dl);

        con.add(l3);
        rbg.add(rb1);
        rbg.add(rb2);

        con.add(rb1);
        con.add(rb2);


        p1.add(l4);
        p1.setVisible(false);
        con.add(p1);


        rbng.add(rb3);
        rbng.add(rb4);
        rbng.add(rb5);
        rbng.add(rb6);

        con.add(l6);
        con.add(rb3);
        con.add(rb4);
        con.add(rb5);
        con.add(rb6);

        con.add(b1);
        con.add(b2);
        con.add(b3);

        rb1.addActionListener(this);
        rb2.addActionListener(this);
        rb3.addActionListener(this);
        rb4.addActionListener(this);
        rb5.addActionListener(this);
        rb6.addActionListener(this);
        b3.addActionListener(this);

        sl.setVisible(false);
        dl.setVisible(false);

        t1.addKeyListener(new KeyAdapter()
                    {
                        public void keyTyped(KeyEvent e)
                        {
                            String input=t1.getText();
                            Pattern p = Pattern.compile("[0-9,&%$#@!()*^`]");
                            Matcher m = p.matcher(input);
                            if (m.find())
                            {
                                sl.setVisible(true);
                                sl.setForeground(Color.RED);
                            }
                            else
                            {
                                sl.setVisible(false);

                            }
                        }
                    });


                    setVisible(true);
        t2.addKeyListener(new KeyAdapter()
                    {
                        public void keyTyped(KeyEvent e)
                        {
                            String input=t2.getText();
                            Pattern p = Pattern.compile("[0-9,&%$#@!()*^`]");
                            Matcher m = p.matcher(input);
                            if (m.find())
                            {
                                dl.setVisible(true);
                                dl.setForeground(Color.RED);
                            }
                            else
                            {
                                dl.setVisible(false);
                            }
                        }
                    });
                    setVisible(true);
    }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==rb1)
            {
            rb1.setText("Day");
            }
                if(ae.getSource()==rb2)
                {
                rb2.setText("Night");
                }
                    if(ae.getSource()==rb3)
                    {
                    rb3.setText("Train");
                    }
                        if(ae.getSource()==rb4)
                        {
                        rb4.setText("Bus");
                        }
                            if(ae.getSource()==rb5)
                            {
                            rb5.setText("Auto");
                            }
                                if(ae.getSource()==rb6)
                                {
                                rb6.setText("Taxi");
                                }
            if(ae.getSource()==b3)
            {
            t1.setText("");
            t2.setText("");             
            sl.setVisible(false);
            dl.setVisible(false);

            rbg.clearSelection();
            rbng.clearSelection();
            }
        }

            public static void main(String args[])
            {
                Nav2 en= new Nav2();
                en.setVisible(true);
                en.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                en.setSize(345,250);
            }

    }

Recommended Answers

All 6 Replies

What do you mean by "can't run it"? - YOu don't know how to, you try to run it but nothing happens, you run it and you get an error message (if so- -s aying what?)... ?

what i meant is tht; when i compile this file using javac Nav2.java the class file is formed bt when i run it by java Nav2, not running..

I just tried your code and it runs, but throws an exception before it can display anything.
What exactly do you mean by "not running"? Does your command prompt hang? Do you get an error message? Is the command ignored and you just get the command prompt again???

i cant see my form.. i can c this.. bt i cant understand it

OK - you should have posted that with your very first post - it explains everything.
Here's what it means...

Exception... something went wrong
... null pointer ... you tried to use an uninitialised variable
... at Nav2.java:67 ... on line 67 of your program

Line 67 is: p1.add(l4);, so either p1 or l4 has not been initialised. Check your code to see which one you didn't initialise.

Yes, Yes, Yes it worked..!! Thank u vry muchhhh.. all i need to do was this: p1.add(l6);.. my previous code was p1.add(l4);.. Thank u vry much

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.