0

I'am getting a

java.lang.NullPointerException

when trying to use regex in the following code:

public static void scanSTAFFELNR() {

    String STAFFELNR = CsvScanner.values.get(1);  // My values are stored in a ArrayList ( values). Here i want to get the second one.

    if (STAFFELNR.matches("[0-9]+")) { // Here i want to check, if the String is a number. 
        System.out.println("erfolg");
    } else {
        STAFFELNRMessage = "Fehler bei STAFFELNR, STAFFELNR muss zwischen 0-3 sein";
        Gui.fehlerCounter++;
    }
}

When i remove the STAFFELNR.matches and replace it with a STAFFELNR == "Hello" , i get no error.

I dont know how to fix this error on my code.

  • Then presumably your `STAFFELNR` is `null`. Also don't ever compare strings with `==`. You use `equals()` for object comparisons. – Kayaman Apr 03 '18 at 12:47
  • i dont get why it is null. As soon as i created the String, i gave it a value. –  Apr 03 '18 at 12:49
  • A `null` value based on the behaviour of your code. Did you put nulls in your arraylist? – Kayaman Apr 03 '18 at 12:50
  • My ArrayList was created in a different Class and filled, when a button was pressed. –  Apr 03 '18 at 12:53
  • That's probably the root of your problem. `CsvScanner.values.get(1);` returns `null`, so you're not putting things in the `ArrayList` correctly. You are putting nulls in there, because otherwise you'd get a different exception. – Kayaman Apr 03 '18 at 12:55
  • But when i system.print.out(values.get(1)) i get the number i want.. –  Apr 03 '18 at 12:58
  • 1
    What about when you do `System.out.println(STAFFELNR);`? – Kayaman Apr 03 '18 at 12:58
  • Nevermind. i got it working..than you=) –  Apr 03 '18 at 13:03

0 Answers0