以下是我写的一个简单的程序,编译时没问题,运行时却抛出异常
import java.util.Scanner;
public class Course
{ /**
 * @param args
 */
public String course;
public String[] student = new String[100];
public int count;
public void getcourse()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("输入课程名称");
course=keyboard.nextLine();
}
public void getstudent()
{     
       Scanner keyboard = new Scanner(System.in);
       System.out.println("输入选修课程"+course+"的学生姓名,输入1结束");
       boolean flat=true;       
       while(flat)
       {
        int i=0;
        count=0;
        student[i]=new String();
        student[i++]=keyboard.nextLine();
        count++;
        if(student[i].equals("1"))
        flat=false;
       }        
   } 
public void print()
{
for(int i=0;i<count;i++)
       {
        System.out.println("选该课程"+course+"的学生:"+student[i]+" ");
       }
}
public static void main(String[] args)
{
// TODO 自动生成方法存根
Course course1=new Course();
course1.getcourse();
course1.getstudent();
course1.print();
Course course2=new Course();
course2.getcourse();
course2.getstudent();
course2.print();
}
}错误:
Exception in thread "main" java.lang.NullPointerException
at Course.getstudent(Course.java:29)
at Course.main(Course.java:45)麻烦高手指教

解决方案 »

  1.   

    student[i++]=keyboard.nextLine(); 
    改成下面这个~   是不是你要的?
    student[++i] = keyboard.nextLine();
      

  2.   

    getstudent代码换成下面那样就行了。
    public void getstudent() {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("输入选修课程" + course + "的学生姓名,输入1结束");
    boolean flat = true;
    int i = 0;
    while (keyboard.hasNext()) {
    String a=keyboard.next();
    if (!a.equals("1")){
    student[i]=a;
    i++;
    count++;
    }else{
    i=0;
    break;
    }
    }
    }
      

  3.   

    以下摘自 Java API
    public class NullPointerExceptionextends RuntimeException当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括: 调用 null 对象的实例方法。 
    访问或修改 null 对象的字段。 
    将 null 作为一个数组,获得其长度。 
    将 null 作为一个数组,访问或修改其时间片。 
    将 null 作为 Throwable 值抛出。 
    应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用。
    出现这个异常,说明了将null对象赋值,即nextLine()读到的为空,所以应该先检查,同楼上
      

  4.   

    NullPointerException是运行时异常,编译自然能通过了
    你那个student[i].equals("1")有问题,你可以试着打印出student[i],估计是null吧
      

  5.   

    import java.util.Scanner;public class TestCourse { public String course;
    public String[] student = new String[100];
    public int count; public void getcourse() {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("输入课程名称: ");
    course = keyboard.nextLine();
    } public void getstudent() {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("输入选修课程 : '" + course + "'的学生姓名,输入1结束");
    boolean flat = true;
    int i = 0;
    while (flat) {
    //student[i] = new String();
    student[i] = keyboard.nextLine();
    count++;
    if (student[i++].equals("1"))
    flat = false;
    }
    }

    public void print() {
    for (int i = 0; i < count - 1; i++) {

    System.out.println("选该课程" + course + "的学生:" + student[i] + " ");
    }
    } public static void main(String[] args) {
    TestCourse course1 = new TestCourse();
    course1.getcourse();
    course1.getstudent();
    course1.print();
    // TestCourse course2 = new TestCourse();
    // course2.getcourse();
    // course2.getstudent();
    // course2.print();
    }}
    给你改了改,,,这样就OK了...
    int i = 0;和count应该放while外面
    要不每次调用都回0;
      

  6.   

    6楼说的很对,多谢了哈,马上给分
    int i = 0;和count确实应该放while外面
    我的是student[i++] = keyboard.nextLine();
                count++;
    if (student[i].equals("1"))
                flat = false;
    这样student[i]==NULL就会报NullPointerException