Exception in thread "main" java.lang.NullPointerException怎么解决

mimosazhao 2009-05-09 11:29:53
以下是我写的一个简单的程序,编译时没问题,运行时却抛出异常
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)

麻烦高手指教
...全文
276 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
myjavazonenichao 2009-05-10
  • 打赏
  • 举报
回复
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;
dsbjoe 2009-05-10
  • 打赏
  • 举报
回复
NullPointerException是运行时异常,编译自然能通过了
你那个student[i].equals("1")有问题,你可以试着打印出student[i],估计是null吧
wanglingzhong 2009-05-10
  • 打赏
  • 举报
回复
以下摘自 Java API
public class NullPointerExceptionextends RuntimeException当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:

调用 null 对象的实例方法。
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用。


出现这个异常,说明了将null对象赋值,即nextLine()读到的为空,所以应该先检查,同楼上
fredy1111 2009-05-10
  • 打赏
  • 举报
回复
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;
}
}
}
mimosazhao 2009-05-10
  • 打赏
  • 举报
回复
3楼的方法也对的,嗯,分有限,这样吧,你和6楼的平分吧
mimosazhao 2009-05-10
  • 打赏
  • 举报
回复
6楼说的很对,多谢了哈,马上给分
int i = 0;和count确实应该放while外面
我的是student[i++] = keyboard.nextLine();
count++;
if (student[i].equals("1"))
flat = false;
这样student[i]==NULL就会报NullPointerException
YHL27 2009-05-09
  • 打赏
  • 举报
回复
学习!
horizonlyhw 2009-05-09
  • 打赏
  • 举报
回复
student[i++]=keyboard.nextLine();
改成下面这个~ 是不是你要的?
student[++i] = keyboard.nextLine();

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧