返回null的最终print语句(java)

时间:2014-02-08 20:12:34

标签: java class parsing constructor

我有一个基本名称应用程序,它从主类中获取用户数据,在解析器类中拆分数据,然后尝试分配最终类中的所有内容并在toString方法中将其打印出来。我知道主类和解析器工作正常。我已经在解析器类中验证了数据是否正确分割,并且还通过我对最终类的对象发送数据以将其全部分配。但是,我的最终代码返回null ..

MAIN CLASS


            import java.util.Scanner;
            public class MainClass {
                public static void main (String[]args)
                {
                    Scanner input = new Scanner(System.in); //create scanner object to gather name information
                    String fullName = null; //set the predefined value for the users name to null
                    nameParse splitInformation = new nameParse(); //method build to split the name into different sections
                    SecondClass access = new SecondClass(); //class built to output the different name data

                    System.out.println("What is your name?");

                    fullName = input.nextLine(); //store the users name and pass it into the data parser

                    splitInformation.parseNameInformation(fullName); //name parsing parameters built

                    System.out.println(access.toString());





                }

            }

数据分析器类


            public class nameParse {
                private String firstName;
                private String middleName;
                private String lastName;

                public nameParse()
                {
                    firstName = "initial";
                    middleName = "initial";
                    lastName = "initial";
                }

                public void parseNameInformation(String inputInfo)
                {

                    //Create an array to store the data and split it into multiple sectors
                    String nameInformation[] = inputInfo.split("\\s");

                    firstName = nameInformation[0];
                    middleName = nameInformation[1];
                    lastName = nameInformation[2];

                    //System.out.println(firstName + " " + middleName + " " + lastName);

                    SecondClass sendData = new SecondClass();

                    sendData.setFirstName(firstName);
                    sendData.setMiddleName(middleName);
                    sendData.setLastName(lastName);


                }

            }

最后的课程 __

    public class SecondClass {

        private String firstName;
        private String middleName;
        private String lastName;


        /*public String GFN()
        {
            return firstName;
        }

        public String GMN()
        {
            return middleName;
        }

        public String GLN()
        {
            return lastName;
        }*/
        public String setFirstName(String yourFirstName)
        {
            firstName = yourFirstName;
            return this.firstName;
        }

        public String setMiddleName(String yourMiddleName)
        {
            middleName = yourMiddleName;
            return this.middleName;
        }

        public String setLastName(String yourLastName)
        {
            lastName = yourLastName;
            return this.lastName;
        }

        public String getFN()
        {
            return firstName;
        }

        public String toString()
        {
            String printNameInfo = "\nYour First Name:\t" + getFN();
            return printNameInfo;
        }

    }

1 个答案:

答案 0 :(得分:1)

您永远不会设置任何SecondClass对象(称为“访问”)字段,因此它们都将为空。

简而言之,您的代码会创建一个nameParse对象,从用户那里获取信息,但不会对该信息执行任何操作。您创建一个名为access的SecondClass对象,不向其中放入任何数据,因此当您尝试将其打印出来时,应该预期其中没有有效数据。解决方案:首先将信息放入SecondClass对象。调用它的setter方法:

// be sure to call the setter methods before trying to print anything out:
access.setSomething(something);
access.setSomethingElse(somethingElse);

修改
你说:

  

我以为我使用sendData.setFirstname(...)等设置了数据?

parseNameInformation方法中,您创建了一个新的SecondClass对象,并且您确实设置了此对象的字段,但此对象与主方法中的字段仍然为空的完全不同。要解决此问题,请为parseNameInformation提供一个方法参数,并将main方法的SecondClass对象传入其中并设置其方法。您必须在调用方法之前创建SecondClass对象

即,

public void parseNameInformation(String inputInfo, SecondClass sendData)
{
    //Create an array to store the data and split it into multiple sectors
    String nameInformation[] = inputInfo.split("\\s");

    firstName = nameInformation[0];
    middleName = nameInformation[1];
    lastName = nameInformation[2];

    //System.out.println(firstName + " " + middleName + " " + lastName);

    // SecondClass sendData = new SecondClass();  // !!! get rid of this

    sendData.setFirstName(firstName);
    sendData.setMiddleName(middleName);
    sendData.setLastName(lastName);
}