简体   繁体   中英

Integer.parseInt() Substring new string and make it Int

I get this

Exception in thread "main" java.lang.NullPointerException at SwedishPersonalNumber.(ScanPersonalNumber.java:5) at
ScanPersonalNumber.main(ScanPersonalNumber.java:54)

My struggle is returning the getYearBorn. But I dont know what Im doing wrong. I can solve getYearBorn with string but Its a exercise so I need it to be int.

import java.util.Scanner;     

class SwedishPersonalNumber {
    String personalNumber;
    String personalNumberBorn = personalNumber.substring(0, 4);
    String female = "Female";
    String male = "Male";
    //  boolean isValid = true;
    int yearBorn = Integer.parseInt(personalNumberBorn);

    int getYearBorn() {
        return yearBorn;
    }

    String getGender() {
        if (personalNumber.charAt(10)%2 == 0) {
            return female;
        }
        else 
            return male;
    }

    /*
    boolean isValid() {}
    */
}

public class ScanPersonalNumber {

    public static void main (String[] args) {
        String scanPersonalNumber;
        Scanner kb = new Scanner(System.in);

        System.out.println("Enter your personalnumber likes this: 196112033581 ");
        System.out.println("Now please enter your personalnumber");
        scanPersonalNumber = kb.nextLine();

        System.out.println("Your personalnumber is: "+ scanPersonalNumber);
        SwedishPersonalNumber myPersonalNumber = new SwedishPersonalNumber();
        myPersonalNumber.personalNumber = scanPersonalNumber;
        kb.close();

        System.out.println("Your birthyear is: "+ myPersonalNumber.getYearBorn());
        System.out.println("You are: "+ myPersonalNumber.getGender());
    }
}

The problem is that you're trying to initialize personalNumberBorn to early, logically what you're doing is correct, when you say that personalNumberBorn should be a substring of personalNumber but if you try to see it from the compilers point of view this is what will happen:

  String personalNumber; // 1) The compiler creates a empty (null) string.
  String personalNumberBorn = personalNumber.substring(0, 4); // 2) The compiler tries to take a substring of personalNumber which we know is empty (null)

The standard way of solving this is writing our own constructors, your code would look something like this:

//...      
String personalNumber;
String personalNumberBorn;
String female = "Female";
String male = "Male";
//    boolean isValid = true;
int yearBorn;

public SwedishPersonalNumber(String personalNr) { //Constructor
  personalNumber = personalNr;
  personalNumberBorn = personalNumber.substring(0, 4);
  yearBorn = Integer.parseInt(personalNumberBorn);
}

//...

And instead of calling like this:

SwedishPersonalNumber myPersonalNumber = new SwedishPersonalNumber();

you do it like this:

SwedishPersonalNumber myPersonalNumber = new SwedishPersonalNumber(scanPersonalNumber);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM