Results 1 to 6 of 6
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [newbie] java.lang.NullPointerException

    Any ideas? Program breaks at the point in bold.

    :confused:

    Java Code:
    [B]FontFrame.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FontFrame extends JFrame {
    	
    	public FontFrame() {
    		setTitle("GridBagLayout Test");
    		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    		
    		GridBagLayout layout = new GridBagLayout();
    		setLayout(layout);
    		
    		ActionListener listener = new FontAction();
    		
    		//construct components
    		@SuppressWarnings("unused")
    		JLabel faceLabel = new JLabel("Face :");
    		face = new JComboBox(new String[] {"Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput"});
    		face.addActionListener(listener);
    		
    		@SuppressWarnings("unused")
    		JLabel sizeLabel = new JLabel("Size: ");
    		size = new JComboBox(new String[] {"8","10","12","15", "18", "24", "36", "48"});
    		
    		size.addActionListener(listener);
    		
    		bold = new JCheckBox("Bold");
    		bold.addActionListener(listener);
    		sample = new JTextArea();
    		sample.setText("The quick brown fox jumps over the lazy dog.");
    		sample.setEditable(false);
    		sample.setLineWrap(true);
    		sample.setBorder(BorderFactory.createEtchedBorder());
    		
    		//add components to grid, using GBC convenience class 
    		add(faceLabel, new GBC(0,0).setAnchor(GBC.EAST));
    		add(face, new GBC(1,0).setAnchor(GBC.HORIZONTAL));
    		add(sizeLabel, new GBC(0,1).setAnchor(GBC.EAST));
    		add(size, new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(1,1,100,100)); //assumed (1,1) to (100,100)
    		add(bold, new GBC(0,2,2,1).setAnchor(GBC.CENTER).setWeight(100,0).setInsets(1,1,100,100)); 
    		add(italic, new [B]GBC(0,3,2,1).setAnchor(GBC.CENTER).setWeight(100,100)); [/B] 
    		add(sample, new GBC(2,0,1,4).setAnchor(GBC.BOTH).setWeight(100,100));
    		
    	}
    	
    	public static final int DEFAULT_WIDTH = 300;
    	public static final int DEFAULT_HEIGHT = 200;
    	
    	private JComboBox face;
    	private JComboBox size;
    	private JCheckBox bold;
    	private JCheckBox italic;
    	private JTextArea sample;
    	
    	/**
    	 * An action listener that changes the font of the sample text
    	 */
    	private class FontAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			String fontFace = (String) face.getSelectedItem();
    			int fontStyle = (bold.isSelected() ? Font.BOLD : 0) +
    			(italic.isSelected() ? Font.ITALIC : 0);
    			int fontSize = Integer.parseInt((String) size.getSelectedItem());
    			Font font = new Font(fontFace, fontStyle, fontSize);
    			sample.setFont(font);
    			sample.repaint();
    		}
    	}
    }
    
    [B]GBC.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    
    @SuppressWarnings("serial")
    /** Convenience class GBC which abbreviates java.awt.GridBagConstraints.
     */
    public class GBC extends GridBagConstraints {
    	/** 
    	 * Constructs a GBC with a given gridx and gridy position and all other grid
    	 * bag constraint values set to the default.
    	 * @param gridx: the gridx position
    	 * @param gridy: the gridy position
    	 */
    	public GBC (int gridx, int gridy)
    	{
    		this.gridx = gridx;
    		this.gridy = gridy;
    	}
    	
    	/** Constructs a GBC with given gridx, gridy, gridwidth, gridheight and all
    	 * other grid bag constraint values set to the default.
    	 * @param gridx: the gridx position
    	 * @param gridy: the gridy position
    	 * @param gridwidth: the cell span in x-direction
    	 * @param gridheight: the cell span in y-direction
    	 */
    	public GBC (int gridx, int gridy, int gridwidth, int gridheight)
    	{
    		this.gridx = gridx;
    		this.gridy = gridy;
    		this.gridwidth = gridwidth;
    		this.gridheight = gridheight;
    	}
    	
    	/** Sets the anchor.
    	 * @param anchor: the anchor value
    	 * @param return: this object for further modification
    	 */
    	public GBC setAnchor(int anchor)
    	{
    		this.anchor = anchor;
    		return this;
    	}
    	
    	/**
    	 * Sets the fill direction.
    	 * @param fill: the fill direction.
    	 * @return: this object for further modification
    	 */
    	public GBC setFill(int fill)
    	{
    		this.fill = fill;
    		return this;
    	}
    	
    	/**
    	 * Sets the cell weights
    	 * @param weightx: the cell weight in x-direction
    	 * @param weighty: the cell height in y-direction
    	 * @return: this object for further modification
    	 */
    	public GBC setWeight(double weightx, double weighty)
    	{
    		this.weightx = weightx;
    		this.weighty = weighty;
    		return this;
    	}
    	
    	/**
    	 * Sets the insets of this cell.
    	 * @param top: the spacing to use on top
    	 * @param left: the spacing to use to the left
    	 * @param bottom: the spacing to use to the bottom
    	 * @param right: the spacing to use to the right
    	 * @return: this object for further modification
    	 */
    	public GBC setInsets(int top, int left, int bottom, int right)
    	{
    		this.insets = new Insets(top, left, bottom, right);
    		return this;
    	}
    	
    	/** Sets the internal padding.
    	 * @param: ipadx: the internal padding in x-direction
    	 * @param: ipady: the internal padding in y-direction
    	 */
    	public GBC setIpad(int ipadx, int ipady)
    	{
    		this.ipadx = ipadx;
    		this.ipady = ipady;
    		return this;
    	}
    }
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at homenetwork.bkr.training.FontFrame.<init>(FontFram e.java:46)
    at homenetwork.bkr.training.GridBagLayoutTest$1.run(G ridBagLayoutTest.java:14)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    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)
    Last edited by jon80; 05-27-2009 at 05:11 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The JCheckBox "italic" has not been instantiated and is therefore null.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks that was it.

    Now I'm getting another error:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal anchor value
    at java.awt.GridBagLayout.AdjustForGravity(Unknown Source)
    at java.awt.GridBagLayout.adjustForGravity(Unknown Source)
    at java.awt.GridBagLayout.ArrangeGrid(Unknown Source)
    at java.awt.GridBagLayout.arrangeGrid(Unknown Source)
    at java.awt.GridBagLayout.layoutContainer(Unknown Source)
    at java.awt.Container.layout(Unknown Source)
    at java.awt.Container.doLayout(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validate(Unknown Source)
    at java.awt.Window.show(Unknown Source)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at homenetwork.bkr.training.GridBagLayoutTest$1.run(G ridBagLayoutTest.java:16)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    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)
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal anchor value
    at java.awt.GridBagLayout.AdjustForGravity(Unknown Source)
    at java.awt.GridBagLayout.adjustForGravity(Unknown Source)
    at java.awt.GridBagLayout.ArrangeGrid(Unknown Source)
    at java.awt.GridBagLayout.arrangeGrid(Unknown Source)
    at java.awt.GridBagLayout.layoutContainer(Unknown Source)
    at java.awt.Container.layout(Unknown Source)
    at java.awt.Container.doLayout(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validate(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    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)


    Updated code:

    Project GridBagLayoutTest
    Java Code:
    [B]FontFrame.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FontFrame extends JFrame {
    	
    	public FontFrame() {
    		setTitle("GridBagLayout Test");
    		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    		
    		GridBagLayout layout = new GridBagLayout();
    		setLayout(layout);
    		
    		ActionListener listener = new FontAction();
    		
    		//construct components
    		@SuppressWarnings("unused")
    		JLabel faceLabel = new JLabel("Face :");
    		face = new JComboBox(new String[] {"Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput"});
    		face.addActionListener(listener);
    		
    		@SuppressWarnings("unused")
    		JLabel sizeLabel = new JLabel("Size: ");
    		size = new JComboBox(new String[] {"8","10","12","15", "18", "24", "36", "48"});
    		
    		size.addActionListener(listener);
    		
    		bold = new JCheckBox("Bold");
    		bold.addActionListener(listener);
    		
    		italic = new JCheckBox("Italic");
    		italic.addActionListener(listener);
    
    		sample = new JTextArea();
    		sample.setText("The quick brown fox jumps over the lazy dog.");
    		sample.setEditable(false);
    		sample.setLineWrap(true);
    		sample.setBorder(BorderFactory.createEtchedBorder());
    		
    		//add components to grid, using GBC convenience class 
    		add(faceLabel, new GBC(0,0).setAnchor(GBC.EAST));
    		add(face, new GBC(1,0).setAnchor(GBC.HORIZONTAL));
    		add(sizeLabel, new GBC(0,1).setAnchor(GBC.EAST));
    		add(size, new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(1,1,100,100)); //assumed (1,1) to (100,100)
    		add(bold, new GBC(0,2,2,1).setAnchor(GBC.CENTER).setWeight(100,0).setInsets(1,1,100,100)); 
    		add(italic, new GBC(0,3,2,1).setAnchor(GBC.CENTER).setWeight(100,100)); //ERROR:java.lang.NullPointerException
    		add(sample, new GBC(2,0,1,4).setAnchor(GBC.BOTH).setWeight(100,100));
    		
    	}
    	
    	public static final int DEFAULT_WIDTH = 300;
    	public static final int DEFAULT_HEIGHT = 200;
    	
    	private JComboBox face;
    	private JComboBox size;
    	private JCheckBox bold;
    	private JCheckBox italic;
    	private JTextArea sample;
    	
    	/**
    	 * An action listener that changes the font of the sample text
    	 */
    	private class FontAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			String fontFace = (String) face.getSelectedItem();
    			int fontStyle = (bold.isSelected() ? Font.BOLD : 0) +
    			(italic.isSelected() ? Font.ITALIC : 0);
    			int fontSize = Integer.parseInt((String) size.getSelectedItem());
    			Font font = new Font(fontFace, fontStyle, fontSize);
    			sample.setFont(font);
    			sample.repaint();
    		}
    	}
    }
    
    
    [B]GridBagLayoutTest.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagLayoutTest {
    
    	public static void main (String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				FontFrame frame = new FontFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
            //add components to grid, using GBC convenience class
            add(faceLabel,
                    new GBC(0,0).setAnchor(GBC.EAST));
            add(face,
                    new GBC(1,0)[b]//.setAnchor(GBC.HORIZONTAL));[/b]
                                .setFill(GBC.HORIZONTAL));
            add(sizeLabel, new GBC(0,1).setAnchor(GBC.EAST));
            //assumed (1,1) to (100,100)
            add(size,
                    new GBC(1,1).setFill(GBC.HORIZONTAL)
                                .setWeight(100,0)
                                .setInsets(1,1,100,100));
            add(bold,
                    new GBC(0,2,2,1).setAnchor(GBC.CENTER)
                                    .setWeight(100,0)
                                    .setInsets(1,1,100,100));
            add(italic,
                    new GBC(0,3,2,1).setAnchor(GBC.CENTER)
                                    .setWeight(100,100));
            add(sample,
                    new GBC(2,0,1,4)[b]//.setAnchor(GBC.BOTH)[/b]
                                    .setFill(GBC.BOTH)
                                    .setWeight(100,100));

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    hmm...nope, still the same error

  6. #6
    xierangh is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default

    //add components to grid, using GBC convenience class
    add(faceLabel, new GBC(0,0).setAnchor(GBC.EAST));
    add(face, new GBC(1,0).setAnchor(GBC.HORIZONTAL));
    add(sizeLabel, new GBC(0,1).setAnchor(GBC.EAST));

    the code whick underline is wrong.

Similar Threads

  1. [SOLVED] [newbie] java.lang.NullPointerException
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:19 PM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  3. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  4. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

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