0

at the moment I am just doing some practise in java:

Main:

import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;
import settelersofcatan.playerinformation;

public class settlersofcatanhelper 
{
     private ArrayList<playerinformation> PlayerArray = new ArrayList<playerinformation>();
     public static void main(String[] args) 
     {
         String NumberOfPlayersString = JOptionPane.showInputDialog("How many player");
         int NumberOfPlayers = Integer.parseInt(NumberOfPlayersString);


         for (int Counter = 0; Counter < NumberOfPlayers; Counter++)
         {
             String Name = JOptionPane.showInputDialog("Name of player " + Counter);
             String Colour = JOptionPane.showInputDialog("which colour are you playing with");
             playerinformation PlayerDetails = new playerinformation(Name, Colour, Counter);
             addplayer(PlayerDetails);
         }

     }

    public void addplayer(playerinformation player)
    {
    this.PlayerArray.add(player);
    }
}

Helper Class:

package settelersofcatan;

public class playerinformation {
    String PlayerName;
    String Colour;
    int Position;

    public playerinformation(String name, String colour, int position) 
    {
        setPlayerName(name);
        setColour(colour);
        setPosition(position);

    }

    public String getPlayerName() {
        return PlayerName;
    }
    public void setPlayerName(String playerName) {
        PlayerName = playerName;
    }
    public String getColour() {
        return Colour;
    }
    public void setColour(String colour) {
        Colour = colour;
    }
    public int getPosition() {
        return Position;
    }
    public void setPosition(int position) {
        Position = position;
    }
}

The Problem that I am having now is that I am trying to add a player's information and get the response "Cannot make a static reference to the non-static method addplayer(playerinformation) from the type settlersofcatanhelper". I have looked over the code where I have this working but cant see any difference or reason why it wouldnt work. Can anyone help me?

Outlander
  • 3
  • 3
  • `public void addplayer(playerinformation player)` is non-static, but you're calling it from `main` (which is `static`). Read the duplicate, and/or information on `static` in Java. – achAmháin May 09 '18 at 16:04
  • 1
    @notyou Will give it a read and see if it helps me – Outlander May 09 '18 at 16:05
  • @notyou, I understand that but how can I make it work then, do I have to make seperate method for it? – Outlander May 09 '18 at 16:06
  • You must understand what static means to begin with. It basically means that it's independent and is not relied on the class's state at any point. By having a static method referencing a property in the class and interfering with it essentially makes it reliant on the state of the class and is therefor not a valid static method and will thus fail. – Jonast92 May 09 '18 at 16:09
  • At a glance you could make `addplayer` static, but then you'd need to make `PlayerArray` static too, which might not be what you want. Oh, and `this` probably will give the same error too, in `addplayer`, so you can use `settlersofcatanhelper.PlayerArray.add(player);`. But I would still read up on `static` and figure it out. Also, please look up Java naming conventions (class begins with capital letter, variables are camelCase etc). – achAmháin May 09 '18 at 16:11
  • sorry @notyou, forgot that stackoverflow hides comments when the replies go over a certain limit, so only saw your comment now – Outlander May 09 '18 at 16:26
  • @notyou, upon using your solution it gives the error twice since I think that settelers of catan is also static – Outlander May 09 '18 at 16:28

3 Answers3

0

You are calling a non-static method(addplayer) from a static one(main). You can fix it, just make addplayer static.

kibitzer
  • 1
  • 2
0

The main method is static; the addplayer method is an instance method. Therefore you need an instance of settlersofcatanhelper in order to call addplayer on it.

Josh
  • 500
  • 3
  • 6
  • so would I have to make one main class that calls it, to make it non-static? – Outlander May 09 '18 at 16:23
  • No, You can do it within the same class, but it is really less confusing to make a separate one. Also it is a better practice, as this gives each class a single responsibility. – Jan Ossowski May 09 '18 at 16:40
0

You need to understand what the word "static" means. You can look here among other places: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html (documentation) https://www.javatpoint.com/static-keyword-in-java (easy to read tutorial)

If you apply static keyword with any method, it is known as static method. A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. static method can access static data member and can change the value of it.

So, You cannot call a non-static (object) method from a static (class) method. You need to create an object and call the method on the object reference:

settlersofcatanhelper a = new settlersofcatanhelper();
a.addPlayer(PlayerDetails);

or change the method to a static one if that's what You want to do (not for me to judge, depends on what You're trying to achieve), but keep in mind this variable cannot be called from static content (the list itself would also have to be static).

private static ArrayList<playerinformation> PlayerArray = new ArrayList<playerinformation>();

[...]

public static void addplayer(playerinformation player) {
    this.PlayerArray.add(player);
}

" Also (not entirely on the topic), I suggest You read about naming conventions in java :)

edit:

My idea to try and get a grasp of it is to create a separate class (call it "Test" or whatever) with the public static void main(String[] args) method (and nothing else), and start from there. The difference between static and non-static members may be a little bit confusing in the beginning, and having a separate place for your "logic" may be helpful to distinguish what you can and cannot use from static content.

then your main method could look something like:

public static void main(String[] args) {
    settlersofcatanhelper object = new settlersofcatanhelper();
    // do what you want with your object (which is an instance of settlersofcatanhelper class)
}
Jan Ossowski
  • 328
  • 1
  • 2
  • 18
  • thanks for the input. I am just not entirely sure how to fix my program in particular. I created an object at the start of my public class settlersofcatanhelper and i still get the error, I dont really know where to go from there? – Outlander May 09 '18 at 16:17
  • and yes, I will read up on the Java naming conventions, but I first wanted to get the program working before I made it neat. – Outlander May 09 '18 at 16:18
  • @Outlander I gave a solution in my comment under your post, which will remove the errors, but be very sure it's what you want regarding `static` methods and variables. – achAmháin May 09 '18 at 16:19
  • @notyou, I do understand what you meant, but I just don't understand how I could then make the program work the way I wanted, instead of just removing the error – Outlander May 09 '18 at 16:22
  • @Outlander the problem here is that You in fact never create any instance of settlersofcatanhelper - I think some reading on a difference between a class and an object is the way to go https://www.ncl.ucar.edu/Document/HLUs/User_Guide/classes/classoview.shtml The problem here is that You are trying to use an object method from within a class method. – Jan Ossowski May 09 '18 at 16:26