简体   繁体   中英

Java, null point exception with split()

Im getting a nullpointerexcpetion when I run this. it allows me to enter all the input fields, but than throws the null pointer. Can someone explain to me why it is doing this. I think it has something to do with the split(). There are three classes, below. Any help would be great. Essentially i want to take the input, split it into an array, and call display the string at a specific index.

//Main Class
public class StarWars {
    public static void main(String[] args) {
        getInput input = new getInput();
        splitInput myInput = new splitInput();
        input.firstName();
        input.lastName();
        input.maidenName();
        input.cityName();

        System.out.println(myInput.splitFirstName());

    }
}

//second class
import java.util.Scanner;


public class getInput {

    public String firstName;
    private String lastName;
    private String maidenName;
    private String cityName;

    Scanner user_input = new Scanner(System.in);

    public void firstName(){    
        System.out.printf("Enter your First Name:");
        firstName = user_input.next().trim().toLowerCase();
    }

    public String getFirstName(){
        return this.firstName;
    }

    public void lastName(){
        System.out.printf("Enter your Last Name:");
        lastName = user_input.next().trim().toLowerCase();
    }

    public String getLastName(){
        return this.lastName;
    }

    public void maidenName(){
        System.out.printf("Enter your Mom's Maiden Name:");
        maidenName = user_input.next().trim().toLowerCase();
    }

    public String getMaidenName(){
        return this.maidenName;
    }

    public void cityName(){
        System.out.printf("Enter your birth City:");
        cityName = user_input.next().trim().toLowerCase();
    }

    public String getCityName(){
        return this.cityName;
    }
}

//Third Class
public class splitInput {
    public String splitFirstName(){
        getInput input = new getInput();
        String x = input.getFirstName();
        String[] splitValue = x.split("");
        String myValue = splitValue[2];
        return myValue;
    }
}

For those commenting who are curious, the Exception indeed happened on this line, because x was null .

  String[] splitValue = x.split("");

The problem is that you made all the calls to get data on the myInput object in main , but you did not pass it into splitFirstName . Instead, you created a completely new input object.

Change the method splitFirstName to take input as an argument.

//Third Class
public class splitInput {

    public String splitFirstName(getInput input){

        String x = input.getFirstName();

        System.out.println(x);
        String[] splitValue = x.split("");

        String myValue = splitValue[2];

        return myValue;

    }
}

Pass the argument in from main:

System.out.println(myInput.splitFirstName(input));

Note that your code as is, will print out the first third character of the first name. If that is not your intent, you should probably change it.

Also, the Java convention is to name classes with an uppercase first letter, and to use nouns rather than verbs to name classes.

When you invoke this statement in main class

System.out.println(myInput.splitFirstName());

you are calling splitFirstName() method and within it you are creating a new instance getInput input = new getInput(); and from the new instance you are trying to get its properties which are Null .. String x = input.getFirstName(); here x value is null.

To solve this

Pass getInput object to splitFirstName method to System.out.println(myInput.splitFirstName(input)); and change your method signature to public String splitFirstName(getInput input){ and Comment the new instance statement

Below i have modified your code which is working::;

package com.test.punith;

import java.util.Scanner;

public class StackTest {

public static void main(String[] args) {

    getInput input = new getInput();
    splitInput myInput = new splitInput();

    input.firstName();
    input.lastName();
    input.maidenName();
    input.cityName();

    System.out.println(myInput.splitFirstName(input));

}

}

class getInput {

public String firstName;
private String lastName;
private String maidenName;
private String cityName;

Scanner user_input = new Scanner(System.in);

public void firstName(){    
    System.out.printf("Enter your First Name:");
    firstName = user_input.next().trim().toLowerCase();

}

public String getFirstName(){
    return this.firstName;
}

public void lastName(){

    System.out.printf("Enter your Last Name:");
    lastName = user_input.next().trim().toLowerCase();
}

public String getLastName(){
    return this.lastName;
}

public void maidenName(){

    System.out.printf("Enter your Mom's Maiden Name:");
    maidenName = user_input.next().trim().toLowerCase();
}

public String getMaidenName(){
    return this.maidenName;
}

public void cityName(){

    System.out.printf("Enter your birth City:");
    cityName = user_input.next().trim().toLowerCase();
}

public String getCityName(){
    return this.cityName;
}

}

//Third Class class splitInput {

public String splitFirstName(getInput input){

    //getInput input = new getInput();

    String x = input.getFirstName();

    String[] splitValue = x.split("");

    String myValue = splitValue[2];

    return myValue;

}

}

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