Results 1 to 3 of 3
- 09-30-2011, 12:33 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Trouble with non static method from a static context
All I have to do is get the two calls to inputData() and queryData() to work so I can run the program, but I'm not sure what's wrong. Here's the code: (at least all that's needed to fix it I think)
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Olympic
{
private DLList numbers = new DLList();
public static void main(String [] args)
{
inputData();
queryData();
}
public void inputData()
{
Scanner scoreScanner = new Scanner(System.in);
String name = "";
double score = 0;
while(!name.equals("done"))
{
System.out.println("Enter in 'done' to exit");
System.out.println("Enter in the name of the diver: ");
name = scoreScanner.next();
System.out.println("Enter in the diver's score: ");
score = scoreScanner.nextDouble();
numbers.add(name, score);
}
}
void queryData()
{
new MyFrame();
displayAllAscending();
displayAllDescending();
System.out.println(getScores());
displayLowest();
displayHighest();
}
Thanks.Last edited by cjw92; 09-30-2011 at 12:42 AM.
- 09-30-2011, 12:51 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Re: Trouble with non static method from a static context
You should create an object of the method you're calling.
Java Code:public static void main(String [] args) { inputData(); queryData(); }Alternative, is you make the method static so that it will not require you to create an object before you call the methodJava Code:public static void main(String [] args) { Olympic oly = new Olympic(); oly.inputData(); oly.queryData(); }
-
Re: Trouble with non static method from a static context
Yep, you can't call non-static methods from within a static method unless you call them on an instance of object that contains the method.
But also, it sort of appears that you're trying to mix console code programming with Swing GUI programming, and if so, that can be a bad idea.
Similar Threads
-
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
Non-static method cannot be referenced from a static context
By GRoss in forum New To JavaReplies: 7Last Post: 05-19-2010, 11:12 AM -
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks