#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0

    Java exception in thread


    I programmed a screen where it supposed to show different labels, text areas, buttons, etc, and it compiles with no errors but when I run the program the command prompt shows a lot of exceptions I can't figure out why its there, and my window isn't showing up... here's my code thanks
    Code:
    import java.awt.*;
    import java.io.*;
    import sun.audio.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Color.*;
    import javax.swing.JFrame.*;
    import java.awt.EventQueue.*;
    
    public class EncMain extends JFrame
    {
    	private JLabel imageIcon;
    	private JComboBox box1;
    	private JButton button1;
    	private JLabel label1;
    	private JLabel label2;
    	private JTextField field1;
    
    
    	public EncMain()
    	{
    		JLabel label1 = new JLabel();
    		JLabel label2 = new JLabel();
    		JButton button1 = new JButton();
    		JTextField text1 = new JTextField();
    		JComboBox box1 = new JComboBox();
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("Main Menu");
    		setBackground(new Color(204, 102, 0));
    
    		label1.setIcon(new ImageIcon ("bible3.gif"));
    		label1.setBorder(BorderFactory.createLineBorder(Color.blue));
    
    		label2.setIcon(new ImageIcon ("seed.jpg"));
    		label2.setPreferredSize(new Dimension(414,330));
    
    		button1.setBackground( new Color(0, 0, 0));
    		button1.setForeground(new Color(255, 0, 0));
    		button1.setText("Exit");
    
    		text1.setBackground(new Color(0, 102, 102));
    		text1.setFont(new Font("Times New Roman", 1, 24));
    		text1.setText("Welcome to Wisdom");
    
    		box1.setModel(new DefaultComboBoxModel(new String[]
    		{
    			"Gospel", "Raeggae", "Hip-Hop"
    
    		}));
    		GroupLayout layout = new GroupLayout(getContentPane());
    		getContentPane().setLayout(layout);
    		layout.setHorizontalGroup(
    			layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addComponent(label2, GroupLayout.PREFERRED_SIZE, 340, GroupLayout.PREFERRED_SIZE)
    			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 286, Short.MAX_VALUE)
    			.addComponent(label1, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
    			.addGroup(layout.createSequentialGroup()
    				.addGap(300, 300, 300)
    				.addComponent(button1, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))
    			.addContainerGap())
    			.addGroup(layout.createSequentialGroup()
    				.addGap(262, 262, 262)
    				.addComponent(text1, GroupLayout.PREFERRED_SIZE, 228, GroupLayout.PREFERRED_SIZE)
    				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
    				.addComponent(box1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    				.addGap(108, 108, 108))
    
    		);
    
                              pack();
                              setVisible(true);
    
    
    
    		layout.setVerticalGroup(
    			layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addGap(8, 8, 8)
    			.addComponent(field1, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
    			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    			.addComponent(label2, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE))
    			.addGroup(layout.createSequentialGroup()
    			.addGap(16, 16, 16)
    			.addComponent(box1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    			.addGap(18, 18, 18)
    			.addComponent(label1)))
    			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
    			.addComponent(button1, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
    			.addContainerGap())
    
    			);
    			pack();
    			setVisible(true);
    		}
    		public static void main(String[] args)
    		{
    			new EncMain();
    		}
    
    }
    The exception threads are as followed

    Exception in thread "main" java.lang.IllegalStateException: javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0 .0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1c843d62,flags=296,maximumSize= ,minimumSize=,pre
    ferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selection
    Color=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
    at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:878)
    at java.awt.Container.preferredSize(Container.java:1788)
    at java.awt.Container.getPreferredSize(Container.java:1773)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:917)
    at java.awt.Container.preferredSize(Container.java:1788)
    at java.awt.Container.getPreferredSize(Container.java:1773)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
    at java.awt.Container.preferredSize(Container.java:1788)
    at java.awt.Container.getPreferredSize(Container.java:1773)
    at java.awt.Window.pack(Window.java:809)
    at EncMain.<init>(EncMain.java:73)
    at EncMain.main(EncMain.java:102)
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    There are two calls to pack(). try removing one of them.

IMN logo majestic logo threadwatch logo seochat tools logo