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

Thread: Programm does not start. Help Please!

  1. #1
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Programm does not start. Help Please!

    Hey I have written this code:
    When I am executing it, I have problems with my main class. What can I do?
    (German Student)

    Programm should just start the methods in 2 other classes.

    ERROR MESSAGE:
    "non-static method ende() cannot be referenced from a static context"


    ---------------------------
     
     
    public class Spielsystem {
     
        Spaceship spaceship;
        CPUShip cpuship;
     
        // Konstruktor
        public static void main(String[] args) {
            Spielsystem Schiff = new Spielsystem();
            while ( !ende() ) {
               Spielstand(); 
               Spielrunde();
            }
        }
     
        //ohne void
        public Spielsystem () {
            spaceship = new Spaceship(5000, 50, 200, 120, 50);
            cpuship = new CPUShip (5000, 50, 200, 120, 50);
     
        }
     
        // Aufrufen der Spaceship-Klasse, 1.Aktion des Spielers, 2.Aktion von CPU (Random)
        public void Spielstand() {
            System.out.println("Verfügbares Geld: " + spaceship.getMoney());
            System.out.println("Besatzungsmitglieder: " + spaceship.getPeople());
            System.out.println("Vorhandene Nahrung: " + spaceship.getFood());
            System.out.println("Treibstoff: " + spaceship.getFuel());
            System.out.println("geladene Ladung: " + spaceship.getLadung());
     
            System.out.println("--------------------------------------------");
     
            System.out.println("CPU Verfügbares Geld: " + cpuship.getMoney());
            System.out.println("CPU Besatzungsmitglieder: " + cpuship.getPeople());
            System.out.println("CPU Vorhandene Nahrung: " + cpuship.getFood());
            System.out.println("CPU Treibstoff: " + cpuship.getFuel());
            System.out.println("CPU geladene Ladung: " + cpuship.getLadung());
        }
     
        public void Spielrunde() {
            //Führt Runde des Spielers mit Klasse "Spaceship" durch, danach "Random"-Events der "CPU"
            spaceship.spielzug();
            cpuship.spielzug();
        }
        //boolean
        public boolean ende() {
            // CPU-Schiff und SpielerSchiff checken
            return (spaceship.leer() || cpuship.leer());
        }
        //String[] args
        //static void
     
    }
    ------------------------------


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Programm does not start. Help Please!

    I have problems with my main class
    Please explain. If you get error messages, copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Programm does not start. Help Please!

    Thanks! will do that!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Programm does not start. Help Please!

    "non-static method ende() cannot be referenced from a static context"
    The non-static method ende() requires an instance of the class to reference it.
    referenceToClassObject.theMethod();

    Just like you did on this line:
      cpuship.spielzug();
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Programm does not start. Help Please!

    Do I have to put this in the method "ende" itself?
    Which instances should I put in there?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Programm does not start. Help Please!

    What class is ende() in? Does the code create an instance of that class (by using the new statement)?

    You should look at the tutorial to see how to use methods:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    http://docs.oracle.com/javase/tutori...arguments.html
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Programm does not start. Help Please!

    ende() is in the same class as the "Error"-Main Class. The Code creates a "new Spielsystem" of that class.
    Where should I enter the data? I assume it is
     
     
     public boolean ende() {
            // CPU-Schiff und SpielerSchiff checken
            Spielsystem.Spielstand();
            Spielsystem.Spielrunde();
            return (spaceship.leer() || cpuship.leer());

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Programm does not start. Help Please!

    Where should I enter the data?
    What data are you asking about?

    Have you fixed the error:
    "non-static method ende() cannot be referenced from a static context"

    See this for more info on static methods:
    http://docs.oracle.com/javase/tutori...classvars.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Programm does not start. Help Please!

    Quote Originally Posted by Norm View Post
    The non-static method ende() requires an instance of the class to reference it.
    referenceToClassObject.theMethod();
    How can I programm this? Didn't I created a new instance with "new Spielsystem();"?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Programm does not start. Help Please!

    If you have a reference to the class that the method is in, you can now call that method like this:
    theReferenceToTheClass.theMethod()
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Programm does not start. Help Please!

    The Method "Ende" is in the Same class in which there is the "main" method, that caused the syntax error.
    If I write
     public static void main(String[] args) {
            Spielsystem Schiff = new Spielsystem();
            while ( !Spielsystem.ende() ) {
               Spielsystem.Spielstand(); 
               Spielsystem.Spielrunde();
            }
        }

    like you told me, the error still occurs.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Programm does not start. Help Please!

    The code should use the reference to the instance of the class: Schiff
    not the name of the class: Spielsystem

    theReferenceToTheClass.theMethod()


    BTW variable names should start with lowercase: schiff
    Class names start with uppercase.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Client-Server programm creation
    By MustSeeMelons in forum Java Networking
    Replies: 16
    Last Post: December 11th, 2012, 04:39 PM
  2. Creating a programm
    By TheBoss84 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 23rd, 2011, 02:36 PM
  3. Can't run Game of Life programm
    By cutekill0 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 13th, 2011, 08:30 AM
  4. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM
  5. Replies: 1
    Last Post: May 3rd, 2010, 01:03 PM