简体   繁体   English

线程“主”中的异常java.lang.NullPointerException-不明白为什么?

[英]Exception in thread “main” java.lang.NullPointerException - don't understand why?

I'm still a beginner with Java and writing a student record programme at the moment, and im stuck with these errors, and can't work out why. 我仍然是Java的初学者,目前正在编写一个学生记录程序,并且陷入了这些错误,无法找出原因。

Exception in thread "main" java.lang.NullPointerException
at Results.Results(Results.java:11)
at Main.main(Main.java:21)

This is where the error is in my main class, on line 21 (second line below). 这是我的主类错误所在的位置,在第21行(下面的第二行)。

public class Main 
{
    public static void main(String[] args) 
    {

        StudentNames nameInputobject = new StudentNames();
        nameInputobject.StudentNames();


        Subject subjectInputobject = new Subject();
        subjectInputobject.Subject();

        StudentNumber numberInputobject = new StudentNumber();
        numberInputobject.StudentNumber();      

        StudentYear yearInputobject = new StudentYear();
        yearInputobject.StudentYear();  

        Results resultsInputobject = new Results();
        resultsInputobject.Results();   // <<<error here




    }
}    

and the error in my results class 和我的结果类中的错误

    import java.util.ArrayList;
import java.util.Scanner;

public class Results 
{
    public static void Results()
    {

        StudentYear obj=new StudentYear();
        ArrayList<Integer> studentYear=obj.getStudentYear();
        int year=studentYear.get(0); // <<<<error here 
        ArrayList<Integer> results = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in); 
        System.out.println("Please enter the results for "+year+":");
        results.add(sc.nextInt());
        System.out.println(results);

        }
    }

the results class gets the studentYear value from an arraylist in another class code here. 结果类从另一个类代码中的数组列表获取studentYear值。

import java.util.ArrayList;
import java.util.Scanner;

public class StudentYear 
{
    private ArrayList<Integer> studentYear;
    public void StudentYear()
    {
        studentYear = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in); 
        System.out.println("Please enter the year the student has most recently completed: ");
        studentYear.add(sc.nextInt());
        System.out.println(studentYear);
        }
        public ArrayList<Integer> getStudentYear()
        {
            return studentYear;
            }
}

And i'm using Arraylists as i plan on making the programme loop in the end to hold multiple student values 我正在使用Arraylists,因为我计划最终使程序循环以容纳多个学生值

Note that the studentYear variable is an instance of ArrayList<Integer> . 请注意, studentYear变量是ArrayList<Integer>的实例。 This means it's a collection of the object Integer . 这意味着它是对象Integer的集合。 This is a "boxed" wrapper around the primitive data type int . 这是围绕原始数据类型int的“盒装”包装。

An Integer object can be null, however an int cannot. Integer对象可以为null,但int不能为null。

Given you have not added anything to this ArrayList, the the element at position zero will be null. 如果您尚未向此ArrayList添加任何内容,则零位置的元素将为null。 When the VM attempts to convert this null instance of Integer to the primtive int it is throwing a NullPointerException . 当VM尝试将此Integer null实例转换为原始int它将引发NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不起作用@Autowired注解线程“主”中的异常java.lang.NullPointerException - Don't work @Autowired annotation Exception in thread “main” java.lang.NullPointerException , 线程“main”中的异常 java.lang.NullPointerException - ,Exception in thread "main" java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException “线程“ main”中的异常java.lang.NullPointerException” - “Exception in thread ”main“ java.lang.NullPointerException” 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“main”中的异常 java.lang.NullPointerException 5 - Exception in thread "main" java.lang.NullPointerException 5 为什么我在Java中有此异常:线程“ main”中的异常java.lang.NullPointerException - why i have this Exception in java : Exception in thread “main” java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException [java] - Exception in thread “main” java.lang.NullPointerException [java] Java:线程“main”java.lang.NullPointerException中的异常 - Java: Exception in thread “main” java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM