0

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());
    }
}
1
  • personalNumber has not been initialized so when you use the .subString() method, your compiler cannot perform the task, so it returns null. You should perform the .subString() function after the personalNumber has been entered. Commented Sep 18, 2015 at 23:58

1 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Okey I get what you mean. But what is personalNr for?
It's a argument to the constructor, so that you can do this in one line instead of two as you do: SwedishPersonalNumber myPersonalNumber = new SwedishPersonalNumber(); myPersonalNumber.personalNumber = scanPersonalNumber;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.