Hi there,

I'm getting an error saying: non-static variable MainList cannot be referenced from a static context.

My snippet of code where the error is occuring:

//this method is in a class called ViewContact
public void printInfo(){
	int index = MainScreen.MainList.getSelectedIndex();
}

Now the List "MainList" is in a class called MainScreen and has public access.
What I'm trying to do, is get an int from the getSelectedIndex() method of the
List class.. But the error above gets thrown.

Thanks for any help!

The error says: "non-static variable MainList" I assume that MainList is non static, so in order to access you need to have an instance of the class MainScreen

MainList is a non-static, ie instance, variable. Java creates one of these variables for every instance of MainScreen that you create. To access any one of these instance variables you need to use the the instance it belongs to. eg

MainScreen myScreen = new MainScreen();
myScreen.Mainlist...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.