JAVA - non-static method add(E) cannot be referenced from a static context

Kirsten de Wit

I am trying to insert String values (using a Scanner) into an ArrayList.

I am getting the error:

non-static method add(E) cannot be referenced from a static context.

I assume the add() method is the problem - what other methods can I use to insert into an ArrayList that is static?

import java.util.*;

public class Solution {

    private static List<String> strings;

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);

        for(int i = 0; i < 5; i++){
            String s = scanner.nextLine();
            List.add(s);
        }
    }
}
Ihor Patsian

You try to add a string to List interface (add() method is not static) but should add it to your instance List<String> strings:

strings.add(s);

Also, you should initialize strings

private static List<String> strings = new ArrayList<>();

otherwise, you will get NullPointerException.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

non static method cannot be referenced from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

non-static variable cannot be referenced from a static context java

From Java

java - Non-static method 'getLogger' cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context when flattening map java 8

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Dev

Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context - Java to Kotlin - Android

From Dev

Java non-static method cannot be refenced from a static context

From Dev

Error:non static method 'edit' cannot be referenced in static context

From Dev

Trying to get my show-method to work (Non static method cannot be referenced from static context)

From Dev

non-static variable s cannot be referenced from a static context

From Dev

non static variable this cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Java

non-static class cannot be referenced from a static context

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

non static setGravity cannot be referenced from static context

From Dev

Non-static variable cannot be referenced from a static context?

From Java

Java - cannot be referenced from a static context

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

Related Related

  1. 1

    non static method cannot be referenced from a static context

  2. 2

    non-static method getIntent() cannot be referenced from a static context

  3. 3

    "Non-static method cannot be referenced from static context" error

  4. 4

    Non-static method getSocketFactory cannot be referenced from a static context

  5. 5

    java: non-static variable this cannot be referenced from a static context

  6. 6

    Non static variable cannot be referenced from static context java

  7. 7

    non-static variable cannot be referenced from static context [JAVA]

  8. 8

    non-static variable cannot be referenced from a static context java

  9. 9

    java - Non-static method 'getLogger' cannot be referenced from a static context

  10. 10

    Non-static method cannot be referenced from a static context when flattening map java 8

  11. 11

    Non-static method cannot be referenced from a static context in java 8 streams

  12. 12

    Non-static method cannot be referenced from a static context in java 8 streams

  13. 13

    Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

  14. 14

    Non-static method cannot be referenced from a static context - Java to Kotlin - Android

  15. 15

    Java non-static method cannot be refenced from a static context

  16. 16

    Error:non static method 'edit' cannot be referenced in static context

  17. 17

    Trying to get my show-method to work (Non static method cannot be referenced from static context)

  18. 18

    non-static variable s cannot be referenced from a static context

  19. 19

    non static variable this cannot be referenced from a static context

  20. 20

    "non-static variable this cannot be referenced from a static context"?

  21. 21

    non-static class cannot be referenced from a static context

  22. 22

    Non-static variable filepath cannot be referenced from a static context

  23. 23

    Non-static edit() cannot be referenced from a static context

  24. 24

    non static setGravity cannot be referenced from static context

  25. 25

    Non-static variable cannot be referenced from a static context?

  26. 26

    Java - cannot be referenced from a static context

  27. 27

    Map.merge .. non-static method cannot be referenced from static context

  28. 28

    non-static method matcher(CharSequence) cannot be referenced from a static context

  29. 29

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

HotTag

Archive