怎么就我遇到這種問題! ?
}else?if(OPERATION_UPDATE.equals(in.toUpperCase()) ||?OPERATION_UPDATE.substring(0,1).equals(in.toUpperCase()) ||?OPERATION_UPDATE.equals(previous)){ previous=OPERATION_UPDATE; //更新 if(1==step){ System.out.println("請輸入要更新的女生的【編號/ID】"); }else?if(2==step){ try?{ if(action.get(Integer.valueOf(in))==null){ System.out.println("ID無效,請重新輸入。。。"); step=1; } }?catch?(NumberFormatException?e)?{ e.printStackTrace(); }?catch?(SQLException?e)?{ e.printStackTrace(); } goddess.setId(Integer.valueOf(in)); System.out.println("【姓名】,如果不更新該字段,則輸入null,下同"); }else?if(3==step){ if(!in.equals("null")){ goddess.setUser_name(in); } System.out.println("【性別】?(0:男?,?1:女)"); }else?if(4==step){ if(!in.equals("null")){ goddess.setSex(Integer.valueOf(in)); } System.out.println("【年齡】"); }else?if(5==step){ if(!in.equals("null")){ goddess.setAge(Integer.valueOf(in)); } System.out.println("【生日】,格式如:yyyy-MM-dd"); }else?if(6==step){ if(!in.equals("null")){ SimpleDateFormat?sf=new?SimpleDateFormat("yyyy-MM-dd"); Date?birthday=null; try?{ birthday?=?sf.parse(in); goddess.setBirthday(birthday); }?catch?(ParseException?e)?{ e.printStackTrace(); System.out.println("您輸入的格式有誤,請重新輸入"); step=5; } } System.out.println("【郵箱】"); }else?if(7==step){ if(!in.equals("null")){ goddess.setEmail(in); } System.out.println("【手機號】"); }else?if(8==step){ if(!in.equals("null")){ goddess.setMobile(in); } try?{ action.edit(goddess); System.out.println("女生信息更新成功!"); previous=null; }?catch?(Exception?e)?{ e.printStackTrace(); System.out.println("女生信息更新失敗!\n"+"請重新輸入。。。"); step=0; } } if(OPERATION_UPDATE.equals(previous)){ step++; } }
如圖,2個問題。誰能助我,感激不盡!
我還疑惑的是,在原始的updateGoddess()方法中,只有9個參數,代碼如下:
public?void?updateGoddess(Goddess?g)?throws?SQLException{ Connection?conn=DBUtil.getConnection(); String?sql="update?imooc_goddess?set?user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?," +?"update_user=?,update_date=current_date(),isdel=??" +?"where?id=?"; PreparedStatement?ptmt=conn.prepareStatement(sql); ptmt.setString(1,?g.getUser_name()); ptmt.setInt(2,?g.getSex()); ptmt.setInt(3,?g.getAge()); ptmt.setDate(4,?new?Date(g.getBirthday().getTime())); ptmt.setString(5,?g.getEmail()); ptmt.setString(6,?g.getMobile()); ptmt.setString(7,?g.getUpdate_user()); ptmt.setInt(8,?g.getIsdel()); ptmt.setInt(9,?g.getId()); ptmt.execute(); }
而我在View中的update模塊里,不論是按視頻里寫ID加姓名、年齡、生日這4個參數,還是對照updateGoddess()寫9個,或是寫完全部12個參數,都報空指針異常。這是為什么?
2016-11-09
原來是邏輯出錯了!我們仿寫前幾個模塊比如add,但此處是update,應該先傳入id,根據id取出該條記錄,然后對此記錄進行修改。
第19行改為?goddess = (Goddess)action.get(Integer.valueOf(in));
一切ok!
空指針就是說 我們連要修改的對象都沒有取到啊,談何修改
2016-11-08
我也遇到這種問題,我覺得是視頻里面有問題,雖然不知道為什么能夠作用。
當時主要要ptmt.setInt(int position,int x) 這里的x是個int類型,
但是我們在定義的sex是個Integer是個對象,然后在View里面并沒有給它和isdel賦值,導致傳遞到ptmt.setInt(int position,int x)中變成ptmt.setInt(int position,NULL),所以就出現問題了。
解決方案是在View里面給g.sex會g.isdel直接賦值或者在類Goddess添加構造器賦值即可。
至于為何視頻里面可以有結果,我不知道。
2016-10-17
我直接把 源代碼 考了一份貼上去 就好了
2016-10-17
我跟你一樣的問題,性別字段空指針異常,補上了也不行?。?/p>
2016-09-16
調用增加女神的方法沒有給定性別,創建人等信息,所以出錯