Search code examples
javaminecraftbukkit

When I call this method it doesn't work in java


When I call on the method "getName(companyName)" from the class Company, it doesn't send the user the name of the company from the yml, it just throws a lot of exceptions. However, when i put in the code that the method "getName(companyName)" returns into the sender.sendMessage, it does send the user the name of the company. Why is this?

Class where the method will be executed:

package me.ben.Corporation.commands;

import me.ben.Corporation.Company;
import me.ben.Corporation.Config;
import me.ben.Corporation.Corporation;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class CorporationCommand implements CommandExecutor
{
    Config companiesFile = Corporation.plugin.companiesFile;
    Company company;

    public boolean onCommand(CommandSender sender, Command cmd, String     cmdLabel, String[] args)
    {
        companiesFile.reload();

        if (cmdLabel.equalsIgnoreCase("corporation"))
        {
            if (args[0].equalsIgnoreCase("test"))
            {
                sender.sendMessage(company.getName(args[1]));
            }
        }
        return false;
    }
}

Class where the method "getName(companyName)" is:

package me.ben.Corporation;

public class Company
{
    Config companiesFile = Corporation.plugin.companiesFile;

    public String getName(String companyName)
    {
        companiesFile.reload();
        return companiesFile.getConfig().getString("companies." + companyName.toLowerCase() + ".name");
    }
}

The YAML File the plugin reads and writes:

    companies:
      simpleco:
        assets:
          cash: {}
          property: {}
        directors: {}
        expenses: {}
        liabilities: {}
        name: SimpleCo
        revenue: {}

Solution

  • Make your getName() method static.