当前位置: 代码迷 >> J2SE >> Exception in thread "main" java.lang.NullPointerException如何解决
  详细解决方案

Exception in thread "main" java.lang.NullPointerException如何解决

热度:2766   发布时间:2013-02-25 00:00:00.0
Exception in thread "main" java.lang.NullPointerException怎么解决啊
StudentMain.java
package com.lele.jdbc.main;

import java.util.Scanner;

import com.lele.jdbc.menu.Menu;
import com.lele.jdbc.menu.Menu;


public class StudentMain {
public static void main(String[] args){
Scanner s=new Scanner(System.in);
boolean ok = true;
while(ok){
Menu.startInterface();
int i = s.nextInt(); //输入一个整数
switch(i ){
case 1:
Menu.look();
break;
case 2:
Menu.Insert();
break;
case 3:
Menu.alter();
break;
case 4:
Menu.query();
break;
case 5:
System.out.println(" 欢迎使用学生信息管理系统,再见!");
ok = false;
break;
}
}
}

}


Menu.java
package com.lele.jdbc.menu;

import java.util.List;
import java.util.Scanner;

import com.lele.jdbc.dao.StudentD;
import com.lele.jdbc.model.Student;

public class Menu {

public static StudentD sD = new StudentD();
public static Scanner s = new Scanner(System.in);

public static void startInterface(){
System.out.println("***********欢迎使用学生信息管理系统************");
System.out.println("1.浏览信息数据");
System.out.println("2.增加信息数据");
System.out.println("3.修改信息数据");
System.out.println("4.查询信息数据");
System.out.println("5.退出");
}

public static void look() {
List< Student > l = sD.getAllStudent();
System.out.println("所有的学生信息为:");
System.out.println("-------------------");
System.out.println("学生编号\t学生学号\t学生姓名\t学生性别\t学生年龄");
for( Student s : l ){
System.out.println(s.getId()+"\t"+s.getNumber()+"\t"+s.getName()+"\t"+s.getSex()+"\t"+s.getAge());
}
if( l.size()==0 ){
System.out.println("您所查询的 数据不存在!");
}
methodEnd();
}

public static void Insert() {
Student student = new Student();
System.out.println("学生信息输入");
Scanner s = new Scanner(System.in);
System.out.println("请输入学生学号:");
  int number= s.nextInt();
  System.out.println("请输入学生姓名:");
  String name=s.next();
  System.out.println("请输入学生性别:");
  String sex=s.next();
  System.out.println("请输入学生年龄:");
  int age=s.nextInt();
sD.save(student);
System.out.println("添加成功!");
methodEnd();
}

public static void alter() {
List< Student > l = sD.getAllStudent();
System.out.println("全部学生信息:");
System.out.println("-------------------");
System.out.println("学生编号\t学生学号\t学生姓名\t学生性别\t学生年龄");
for( Student s : l ){
System.out.println(s.getId()+"\t"+s.getNumber()+"\t"+s.getName()+"\t"+s.getSex()+"\t"+s.getAge());
}
Student student = new Student();
System.out.println("请输入要修改的学生的编号");
  Scanner a=new Scanner(System.in);  
  int ID=a.nextInt();
  System.out.println("请输入学生学号:");
  int number= a.nextInt();
  System.out.println("请输入学生姓名:");
  String name=a.next();
  System.out.println("请输入学生性别:");
  String sex=a.next();
  System.out.println("请输入学生年龄:");
  int age=a.nextInt();
try {
sD.updateById(student);
} catch (Exception e) {
System.out.println("更新失败,请检查您的输入!");
return ;
}
System.out.println("更新成功");
  相关解决方案