mysql - I want to display record getting value from database where id=id (insert value into database from getter method not work) in java -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i create java file name attend.java
public class attend { string attend_id,studname,owner; public string getowner() { return owner; } public void setowner(string owner) { this.owner = owner; }public string getstudname() { return studname; } public void setstudname(string studname) { this.studname = studname; } public string getattend_id() { return attend_id; } public void setattend_id(string attend_id) { this.attend_id = attend_id; }} ; other file dao file attenddao.java
, public class attenddao {public static list<attend> getallrecords(){ list<attend> list=new arraylist<attend>(); //attend u1=null; try{ connection con=dbconnector.getconnection(); preparedstatement ps=con.preparestatement("select attend_id,studname,owner attend_detail owner=?"); attend u1 = null; //system.out.println(u1.getowner()); ps.setstring(1,u1.getowner()); resultset rs=ps.executequery(); while(rs.next()){ attend u=new attend(); u.setattend_id(rs.getstring("attend_id")); u.setstudname(rs.getstring("studname")); u.setowner(rs.getstring("owner")); list.add(u); } }catch(sqlexception e1){system.out.println("sql error:"+e1); }catch(exception e){system.out.println(e);} return list; //} }}, i want owner value database if owner="xyz@xyz.com" display rows data
this database , value.. database name attend_detail.
attend_id studname owner atn001 abcd xyz@xyz.com atn002 efgh null this jsp view.jsp
<body> <%@page import="com.attend.detail.attenddao,com.attend.detail.attend,java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <div class="container"> <h1>attendance list</h1> <% list<attend> list=attenddao.getallrecords(); request.setattribute("list",list); %> <div class="table-responsive" width="100%"> <table id="mytable" class="display table table-bordered" width="99%" > <thead> <tr> <th>attend_id</th> <th>student_id</th> <th>owner</th> </tr> </thead> <tbody> <c:foreach items="${list}" var="u"> <tr> <td>${u.getattend_id()}</td> <td>${u.getstudentid()}</td> <td>${u.getowner()}</td> </tr> </c:foreach> </tbody> </table> </div></body> it shows nullpointerexception how solve it.. please rply.
you need assign value after attend u1 = null, initialize null.
attend u1 = null; //system.out.println(u1.getowner()); ps.setstring(1,u1.getowner()); so, basically, u1 has null , need assign attend object correct issue.
wiki
Comments
Post a Comment