umershaikh
New Member
Hi I am novice Java learner. I was tring to implement a simple GUI program to change the color of a panel on clicking clicking of a button.\[code\]import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Button_lable implements ActionListener {public JFrame frame;//JPanel panel;//JLabel label;public static void main(String[] args) { Button_lable gui = new Button_lable(); gui.go();}//end of mainpublic void go(){ //System.out.println("Entered Go()"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b_frame = new JButton("Click to change the color"); b_frame.addActionListener(this); MyDrawpanel d_panel = new MyDrawpanel(); frame.getContentPane().add(BorderLayout.SOUTH,b_frame); frame.getContentPane().add(BorderLayout.CENTER ,d_panel); frame.setSize(300, 300); frame.setVisible(true);}//end of gopublic void actionPerformed(ActionEvent e) { frame.repaint();}}//end of Button_lableclass MyDrawpanel extends JPanel { public void paintComponent(Graphics g){Graphics2D grph = (Graphics2D) g;int red = (int)(Math.random()* 255);int green = (int)(Math.random()* 255);int blue = (int)(Math.random()* 255);Color strt_clr = new Color(red,green,blue);red = (int)(Math.random()* 255);green = (int)(Math.random()* 255);blue = (int)(Math.random()* 255);Color end_clr = new Color(red,green,blue);GradientPaint gradient = new GradientPaint(70,70,strt_clr,150,150,end_clr); grph.setPaint(gradient);grph.fillOval(50,25, 150, 150); }}\[/code\]I get output window. But when i click on the button, i get the following exception.\[code\] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionat Button_lable.actionPerformed(Button_lable.java:34)at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)at javax.swing.DefaultButtonModel.setPressed(Unknown Source)at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)at java.awt.Component.processMouseEvent(Unknown Source)at javax.swing.JComponent.processMouseEvent(Unknown Source)at java.awt.Component.processEvent(Unknown Source)at java.awt.Container.processEvent(Unknown Source)at java.awt.Component.dispatchEventImpl(Unknown Source)at java.awt.Container.dispatchEventImpl(Unknown Source)at java.awt.Component.dispatchEvent(Unknown Source)at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)at java.awt.LightweightDispatcher.dispatchEvent(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$200(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.doIntersectionPrivilege(Unknown Source)at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)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.doIntersectionPrivilege(Unknown Source)at java.awt.EventQueue.dispatchEvent(Unknown Source)at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.run(Unknown Source)\[/code\]Please advice.Kind Regards,Arpit.