普通的Person类
- public class Person {
- private int id;
- private String name;
- private int age;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
- }
- public Person(int id, String name, int age) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- }
- public Person() {
- super();
- }
-
- }
复制代码 用于实现添加删除更新功能的接口
- public interface Service {
- void addNew(Person person);
- void delete(Integer id);
- void update(Person person);
- }
复制代码
实现接口的业务类
- public class ServiceImp implements Service {
- private Map<Integer, Person> map= new HashMap<Integer,Person>();
- public ServiceImp(){//空异常
- map.put(1001, new Person(1002,"tom1", 15));
- map.put(1002, new Person(1002,"tom1", 15));
- map.put(1003, new Person(1002,"tom1", 15));
-
- }
- public Map<Integer, Person> getMap() {
- return map;
- }
- public void setMap(Map<Integer, Person> map) {
- this.map = map;
- }
- @Override
- public void addNew(Person person) {
- // TODO Auto-generated method stub
- map.put(person.getId(), person);
- }
- @Override
- public void delete(Integer id) {
- // TODO Auto-generated method stub
- map.remove(id);
- }
- @Override
- public void update(Person person) {
- // TODO Auto-generated method stub
- map.put(person.getId(), person);
- }
- }
复制代码 测试类
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Service target=new ServiceImp();//直接报空异常
-
-
- }
- }
复制代码 在测试类中一new对象就在业务类中报空异常
Exception in thread "main" java.lang.NullPointerException
at com.sun.proxy.ServiceImp.<init>(ServiceImp.java:10)
at com.sun.proxy.TestProxy.main(TestProxy.java:11)
求助高手 实在不知道怎么回事
|
|