我正在阅读 OCA Java SE 7 学习指南,并且正在阅读软件包。但是,我在 Eclipse 中输入了在书中找到的相同代码,但我收到了这个错误。错误是
com.ocaj.exam.tutorial.MainClass.main(MainClass.java:12) 处的线程“main”java.lang.NullPointerException 中的异常
这是我的代码...
package com.ocaj.exam.tutorial; //Package statement
//Imports class ArrayList from the java.util package
import java.util.ArrayList;
//Imports all classes from the java.io package
import java.io.*;
public class MainClass {
public static void main(String[] args) {
//Creates console from java.io package
Console console = System.console();
String planet = console.readLine("\nEnter your favourite planet: ");
//Creates list for planets
ArrayList planetList = new ArrayList();
planetList.add(planet); //Adds users input into the list
planetList.add("Gliese 581 c"); //Adds a string to the list
System.out.println("\nTwo cool planets: " + planetList);
}
}
非常感谢