Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Simple GUI Error

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple GUI Error

    I coded all of this from what the book i'm reading said to do and I don't know what I did wrong. The majority of the code looks fine except for these two errors.

    import java.awt.*;
    import javax.swing.*;
     
    public class LottoMadness extends JFrame {
     
    	//set up row 1
    	JPanel row1 = new JPanel();
    	ButtonGroup option = new ButtonGroup();
    	JCheckBox quickpick = new JCheckBox("Quick Pick", false);
    	JCheckBox personal = new JCheckBox("Personal", true);
    	//set up row 2
    	JPanel row2 = new JPanel();
    	JLabel numbersLabel = new JLabel("Your picks: ", JLabel.RIGHT);
    	JTextField[] numbers = new JTextField[6];
    	JLabel winnersLabel = new JLabel("Winners:", JLabel.RIGHT);
    	JTextField[] winners = new JTextField[6];
    	//set up row 3
    	JPanel row3 = new JPanel();
    	JButton stop = new JButton("Stop");
    	JButton play = new JButton("Play");
    	JButton reset = new JButton("Reset");
    	//set up row 4
    	JPanel row4 = new JPanel();
    	JLabel got3Label = new JLabel("3 of 6", JLabel.RIGHT);
    	JTextField got3 = new JTextField("0");
    	JLabel got4Label = new JLabel("4 of 6", JLabel.RIGHT);
    	JTextField got4 = new JTextField("0");
    	JLabel got5Label = new JLabel("5 of 6", JLabel.RIGHT);
    	JTextField got5 = new JTextField("0");
    	JLabel got6Label = new JLabel("6 of 6", JLabel.RIGHT);
    	JTextField got6 = new JTextField("0");
    	JLabel drawingsLabel = new JLabel("Drawings", JLabel.RIGHT);
    	JTextField drawings = new JTextField("0");
    	JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
    	JTextField years = new JTextField();
     
    	public LottoMadness() {
    		super("Lotto Madness");
    		setLookAndFeel();
    		setSize(550,400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GridLayout layout = new GridLayout(5, 1, 10, 10);
    		setLayout(layout);
     
    		FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
    		option.add(quickpick);
    		option.add(personal);
    		row1.setLayout(layout1);
    		row1.add(quickpick);
    		row1.add(personal);
    		add(row1);
     
    		GridLayout layout2 = new GridLayout(2, 7, 10, 10);
    		row2.setLayout(layout2);
    		row2.add(numbersLabel);
    		for (int i = 0; i < 6; i++){
    			numbers[i] = new JTextField();
    			row2.add(numbers[1]);
    		}
    		row2.add(winnersLabel);
    		for (int i = 0; i < 6; i++){
    			winners[i] = new JTextField();
    			winners[i].setEditable(false);
    			row2.add(winners[i]);
    		}
    		add(row2);
     
    		FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
    		row3.setLayout(layout3);
    		stop.setEnabled(false);
    		row3.add(stop);
    		row3.add(play);
    		row3.add(reset);
    		add(row3);
     
    		GridLayout layout4 = new GridLayout(2, 3, 20, 10);
    		row4.setLayout(layout4);
    		row4.add(got3Label);
    		got3.setEditable(false);
    		row4.add(got3);
    		row4.add(got4Label);
    		got4.setEditable(false);
    		row4.add(got4);
    		row4.add(got5Label);
    		got5.setEditable(false);
    		row4.add(got5);
    		row4.add(got6Label);
    		got6.setEditable(false);
    		row4.add(got6);
    		drawings.setEditable(false);
    		row4.add(drawings);
    		row4.add(yearsLabel);
    		years.setEditable(false);
    		row4.add(years);
    		add(row4);
     
    		setVisible(true);
    	}
     
    	private void setLookAndFeel() {
    		try {
    			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    		} catch (Exception exc) {
    			//ignore error
    		}
    	}
     
    	public static void main(String[] args){
    		LottoMadness frame = new LottoMadness();
    	}
     
    }

    The errors are:

    1. Where I define the class (public class LottoMadness extends JFrame {) it warns me "The serializable class LottoMadness does not declare a static final serialVersionUID of type long.

    2. In the main method it says the variables JFrame and frame are not used.

    Help is appreciated!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple GUI Error

    What book are you using? I ask, because the main() method is teaching bad habits and is not technically correct. Will it work? Yes, but it's bad form for later. (Saw something similar online, is it Sam's Teach Yourself Java in 24 Hours? If so, which edition do you have?)

    What you posted are warnings, not errors. The code you posted will run fine with those warnings. I didn't run the code, but I'm assuming there are no other errors and therefore that you've dong nothing wrong. If you're bothered by them, you can configure the IDE (if you're using one) and the compiler to suppress warnings.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple GUI Error

    Yes, that is the book i'm using (7th edition). And you said it should work but it doesn't. Here's what the console says :

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at LottoMadness.<init>(LottoMadness.java:58)
    at LottoMadness.main(LottoMadness.java:109)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple GUI Error

    That error is not related to the warnings you posted. I assumed there were no other errors, and I was wrong. You have a typo in the line (#58 by my count):

    row2.add( numbers[1] );

    See if you can find it.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple GUI Error

    O! It's supposed to be [i]. Thanks!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple GUI Error

    You're welcome.

Similar Threads

  1. simple gui java quiz
    By jumpman8947 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 4th, 2013, 07:41 AM
  2. Creating a Simple GUI JFrame
    By Xbatz in forum Java Swing Tutorials
    Replies: 1
    Last Post: April 16th, 2012, 11:03 PM
  3. Very simple GUI problem
    By clydefrog in forum AWT / Java Swing
    Replies: 8
    Last Post: March 20th, 2012, 10:37 AM
  4. simple translator GUI ??
    By sciences in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2012, 06:18 AM
  5. Simple GUI/Animation question...
    By teslatrain in forum AWT / Java Swing
    Replies: 3
    Last Post: April 29th, 2011, 04:10 PM

Tags for this Thread