Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.NullPointerException-我收到这个错误(新手)_Java_Scope - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.NullPointerException-我收到这个错误(新手)

“线程中的异常”;“主要”;java.lang.NullPointerException-我收到这个错误(新手),java,scope,Java,Scope,在该程序中,要求您输入S C或T,并再次要求Y继续,N结束。(在这里以前天气很好)。之后,如果输入A,则添加信息,否则P将打印信息。当我输入A零件时,它会给出此错误 Exception in thread "main" java.lang.NullPointerException at Assignment07.JavaMethod.makeChoiceThree(JavaMethod.java:84) at Assignment07.JavaMethod.makeChoiceTwo(JavaM

在该程序中,要求您输入S C或T,并再次要求Y继续,N结束。(在这里以前天气很好)。之后,如果输入A,则添加信息,否则P将打印信息。当我输入A零件时,它会给出此错误

Exception in thread "main" java.lang.NullPointerException
at Assignment07.JavaMethod.makeChoiceThree(JavaMethod.java:84)
at Assignment07.JavaMethod.makeChoiceTwo(JavaMethod.java:67)
at Assignment07.JavaMethod.makeChoice(JavaMethod.java:34)
at Assignment07.JavaMethod.main(JavaMethod.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

package Assignment07;

import java.util.Scanner;

/**
 * Main class
 */

`public class JavaMethod {

    Information info = new Information();
    Scanner console = new Scanner(System.in);
    Service service = new Service();


    public static void main(String[] args) {

        JavaMethod javaMethod = new JavaMethod();
        javaMethod.makeChoice();
    }


    //method to make choice on S,T or C
    public void makeChoice() {

        JavaMethod javaMethod = new JavaMethod();

        System.out.println("Enter S for Student, T for teacher and C for College");
        info.choice = console.nextLine();

        switch (info.choice) {
            case "S":

                javaMethod.makeChoiceTwo();
                break;

            case "T":

                javaMethod.makeChoiceTwo();
                break;

            case "C":

                javaMethod.makeChoiceTwo();
                break;

            default:
                System.out.println("Invalid!!! Please enter again.");
                javaMethod.makeChoice();

        }
    }

    //method to make choice Yes or No
    public void makeChoiceTwo() {

        //object of class JavaMethod
        JavaMethod javaMethod = new JavaMethod();


        System.out.println("Enter Y to continue and N to exit");
        info.yesNo = console.nextLine();

        switch (info.yesNo) {

            case "Y":
                javaMethod.makeChoiceThree();


            case "N":
                break;

        }
    }

    //method to make choice Add or Print
    public void makeChoiceThree() {


        System.out.println("Enter P to print or A to Add information");
        info.addPrint = console.nextLine();


        if (info.addPrint.equalsIgnoreCase("A") && info.choice.equalsIgnoreCase("S")) {

            service.setStudentInformation();

        } else if (info.addPrint.equalsIgnoreCase("A") && info.choice.equalsIgnoreCase("T")) {

            service.setTeacherInformation();

        } else if (info.addPrint.equalsIgnoreCase("A") && info.choice.equalsIgnoreCase("C")) {

            service.setCollegeInformation();
        } else if (info.addPrint.equalsIgnoreCase("P") && info.choice.equalsIgnoreCase("S")) {

            service.printStudentInformation();
        } else if (info.addPrint.equalsIgnoreCase("P") && info.choice.equalsIgnoreCase("T")) {

            service.printTeacherInformation();
        } else if (info.addPrint.equalsIgnoreCase("P")&& info.choice.equalsIgnoreCase("C")) {

            service.printCollegeInformation();
        } else {
            System.out.println("Invalid");
            makeChoiceThree();
        }

    }
}`





package Assignment07;

/**
 * I've init my variables here
 */

`public class Information {

    String choice;
    String yesNo;
    String addPrint;
}`





package Assignment07;

import java.util.Scanner;

/**
 * This is the service class which adds and prints information
 */

`public class Service {

    StudentInformation studentInformation = new StudentInformation();

    TeacherInformation teacherInformation = new TeacherInformation();

    CollegeInformation collegeInformation = new CollegeInformation();


    //method to get information
    public void setStudentInformation (){
         Scanner console = new Scanner(System.in);

         System.out.println("Enter the name of Student");
        studentInformation.name = console.nextLine();

        System.out.println("Enter the address of Student");
        studentInformation.address = console.nextLine();

        System.out.println("Enter the grade of Student");
        studentInformation.grade = console.nextLine();

        System.out.println("Enter the Roll Number of Student");
        studentInformation.rollno = console.nextLine();

        System.out.println("Enter the contact of Student");
        studentInformation.contact = console.nextLine();

   }

    //method to get information
    public void setTeacherInformation(){
        Scanner console = new Scanner(System.in);

        System.out.println("Enter the name of Teacher");
        teacherInformation.name = console.nextLine();

        System.out.println("Enter the address of Teacher");
        teacherInformation.address = console.nextLine();

        System.out.println("Enter the grade of Teacher");
        teacherInformation.subject = console.nextLine();

        System.out.println("Enter the contact of Teacher");
        teacherInformation.contact = console.nextLine();

    }

    //method to get information
    public void setCollegeInformation (){
        Scanner console = new Scanner(System.in);

        System.out.println("Enter the name of College");
        collegeInformation.name = console.nextLine();

        System.out.println("Enter the address of College");
        collegeInformation.address = console.nextLine();

        System.out.println("Enter the grade of College");
        collegeInformation.contact = console.nextLine();

    }

    //method to print information
    public void printStudentInformation (){
        System.out.println("Student Name:"+ studentInformation.name);
    }

    //method to print information
    public void printTeacherInformation (){
        System.out.println("Teacher Name:"+ teacherInformation.name);
    }

    //method to print information
    public void printCollegeInformation (){
        System.out.println("College Name:"+ collegeInformation.name);
    }
}`

为什么我会犯这样的错误?我怎样才能解决它

为什么要在方法中创建一个新的
JavaMethod
实例?异常发生在这一行<代码>if(info.addPrint.equalsIgnoreCase(“A”)和&info.choice.equalsIgnoreCase(“S”)){@turbo以访问同一类中的其他方法。@RyanCarlson为什么会发生这种情况?我如何解决它?我搜索了类似的问题,但自己无法将其发布。这不是必需的,请删除新对象的创建。我打赌
info.addprint
/
info.choice
为空,因为您正在创建一个新对象失去它。