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!
Code java:
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() + "'.");
}
});
}}
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.
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!
Code java:
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() + "'.");
}
});
}}
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
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?
Re: Newbie Java problem --->"Cannot make a static reference to the non-static field d
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?
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() + "'.");
}
});
}}
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
}
}
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 ??