为什么报空指针异常
package com.imooc.action; import Dao.GoddessDao; import com.imooc.model.Goddess; import java.util.Date; import java.util.List; public class GoddessAction { public static void main(String[] args) throws Exception { GoddessDao g = new GoddessDao(); Goddess g1 = new Goddess(); g1.setUser_name("小夏"); g1.setAge(22); g1.setSex(1); g1.setBirthday(new Date()); g1.setEmail("xiaoxia@imooc.com"); g1.setUser_name("admin"); g1.setUpdate_user("admin"); g1.setIsdel(1); g.addGoddess(g1); } }
package Dao; import com.imooc.db.DBUtil; import com.imooc.model.Goddess; import java.sql.*; import java.util.ArrayList; import java.util.List; public class GoddessDao { public void addGoddess(Goddess g) throws Exception { Connection connection = DBUtil.getConnection(); String sql = "" + "insert into imooc_goddess" + "(user_name, sex, age, birthday, email," + "create_user, create_date, update_user, update_date, isbel)" + "values(" + "?,?,?,?,?,?,current_date(),?,current_date(),?)"; PreparedStatement ptmt = connection.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.getCreate_user()); ptmt.setString(7,g.getUpdate_user()); ptmt.setInt(8,g.getIsdel()); ptmt.execute(); } public void updateGddess() { } public void deleteGoddess() { } public List<Goddess> query() throws Exception { Connection connection = DBUtil.getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT user_name,age FROM imooc_goddess"); List<Goddess> gs = new ArrayList<Goddess>(); Goddess g = null; while (resultSet.next()) { g = new Goddess(); g.setUser_name(resultSet.getString("user_name")); g.setAge(resultSet.getInt("age")); gs.add(g); } return gs; } public Goddess get() { return null; } }
这两段代码显示空指针异常