0

I am getting a Exception in Thread main

java.lang.NoSuchMethodException: com.laurens.Main.main([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1786)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)

Can someone explain where I am going wrong here?

Main

   package com.laurens;
   public class Main {
   private player player;

     public Main(com.laurens.player player) {
    this.player = player;
     }

public com.laurens.player getPlayer() {
    return player;
      }

public void setPlayer (int performance, String name) {
    if (performance < 4) {

        boolean injured = true;

    }

}

@Override
public String toString() {
    return "com.laurens.Main{" +
            "player=" + player +
            '}';
}
 }

player

 package com.laurens;

/**
* Created by laurensvanoorschot on 20-01-16.
 */
  public class player {
  private String name;
   private int performance;
  private boolean injured;

public player(int performance, boolean injured, String name) {
    this.injured = injured;
    this.name = name;
    this.performance = performance;
}

public boolean isInjured() {
    return injured;
}

public void setInjured(boolean injured) {
    this.injured = injured;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPerformance() {
    return performance;
}

public void setPerformance(int performance) {
    this.performance = performance;
    }
   }
6
  • 1
    where is the main() method ? Commented Jan 21, 2016 at 10:34
  • Class Main is the main method, but that shouldn't prevent complier from running the code. Commented Jan 21, 2016 at 10:39
  • probably an intellij issue. You may want to check stackoverflow.com/a/24142199/3215527 Commented Jan 21, 2016 at 10:42
  • @LaurensVanOorschot, you didn't post the code of your main() method, the main where you run your code. Commented Jan 21, 2016 at 10:47
  • That's not a compiler error. Commented Jan 21, 2016 at 11:12

1 Answer 1

3

You don't have a method called main, which is what it is looking for to run your program. Notice that when you create a template application for a java console application in intelliJ it has a method:

public static void main(string[] args) { 

}

That needs to be there for your program to run.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, but now the code doesn't do anything. How do I execute the toString method so it read the player object?
Just call the method i.e toString(), just within the main function. The main function is where the code will be executed.
Tried it, but getting Error:(5, 9) java: non-static method toString() cannot be referenced from a static context
Make the methods static, it's pretty much what it says :) You can't call non-static code from a static method (the main method is static)
But the toString needs to be static else it won't be able to return anything. How do I change it to now static?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.