以下是我写的一个简单的程序,编译时没问题,运行时却抛出异常
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)麻烦高手指教
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)麻烦高手指教
解决方案 »
- 还是Java全排列
- 问个简单问题...
- 小菜鸟 又提问了~~~`
- 请帮我解决一下字符串比较的问题
- 如何在java程序中动态显示调用进程(如"tail -f"命令)的输出
- 哪位大哥能帮在下写一个完整的JDBC数据库操作类?过年了,帮帮忙,我实在是没米了。。。
- System.out缺省的是道控制台的输出流,能否有办法将System.out变为到文件的输出流?
- 基于windowbuilder的socket通信问题
- 开发页面显示时经常遇到的问题!
- (很急)谢谢!我想问一下调用一个对话框的命令语句,比方说我已建立了一个Applet2,接下来该如何在Applet中能点击一个按钮来打开这个Apple
- 如何读出TXT文件中某一到题目出来 帮我看下代码哪里出错帮忙修改下
- 如何用java绘制弧形文字(急!!!!!)
改成下面这个~ 是不是你要的?
student[++i] = keyboard.nextLine();
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;
}
}
}
public class NullPointerExceptionextends RuntimeException当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括: 调用 null 对象的实例方法。
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用。
出现这个异常,说明了将null对象赋值,即nextLine()读到的为空,所以应该先检查,同楼上
你那个student[i].equals("1")有问题,你可以试着打印出student[i],估计是null吧
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;
int i = 0;和count确实应该放while外面
我的是student[i++] = keyboard.nextLine();
count++;
if (student[i].equals("1"))
flat = false;
这样student[i]==NULL就会报NullPointerException