0

Alright, so I have a driver class and an arraylist (as you can probably guess).

My arraylist is full of strings and I'm accessing it through a getter that I've created in my other class.

It says that "The static method getMouseList() from the type Desktop should be accessed in a static way". How should I go about fixing this? Do you see any other errors?

Thanks to everyone who can help!

So here's my code:

Class:

    static ArrayList <String> Strings;


public static void main(String[] args) {
  Strings = new ArrayList <String>(); 
        Strings.add("goodbye");
        Strings.add("hi");
        Strings.add("hello");
}

    public static ArrayList<String> getStrings() {
        return Strings;
    }

Driver class:

Desktop test4 = new Desktop(); 
System.out.println(test4.getStrings()); 

1 Answers1

3

Since it's static, it belongs to its class, not to a class instance. Thus, you need to access it using the class: Desktop.getStrings()

UPDATE I'd need to see more of your code to exactly explain what's going on. So, I'll try to explain on what I imagine is your code. Please note that Java programs start from one main method. Depending on how you execute your program, one of them is going to be invoked, the others will not.

In any case, one way would be to create another static method in Desktop and move the initialization code from main to that method, such as:

public static void initializeStrings(){
    Strings = new ArrayList <String>(); 
    Strings.add("goodbye");
    Strings.add("hi");
    Strings.add("hello");
}

Then, make sure that you call that method, via Desktop.initializeStings(); before trying to access the Strings array (for example in the start of the actual main method of your program.

EDIT 2: OK, so your main should now look something like this:

public static void main(String[] args) {
    Desktop.initializeStrings();
    Driver.doSomething();
}

and your Driver's method (inside Driver class):

public static void doSomething() {
    //some code

    //strings are initialized and will get printed
    System.out.println(Desktop.getStrings());
}

That should give you a hint on how to move forward. On a side note, all this staticness is hindering the Object oriented nature of Java. I would suggest a tutorial in Java's object oriented programming model: https://docs.oracle.com/javase/tutorial/java/index.html

JChrist
  • 1,734
  • 17
  • 23
  • The error is gone, but it still returns null. Any suggestions? – Hugh Notman Apr 03 '16 at 18:16
  • 2
    Then this means that it hasn't been initialized yet. In the code sample you provided, the initialization happens in the main method. However, I guess that the code you're running is most probably using another main method and not the one in the example, thus the Strings array is never initialized. – JChrist Apr 03 '16 at 18:19
  • That makes sense, but I just don't know how to do that because I'm a newbie. Sorry, but can I get a little extra help. I'm adding stuff to the arraylist in my main and trying to call it in my driver. How should I change this? – Hugh Notman Apr 03 '16 at 18:35