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 10 of 10

Thread: Newbie Java problem --->"Cannot make a static reference to the non-static field data"

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    6
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Newbie Java problem --->"Cannot make a static reference to the non-static field data"

    Hey so I've been working on a lab for my intro to software developing class and have been trying to get my program to work for hours since yesterday morning. It's a real basic program, im sure someone will be able to figure out the issue im having. Hopefully once I learn what I'm doing I'll be able to return the favor, but until then thanks, heres the code.

    I bolded the errors which happen when I try to reference the non static variable in my main method which i know doesnt work, but I cant seem to get it, there are always either errors, the program freezes, or just completely does nothing. Any help is appreciated, thanks!

    package lab8;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
    @SuppressWarnings("serial")
    public class GUIMonthNames extends JFrame {
    	private JLabel instructions;
    	private JTextField data;
    	public GUIMonthNames() {		
    		  super("Month Names: GUI version");
    		  setSize(400,400);
    		  setDefaultCloseOperation(EXIT_ON_CLOSE);
    		  Container myPane = getContentPane();
    		  myPane.setLayout(new FlowLayout());
    		  instructions = new JLabel("Enter a number (1-12) below");
    		  data = new JTextField(20);
    		  add(data);
    		  add(instructions);
    		  setVisible(true);	
    		}
     
     
    	public static void main(String[] args) {
    				new GUIMonthNames();
     
    			[B]data[/B].addActionListener(new ActionListener() {	
    				public void actionPerformed(ActionEvent e) {
    					Scanner keyboard = new Scanner(System.in);
    					int monthNumber = keyboard.nextInt();
    					monthNumber = new Integer([B]data[/B].getText());
    					final String MONTH_TABLE = "January  February March    April    May      June     " +
    							"July     August   SeptemberOctober  November December ";
    					int start = (monthNumber - 1) * 9;
    					int stop = start + 9;
    					String monthName = MONTH_TABLE.substring(start, stop);
    					[B]data[/B].setText(monthNumber +	" is '" + monthName.trim() + "'.");	
    				}
     
    		});	
     
    }}
    Last edited by Freaky Chris; November 4th, 2011 at 04:44 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    Static things (variables, methods, classes, etc) are things that don't belong to a particular instance of a class. For example, JFrame.EXIT_ON_CLOSE doesn't care which instance of JFrame it belongs to, because it's always going to be and mean the same thing. That's why the main method is always static- it doesn't belong to a particular instance of the class it's in.

    Your data variable is not static, so it belongs to a particular instance of your class. If you create two instances of your GUIMonthNames class, you're going to have two data variables that refer to two completely different things. But you'll still only have one main class, since it does not belong to any particular instance.

    So, when you're in the main method, you aren't in any particular instance of GUIMonthNames- so which data variable should it access? That's what that error means. To fix it, look at what you're doing with your constructor- you're creating an instance of GUIMonthNames, but then you aren't doing anything with it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    jhamill (November 5th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    6
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    hey thanks for the quick reply, I tried to fix what you suggested.

    Now there are no errors and I tried to create an instance for GUIMonthNames called MonthNames, then I used the instance to reference my non static variable. No errors but no dice. Is it a syntax problem or something larger, help please!


    package lab8;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    @SuppressWarnings("serial")
     
    public class GUIMonthNames extends JFrame {
    	private JLabel instructions;
    	private JTextField data;
     
    	public GUIMonthNames() {		
    		  super("Month Names: GUI version");
    		  setSize(400,400);
    		  setDefaultCloseOperation(EXIT_ON_CLOSE);
    		  Container myPane = getContentPane();
    		  myPane.setLayout(new FlowLayout());
    		  instructions = new JLabel("Enter a number (1-12) below");
    		  data = new JTextField(20);
    		  setVisible(true);	
    		  add(instructions);
    		  add(data);
    		}	
     
    	public static void main(String[] args) {
    			new GUIMonthNames();
    			final GUIMonthNames monthNames = new GUIMonthNames();
    			monthNames.data.addActionListener(new ActionListener()
    			 {	
    				public void actionPerformed(ActionEvent e) {
    					int	monthNumber = new Integer(monthNames.data.getText());
    					Scanner keyboard = new Scanner(System.in);
    					monthNumber = keyboard.nextInt();
    					final String MONTH_TABLE = "January  February March    April    May      June     " +
    							"July     August   SeptemberOctober  November December ";
    					int start = (monthNumber - 1) * 9;
    					int stop = start + 9;
    					String monthName = MONTH_TABLE.substring(start, stop);
    					monthNames.data.setText(monthNumber +	" is '" + monthName.trim() + "'.");	
    				}
     
    	});
     
    }}
    Last edited by Freaky Chris; November 4th, 2011 at 04:41 PM.

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    data is a private member of the class, so you cannot access it from outside of the class. You cannot use monthNames.data because data is declared as private, so it is hidden from the context.


    Chris

  6. The Following User Says Thank You to Freaky Chris For This Useful Post:

    jhamill (November 5th, 2011)

  7. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    6
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    so how can I go about changing it this to make it work properly?

  8. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    Have a read through this trail
    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    This should give you an insite into what you are trying to do.

    Chris

  9. The Following User Says Thank You to Freaky Chris For This Useful Post:

    jhamill (November 5th, 2011)

  10. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    You could also use a getter function.

    But why are you doing any of that from main in the first place?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #8
    Junior Member
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    6
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    Hey guys, I hope I dont seem too dense, I just started using Java a few weeks ago. I really cannot seem to understand what I need to do in order to get this program to run. Fundamentally I understand that my data variable is private and cannot be accessed by different packages or subclasses, but I followed the instructions for our lab perfectly and am still stuck. I put my code back to how it originally was with the only errors being whenever data is referenced in my main method. If it shouldnt be in main, what method should it be nested under?



    package lab8;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    @SuppressWarnings("serial")

    public class GUIMonthNames extends JFrame {
    private JLabel instructions;
    private JTextField data;

    public GUIMonthNames() {
    super("Month Names: GUI version");
    setSize(400,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container myPane = getContentPane();
    myPane.setLayout(new FlowLayout());
    instructions = new JLabel("Enter a number (1-12) below");
    data = new JTextField(20);
    setVisible(true);
    add(instructions);
    add(data);
    }

    public static void main(String[] args) {
    new GUIMonthNames();
    data.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e) {
    int monthNumber = new Integer(data.getText());
    Scanner keyboard = new Scanner(System.in);
    monthNumber = keyboard.nextInt();
    final String MONTH_TABLE = "January February March April May June " +
    "July August SeptemberOctober November December ";
    int start = (monthNumber - 1) * 9;
    int stop = start + 9;
    String monthName = MONTH_TABLE.substring(start, stop);
    data.setText(monthNumber + " is '" + monthName.trim() + "'.");
    }

    });

    }}

  12. #9
    Junior Member
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    6
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    ive gotta be getting close, no errors again, this time i tried to implement the actionlistener from the public class, that way I could move the actionperformed/actionlistener outside of public static void main. So everything seems like it should work, but it doesnt. Doesnt freeze but doesnt work.

    package lab8;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;

    @SuppressWarnings("serial")
    public class GUIMonthNames extends JFrame implements ActionListener {
    private JLabel instructions;
    private JTextField data;
    public GUIMonthNames() {
    super("Month Names: GUI version");
    setSize(400,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container myPane = getContentPane();
    myPane.setLayout(new FlowLayout());
    instructions = new JLabel("Enter a number (1-12) below");
    data = new JTextField(20);
    add(data);
    add(instructions);
    setVisible(true);
    }
    public static void main(String[] args) {
    new GUIMonthNames();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    data.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    Scanner keyboard = new Scanner(System.in);
    int monthNumber = keyboard.nextInt();
    monthNumber = new Integer(data.getText());
    final String MONTH_TABLE = "January February March April May June " +
    "July August SeptemberOctober November December ";
    int start = (monthNumber - 1) * 9;
    int stop = start + 9;
    String monthName = MONTH_TABLE.substring(start, stop);
    data.setText(monthNumber + " is '" + monthName.trim() + "'.");
    }
    });
    // TODO Auto-generated method stub
    }
    }

  13. #10
    Junior Member
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    6
    My Mood
    Inspired
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d

    i think i need a document listener to get the jtextfield to read the changes ??

Similar Threads

  1. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  2. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  3. Replies: 13
    Last Post: October 13th, 2010, 11:20 AM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. password field, with focused "Enter" button
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM

Tags for this Thread