Results 1 to 4 of 4
  1. #1
    spec8320 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Colorchooser - Strange Exceptions in simple code.

    Hi, I have really tough problem I tried to solve it but I cant ... After running this code: I have some exceptions. Line 76 is "Color wybor = Okno.Chooser.getColor();" and I cant understand why its a problem... So I'm looking for help from you guys.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.colorchooser.*;
    
    class data
    {
    	public static boolean zmienna_do_przycisku_chooser = false;
    }
    class Okno extends JFrame {
     
    	// Colorchosser
    	public static JColorChooser Chooser;
    	// Przycisk colorchoosera
    	static JButton przycisk_colorchooser = new JButton();
    	ActionListener przycisk_colorchooser_listener = new przycisk_colorchooser();
    
    	private static final long serialVersionUID = 1L;
    
    	public Okno() {
            setSize(1000, 700);
            setTitle("Grafika komputerowa"); 
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
            
            // Dodaje panel
            Panel panel = new Panel();
            add(panel);
            // Przycisk Rysowania
    		przycisk_colorchooser.addActionListener(przycisk_colorchooser_listener);
    		panel.add(przycisk_colorchooser);
    		
            Chooser = new JColorChooser ();
            Chooser.setPreviewPanel(new JPanel());
    		panel.add(Chooser);
    		Chooser.setVisible(false);
    		Chooser.setColor(Color.black);
        }
    	
    	// Przycisk colorchooser
    	public class przycisk_colorchooser implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(data.zmienna_do_przycisku_chooser == false)
    			{
    				Okno.Chooser.setVisible(true);
    				data.zmienna_do_przycisku_chooser = true;
    			}
    			else
    			{
    				Okno.Chooser.setVisible(false);	
    				data.zmienna_do_przycisku_chooser = false;
    			}
    			repaint();
    		}
    	}
    }
     
    class Panel extends JPanel {
     
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public void paintComponent(Graphics g) {
     
            Graphics2D g2 = (Graphics2D) g;
            // Czyszczenie
            g2.clearRect(0,0,1000,700);
            // Przycisk colorchooser
            Okno.przycisk_colorchooser.setBounds(30,30,100,100);
            Color wybor = Okno.Chooser.getColor();
            Okno.przycisk_colorchooser.setBackground(wybor);
            g2.setPaint(Color.black);
    		repaint();
        }
    }
     
    public class Main {
     
        public static void main(String args[]) {
            Okno okno = new Okno();
        }
    }
    Exceptions:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Panel.paintComponent(Main.java:76)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JLayeredPane.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    	at java.awt.Container.paint(Unknown Source)
    	at java.awt.Window.paint(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$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)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Panel.paintComponent(Main.java:76)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$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)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your code has many problems not the least of which is over use of statics variables, but your problem is your calling the chooser from within paintComponent before any colors have been chosen and before the chooser has been rendered. You should re-think your design. Perhaps set your color first to a default value, then give the Panel class a Color field and a public setColor(Color color) method. Then have the other class, the one with the color chooser set the Panel's color once a color has been chosen.

  3. #3
    spec8320 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Fubarable as you can see, I'm new in this and I don't understand at all how its work, so if you can give me some advice, or link or simply repair it I will be really glad for you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again your code has many mistakes, in fact I see that you're trying to set the color of another component from within a paint/paintComponent method:

    Java Code:
       public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.clearRect(0, 0, 1000, 700);
          Okno.przycisk_colorchooser.setBounds(30, 30, 100, 100); //You shouldn't be setting bounds 
          Color wybor = Okno.Chooser.getColor();  // or colors of other components in paintComponent
          Okno.przycisk_colorchooser.setBackground(wybor);
          g2.setPaint(Color.black);
          repaint();  // you should never call repaint from within a paintComponent method.
       }
    and so that code shouldn't even be in that method. I think that you should start at the beginning, going through the basic Java tutorials here:

    Trail: Learning the Java Language (The Java™ Tutorials)

    Then when you've got a solid footing in Java, look over the Swing tutorials which you can find here:

    Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Last edited by Fubarable; 04-09-2011 at 04:43 PM.

Similar Threads

  1. Create customized ColorChooser
    By LianaN in forum AWT / Swing
    Replies: 1
    Last Post: 08-26-2010, 08:24 PM
  2. Replies: 2
    Last Post: 12-08-2009, 08:33 PM
  3. strange code
    By tghn2b in forum New To Java
    Replies: 3
    Last Post: 12-22-2008, 11:51 PM
  4. Please help me this is simple bit of code
    By BlitzAcez in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 05:52 AM
  5. simple code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •