Unknown Source

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tactech
    New Member
    • Mar 2007
    • 1

    #1

    Unknown Source

    Hello

    Can somebody help me and tell me why I can’t change the background color of the second PN2 panel.

    I get this error about unknown source

    Exception in thread "AWT-EventQueue-1" java.lang.NullP ointerException
    at test$myFreaking Listener.action Performed(test. java:34)
    at java.awt.Button .processActionE vent(Unknown Source)
    at java.awt.Button .processEvent(U nknown Source)
    at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
    at java.awt.Compon ent.dispatchEve nt(Unknown Source)
    at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
    at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
    at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
    at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
    at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
    at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
    at java.awt.EventD ispatchThread.r un(Unknown Source)




    and this is the source
    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class  test extends Applet  {
    	
    	 public void init () {
    		 
    		    this.setSize(600,440);
    		   	this.setBackground(Color.white);
    		   	this.setLayout(new GridLayout(2,1));
    		   	Panel PN1 = new Panel(); 
    		   	this.add(PN1);
    		   	
    			Panel PN2 = new Panel(); 
    		   	this.add(PN2);
    		   	PN2.setBackground(Color.red);
    		   	
    		   	
    		   	
    		   	Button but = new Button();
    		   	PN1.add(but);
    		   	but.setLabel("Change Panel's Color ");
    		    but.addActionListener(new myFreakingListener());
    		    
    		    
    		    
    	 }
    	 class myFreakingListener  implements ActionListener{
    
    		   public void actionPerformed(ActionEvent ae){
    	    			
    			  PN2.setBackground(Color.orange);
    	    }
    	   }	
    	 Panel PN1, PN2;
    }
    I am doing something wrong here but I can’t find it.

    Any one?
    Last edited by horace1; Mar 9 '07, 07:59 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you have class instance variables PN1 and PN2
    Code:
             Panel PN1, PN2;
    but also decalre tham as local variables in init()
    Code:
                            Panel PN2 = new Panel(); 
                            this.add(PN2);
                            PN2.setBackground(Color.red);
    therefore in
    Code:
                       public void actionPerformed(ActionEvent ae){                       
                              PN2.setBackground(Color.orange);
                }
    PN2 is NULL.

    code in init() should be
    Code:
                            PN2 = new Panel(); 
                            this.add(PN2);
                            PN2.setBackground(Color.red);
    so that the instance variable PN2 is set up to reference the new Panel()

    Comment

    Working...