解决:java.lang.NumberFormatException:null异常报错

2020年4月25日 23点热度 0条评论 来源: 莫名~

今天下午在写三层模式信息管理系统的时候,一切进展的异常顺利,心里美滋滋,呵呵哒,然后。。。。。。。。。你懂得嘛 呜呜呜~~~~~~含泪回想。。。。话不多说,你品你细品。。。。。

先奉上Dao层源码

package org.student.dao;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.student.entity.Student;

import com.mysql.jdbc.Connection;
//真正访问数据库的是isExist(student)和addStudent(student)方法


//数据访问层,原子性的增删改查
public class StudentDao { 
	private static final String URL = "jdbc:mysql://localhost:3306/gmh";
	private static String USERNAME = "root";
	private static final String PWD = "1234";
	
//4.增加学生信息
	public boolean addStudent(Student student){ //zs 23 xa
		Connection connection = null;
		PreparedStatement pstmt = null;
		
		try { 
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(URL, USERNAME, PWD);
			String sql = "insert into student(sno,sname,sage,saddress) values(?,?,?,?)";

			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, student.getSno());
		    pstmt.setString(2,student.getSname());
		    pstmt.setInt(3, student.getSage());
		    pstmt.setString(4, student.getSaddress());
		    int count = pstmt.executeUpdate();//执行增加操作返回值count
		    
		    if(count>0)
		    	return true;
		    else
		    	 return false;//将结果返回给21 行 public boolean isExist(int sno)
		    	

		} catch (ClassNotFoundException e) { 
			e.printStackTrace();
			return false;//
		} catch (SQLException e) { 
			e.printStackTrace();
			return false;
		} catch (Exception e) { 
			e.printStackTrace();
		   return false;
		} finally { 
			try { 
				
				if (pstmt != null)
					pstmt.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) { 
				e.printStackTrace();
			}
		}
		
	}
	//3.根据学号来修改学生:根据sno知道待修改的人,把这个人修改成student,
	public boolean  updateStudentBySno(int sno,Student student){ //3号改成zs,23,bj
		Connection connection = null;
		PreparedStatement pstmt = null;
		
		try { 
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(URL, USERNAME, PWD);
			String sql = "update student set sname=?,sage=?,saddress=? where sno=?";

			pstmt = connection.prepareStatement(sql);
			
			//修改后的内容
			pstmt.setString(1, student.getSname());
			pstmt.setInt(2, student.getSage());
			pstmt.setString(3, student.getSaddress());
			
			//修改的那个人
			pstmt.setInt(4,sno);都是泪啊啊啊啊这个地方
		    
		    int count = pstmt.executeUpdate();//执行增加操作返回值count
		    
		    if(count>0)
		    	return true;
		    else
		    	 return false;//将结果返回给21 行 public boolean isExist(int sno)
		    	

		} catch (ClassNotFoundException e) { 
			e.printStackTrace();
			return false;//
		} catch (SQLException e) { 
			e.printStackTrace();
			return false;
		} catch (Exception e) { 
			e.printStackTrace();
		   return false;
		} finally { 
			try { 
				
				if (pstmt != null)
					pstmt.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) { 
				e.printStackTrace();
			}
		}
	}
	
	//2.根据学号来删除学生
	public boolean  deleteStudentBySno(int sno){ 
		Connection connection = null;
		PreparedStatement pstmt = null;
		
		try { 
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(URL, USERNAME, PWD);
			String sql = "delete from student where sno=?";

			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, sno);
		    
		    int count = pstmt.executeUpdate();//执行增加操作返回值count
		    
		    if(count>0)
		    	return true;
		    else
		    	 return false;//将结果返回给21 行 public boolean isExist(int sno)
		    	

		} catch (ClassNotFoundException e) { 
			e.printStackTrace();
			return false;//
		} catch (SQLException e) { 
			e.printStackTrace();
			return false;
		} catch (Exception e) { 
			e.printStackTrace();
		   return false;
		} finally { 
			try { 
				
				if (pstmt != null)
					pstmt.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) { 
				e.printStackTrace();
			}
		}
	}
	
	//一般情况下,有多种查询,查询方法如下
	
	
	//1.(3)查询全部学生(很多学生),放到集合里面
	public List<Student> queryAllStudents() { //函数不需要参数了
		List<Student> students =new ArrayList<>();//yonglist集合保存全部学生
	
		Student student = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try { 
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(URL, USERNAME, PWD);
			String sql = "select * from student ";

			pstmt = connection.prepareStatement(sql);
			
			rs = pstmt.executeQuery();
			while(rs.next()) { 

				int no = rs.getInt("sno");
				String name = rs.getString("sname");
				int age = rs.getInt("sage");
				String address = rs.getString("saddress");
				student = new Student(no, name, age, address);
				
                students.add(student);//查出来一个人,就把这个人的信息放进里面,然后返回给students
                //这样会使得资源浪费,后面会用springboot进行封装起来
			}

			return students;

		} catch (ClassNotFoundException e) { 
			e.printStackTrace();
			return null;// 查不到返回null
		} catch (SQLException e) { 
			e.printStackTrace();
			return null;
		} catch (Exception e) { 
			e.printStackTrace();
		   return null;
		} finally { 
			try { 
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) { 
				e.printStackTrace();
			}
			}
		}
		
	
    //1.(4)根据姓名查询
	//1.(5)根据年龄查询
	//1.(6)查询此人是否存在
	
	
	
	//第二种方法查询1.(2),直接通过学号来进行查,没有必要查一个学生的所有信息 
		public boolean isExist(int sno){ //
	    return queryStudentBySno(sno)==null? false:true;
		
		}
		
	
	// 1.(1)根据学号来查询学生,查询之后返回给我public void 换为public student,查完之后要把学生封装一下
	// Student student= new Student();
    //以下为第一种方式查询,虽然麻烦,但为了以后使用方便
	public Student queryStudentBySno(int sno) { 
		Student student = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try { 
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(URL, USERNAME, PWD);
			String sql = "select * from student where sno=?";

			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, sno);
			rs = pstmt.executeQuery();
			if (rs.next()) { 

				int no = rs.getInt("sno");
				String name = rs.getString("sname");
				int age = rs.getInt("sage");
				String address = rs.getString("saddress");
				student = new Student(no, name, age, address);

			}

			return student;

		} catch (ClassNotFoundException e) { 
			e.printStackTrace();
			return null;// 查不到返回null
		} catch (SQLException e) { 
			e.printStackTrace();
			return null;
		} catch (Exception e) { 
			e.printStackTrace();
		   return null;
		} finally { 
			try { 
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) { 
				e.printStackTrace();
			}
		}
		
		
	}
}


啊啊啊,小问题。。呸,疯狂抽嘴。。。。。呜呜呜呜。。。。

查了报错文档后,初步认定是空指针问题。。。然后改了 。。run。。
跑路‘,网上众说纷纭,有的说

原因: 空指针运行异常 解决办法:主要是代码运行异常 在控制台找出错误 改正即可
我信你个鬼,你个糟老头子坏得很。。。。此处省略10000000个字

update删了重写,保存,运行,啊,舒服,窗外天空分外蓝。。。蓝的发黑。。。。黑眼圈警告!!!!!

额鹅鹅鹅,,,,啊啊啊啊,,,丫丫丫丫,,,,nice

附加一张bug玄学思维导图,喜欢的点赞加关注吧,,我是小蝈,小伙伴们明天见~~~~~~~~~~~~~

    原文作者:莫名~
    原文地址: https://blog.csdn.net/qq_44893880/article/details/105755241
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。