Results 1 to 3 of 3
  1. #1
    cjw92 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default 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.

  2. #2
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default 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();
    }
    Java Code:
    public static void main(String [] args)
    {
    Olympic oly = new Olympic();
    oly.inputData(); 
    oly.queryData();
    }
    Alternative, is you make the method static so that it will not require you to create an object before you call the method

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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

  1. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  2. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  3. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  4. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •