Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: please correct my programme on swings

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face please correct my programme on swings

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.border.*;
    class Mypanel extends JPanel
    {
    Mypanel()
    {
    this.setBackground(Color.pink);
    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.setColor(Color.black);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("click the buttons and get your favourite border ",100,20);
    }
    }
    class JBut extends JFrame
    {
    JButton b1,b2,b3,b4,b5,b6;
    JBut()
    {
    Container c=getContentPane();
    Mypanel p=new Mypanel();
    c.add(p);
    c.setLayout(new GridLayout());
    b1=new JButton("Raised Bevel Border");
    b1=new JButton("Lowered Bevel Border");
    b1=new JButton("Raised Etch Border");
    b1=new JButton("Raised Etch Border");
    b1=new JButton("Line Border");
    b1=new JButton("Empty Border");
    Border bd=BorderFactory.createBevelBorder(BevelBorder.RAI SED,Color.red,Color.green);
    b1.setBorder(bd);
    bd=BorderFactory.createBevelBorder(BevelBorder.LOW ERED,Color.red,Color.green);
    b2.setBorder(bd);
    bd=BorderFactory.createBevelBorder(EtchedBorder.RA ISED,Color.red,Color.green);
    b3.setBorder(bd);
    bd=BorderFactory.createBevelBorder(EtchedBorder.RA ISED,Color.red,Color.green);
    b4.setBorder(bd);
    bd=BorderFactory.createLineBorder(Color.red,5);
    b5.setBorder(bd);
    bd=BorderFactory.createEmptyBorder(10,10,10,10);
    b6.setBorder(bd);
    c.add(b1);
    c.add(b2);
    c.add(b3);
    c.add(b4);
    c.add(b5);
    c.add(b6);
    }
    public static void main(String[] args)throws NullPointerException
    {
    JBut j=new JBut();
    j.setSize(600,600);
    j.setTitle("border choosing frame");
    j.setVisible(true);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
    ################################################## #########################################
    this is the run time error i am getting during running....... please help me out guyss!!!!!!
    Exception in thread "main" java.lang.NullPointerException
    at JBut.<init>(JBut.java:36)
    at JBut.main(JBut.java:54)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: please correct my programme on swings

    Look at line 36. Something is null there. Use print statements (or better yet, a debugger) to figure out what is null.

    Hint: Do you mean to set the same variable (b1) 6 different times?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please correct my programme on swings

    oh god what a silly mistake that was sory to waste your valuable time buddy...but thx yet ... i have another programme i will post it too..

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please correct my programme on swings

    import java.awt.*;
    import java.awt.event.*;
    class Allawt extends Frame implements ActionListener,ItemListener,AdjustmentListener
    {
    /**
    *
    */
    //private static final long serialVersionUID = 1L;
    String msg;
    Button b;
    Checkbox ch;
    CheckboxGroup cbg;
    Checkbox rb;
    Choice c;
    List lst;
    Scrollbar sc;
    TextField name;
    TextField password;
    Allawt()
    {
    try{
    //defining layout
    setLayout(new BorderLayout());

    //initializing components
    b=new Button();
    ch=new Checkbox("vja");
    c=new Choice();
    lst=new List(1,true);
    cbg=new CheckboxGroup();
    rb=new Checkbox("male",cbg,true);
    sc=new Scrollbar(Scrollbar.VERTICAL,0,30,0,500);
    Label n=new Label("Name:",Label.LEFT);
    Label p=new Label("Password:",Label.LEFT);
    //adding components
    add(b);
    add(ch);
    add(c);
    add(lst);
    add(rb);
    b.setBounds(10,20,30,30);
    b.setLabel("push me");
    ch.setLabel("vja");
    c.add("ferrari");
    c.add("bmw");
    lst.add("linux");
    lst.add("windows 7");
    add(sc);
    add(n);
    add(name);
    add(p);
    add(password);
    sc.setBounds(0,0,15,500);
    n.setBackground(Color.yellow);
    n.setForeground(Color.blue);
    password.setEchoChar('-');
    //assigning action listener to components

    b.addActionListener(this);
    ch.addItemListener(this);
    c.addItemListener(this);
    lst.addItemListener(this);
    rb.addItemListener(this);
    sc.addAdjustmentListener(this);
    name.addActionListener(this);
    password.addActionListener(this);
    //closing window function

    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    public void actionPerformed(ActionEvent ae)
    {
    System.out.println("button is pressed");
    }
    public void itemStateChanged(ItemEvent ie)
    {
    repaint();
    }
    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
    repaint();
    }

    public void paint(Graphics g)
    {
    msg=b.getActionCommand();
    g.drawString("button pressed::"+msg,100,20);
    g.drawString("checkbox status:"+ch.getState(),100,40);
    msg=c.getSelectedItem();
    g.drawString("choice status:"+msg,100,60);
    msg=cbg.getSelectedCheckbox().getLabel();
    g.drawString("radio button status:"+msg,100,80);
    g.drawString("scrollbar position:"+sc.getValue(),200,100);
    msg=name.getText();
    g.drawString("name typed is:"+msg,200,120);
    msg=password.getText();
    g.drawString("password typed is:"+msg,200,140);
    }
    public static void main(String[] args)
    {
    Allawt f=new Allawt();
    f.setTitle("frame");
    f.setSize(1000,1000);
    f.setVisible(true);
    }
    }
    ################################################## #################################
    this is the runtime error.. i am a newbee to java
    java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at Allawt.<init>(Allawt.java:50)
    at Allawt.main(Allawt.java:113)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Allawt.paint(Allawt.java:106)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    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)

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: please correct my programme on swings

    Again, look at that stack trace. It's telling you exactly what line the error is on: Allawt.paint(Allawt.java:106)

    What line is that? Have you used print statements or a debugger to figure out the values of each thing on that line?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please correct my programme on swings

    friend (Kevin Workman) please help me correct my code please

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: please correct my programme on swings

    Quote Originally Posted by jeevan reddy mandali View Post
    friend (Kevin Workman) please help me correct my code please
    Can't you see, he is helping you?
    Just do as he asked you to do and you will be able to learn to solve this problem as well as many other problems related to programming.

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: please correct my programme on swings

    thx kevin and mr.777

Similar Threads

  1. Java Swings
    By rosebrit3 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 19th, 2011, 08:16 AM
  2. Java jar programme with a microcontroller
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 11th, 2011, 08:01 AM
  3. [SOLVED] Can anyone mail me an e-book on Swings?
    By Lord Voldemort in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2011, 07:57 AM
  4. [SOLVED] I have a problem with this Joption programme.
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 12th, 2011, 03:31 AM
  5. help with bookfinder programme
    By kidza12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2011, 02:15 PM

Tags for this Thread