java报错Exception in thread "main" java.lang.NullPointerException
Exceptioninthread"main"java.lang.NullPointerExceptionatStudentAccount.innerstudent(St...
Exception in thread "main" java.lang.NullPointerException
at StudentAccount.innerstudent(Studentmain.java:53)
at Studentmain.main(Studentmain.java:94)
源代码
import java.io.*;
class StudentInfo {
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getYx() {
return Yx;
}
public void setYx(String yx) {
Yx = yx;
}
public String getZz() {
return Zz;
}
public void setZz(String zz) {
Zz = zz;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
private String Id;
private String Yx;
private String Zz;
private String Phone;
public StudentInfo(){}
public StudentInfo(String id,String yx,String zz,String phone){
this.Id=id;
this.Yx=yx;
this.Zz=zz;
this.Phone=phone;
}
}
class StudentAccount {
public StudentInfo[] student=new StudentInfo[100];
String innerstudent(String id,String yx,String zz,String phone){
int count=0;
for(int i=0;i<100;i++){
if(student[i]!=null){
count++;
}
student[count].setId(id);
student[count].setYx(yx);
student[count].setZz(zz);
student[count].setPhone(phone);
System.out.println("新增成功!");
}
return id;
}
}
public class Studentmain {
public static void main(String[] args){
String id="";
String yx="";
String zz="";
String phone="";
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String choose ="";
while (true){
System.out.println("请选择要进行的操作:1.新增2.删除3.查询"+"4.显示账户5.退出");
try{
choose= br.readLine();
}catch (IOException e){
e.printStackTrace();
}
if(choose.equals("1"))
while(true){
try{System.out.println("请输入姓名:");
id=br.readLine();
System.out.println("请输入院系:");
yx=br.readLine();
System.out.println("请输入住址:");
zz=br.readLine();
System.out.println("请输入电话:");
phone=br.readLine();
StudentAccount one=new StudentAccount();
one.innerstudent(id,yx,zz,phone);
}catch(NumberFormatException e){
System.out.println("输入错误,请输入整数");
continue;
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
}
目的是实现学生数组的新增功能,是想判断数组中空的元素位置然后赋值。
希望能人指出错误啊,,谢谢啦,说得浅一点我是菜鸟啊,, 展开
at StudentAccount.innerstudent(Studentmain.java:53)
at Studentmain.main(Studentmain.java:94)
源代码
import java.io.*;
class StudentInfo {
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getYx() {
return Yx;
}
public void setYx(String yx) {
Yx = yx;
}
public String getZz() {
return Zz;
}
public void setZz(String zz) {
Zz = zz;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
private String Id;
private String Yx;
private String Zz;
private String Phone;
public StudentInfo(){}
public StudentInfo(String id,String yx,String zz,String phone){
this.Id=id;
this.Yx=yx;
this.Zz=zz;
this.Phone=phone;
}
}
class StudentAccount {
public StudentInfo[] student=new StudentInfo[100];
String innerstudent(String id,String yx,String zz,String phone){
int count=0;
for(int i=0;i<100;i++){
if(student[i]!=null){
count++;
}
student[count].setId(id);
student[count].setYx(yx);
student[count].setZz(zz);
student[count].setPhone(phone);
System.out.println("新增成功!");
}
return id;
}
}
public class Studentmain {
public static void main(String[] args){
String id="";
String yx="";
String zz="";
String phone="";
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String choose ="";
while (true){
System.out.println("请选择要进行的操作:1.新增2.删除3.查询"+"4.显示账户5.退出");
try{
choose= br.readLine();
}catch (IOException e){
e.printStackTrace();
}
if(choose.equals("1"))
while(true){
try{System.out.println("请输入姓名:");
id=br.readLine();
System.out.println("请输入院系:");
yx=br.readLine();
System.out.println("请输入住址:");
zz=br.readLine();
System.out.println("请输入电话:");
phone=br.readLine();
StudentAccount one=new StudentAccount();
one.innerstudent(id,yx,zz,phone);
}catch(NumberFormatException e){
System.out.println("输入错误,请输入整数");
continue;
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
}
目的是实现学生数组的新增功能,是想判断数组中空的元素位置然后赋值。
希望能人指出错误啊,,谢谢啦,说得浅一点我是菜鸟啊,, 展开
4个回答
展开全部
额,楼上动作真快。。。
你这是空指针异常,因为你创建student数组时并没有赋值,所以里面默认是null
当你调用这个数组时因为里面还没有东西,就等于是你在调用一个还不存在的东西,所以会报空指针异常
你这是空指针异常,因为你创建student数组时并没有赋值,所以里面默认是null
当你调用这个数组时因为里面还没有东西,就等于是你在调用一个还不存在的东西,所以会报空指针异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,出错的原因在这里:
public StudentInfo[] student = new StudentInfo[100];
上面的数组开辟了100长度,但是每个元素的默认值都是null,仔细想想是不是?
所以解决办法就是:
String innerstudent(String id, String yx, String zz, String phone) {
StudentInfo si = new StudentInfo() ;
int count = 0;
for (int i = 0; i < 100; i++) {
if(i == 0 && student[i] == null){
student[i] = si ;
}else if (student[i] != null) {
count++;
}else{
student[count].setId(id);
student[count].setYx(yx);
student[count].setZz(zz);
student[count].setPhone(phone);
System.out.println("新增成功!");
break ;
}
}
return id;
}
public StudentInfo[] student = new StudentInfo[100];
上面的数组开辟了100长度,但是每个元素的默认值都是null,仔细想想是不是?
所以解决办法就是:
String innerstudent(String id, String yx, String zz, String phone) {
StudentInfo si = new StudentInfo() ;
int count = 0;
for (int i = 0; i < 100; i++) {
if(i == 0 && student[i] == null){
student[i] = si ;
}else if (student[i] != null) {
count++;
}else{
student[count].setId(id);
student[count].setYx(yx);
student[count].setZz(zz);
student[count].setPhone(phone);
System.out.println("新增成功!");
break ;
}
}
return id;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
空指针异常,没有初始化数组,所以数组的下标内没有值!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询