开发者

sharing variables between classes

开发者 https://www.devze.com 2023-03-02 20:37 出处:网络
I have a problem that sounds stupid but don\'t know why it does not work. I have 2 classes, Directory and Map, and inside the Directory class there is a local String variable that takes the name of th

I have a problem that sounds stupid but don't know why it does not work. I have 2 classes, Directory and Map, and inside the Directory class there is a local String variable that takes the name of the current folder of the directory and want to put it inside a rectangle in the Map class that displays the graphics. After putting the variable into the Map class, the problem is that the string is empty.

Here is the code :

public class Directory
{
    public static File directory;   // the directory that we want to use
    public static String dirName = directory.getName();
    public String test = "test";

    public Directory(File directory)
    {
        files = directory.listFiles();
    }    
}

public class Test extends JComponent
{
    Directory dir = new Directory(null);
    public Test()
    {
        JFrame frame = new JFrame("Test");
        frame.setSize(800,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(this);
        frame.setVisible(true);

    }

    public void paintCom开发者_运维百科ponent(Graphics g)
    {
        super.paintComponents(g);

        g.setColor(Color.yellow);
        g.fillRect(10, 10, 100, 100);
        g.drawString(dir.tigka, 10, 20);
    }
}


You instantiated a Directory object with (null), and then used that instance to set the String variable. Makes perfect sense that the null passes though.

Directory dir = new Directory(null);
String folderName = dir.folderName;


This is a new solution I discovered (I'm a beginner) for maintaining one central, dynamic variable location for all classes in an application, regardless of how many times the same variables are used or where or when they are used. It sounds like it might apply to a solution?

https://stackoverflow.com/questions/17861704/solution-for-sharing-variables-between-classes?noredirect=1#comment26078064_17861704


weird question and hard to evaluate the answers, as the target is changing so much .. nevertheless, can't resist throwing in my couple of cents (Euro!) as well because I think the reason for the NPE is not yet spotted

The code I'm referring to is (in case it's changing while I'm writing)

public /* static */ class Directory
{
    public static File directory;   // the directory that we want to use
    public static String dirName = directory.getName();
    public String test = "test";

    public Directory(File directory)
    {
        files = directory.listFiles();
    }    
}    

instantiating with a null file looks like the culprit ... but is never reached: the first NPE thrown comes from the static initialization of dirName which accesses the static field directory which is .. well, null

Edit (as of @Eristikos comment): removed the static class modifier to stay in synch with the original example.

0

精彩评论

暂无评论...
验证码 换一张
取 消