#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    12
    Rep Power
    0

    error in returned object


    Hi,

    Here is the problem I am having now:

    my class:
    ======
    UserDetails
    {
    ......
    }

    my Java bean:
    ==========
    UserDB
    {
    ......
    public UserDetails getUser(int uID)
    {
    ......
    UserDetails rd = new UserDetails(......);
    return rd;
    }
    ......
    }

    my JSP page:
    =========
    ......
    <jsp:useBean id="user" scope="request" class="database.UserBean" />
    UserDetails rd = user.getUser(i);
    rd.getUserName(); // I get error from here
    ......

    And I get "java.lang.NullPointerException" from my JSP page. I thought it might need the same thing as Copy Constructor in C++. So I used clone(). Here is what I did:

    my class:
    ======
    class UserDetails implements Cloneable
    {
    ...
    public Object clone ( )
    {
    try
    {
    return super.clone() ;
    }
    catch ( CloneNotSupportedException e )
    {
    return null;
    }
    }
    ...
    }

    my JSP page:
    =========
    ......
    <jsp:useBean id="user" scope="request" class="database.UserBean" />
    UserDetails rd = (UserDetails)user.getUser(i).clone();
    rd.getUserName();
    ......

    But I still get the same error. Can you help me figure out what the problem is? Thanks you a lot!
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    I do not see a UserBean class (class="database.UserBean" ). Shouldn't the name be UserDB?
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    12
    Rep Power
    0
    Hi,

    thanks for your reply! Sorry, that is my typo. Yes, the class name should be: UserBean. So basically, I have two classes: UserDetail and UserBean. UserBean query/update user database. UserDetail is the return type for query. But my current problem is that the returned UserDetail object is not useable in my JSP page. And I don't know what the problem is. Thanks!


    file name: UserBean.java
    ==================

    package database;

    import ......;

    public class UserBean
    {
    ......
    }
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    You will have to post your code complete.

    Guessing - When you use the useBean tag, it calls the no-argument constructor of the class in question. If you are not doing initialization in that no-arg constructor, then you may have problems from that.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    12
    Rep Power
    0
    Hi,

    Thanks a lot for the reply! Here is my code:

    UserBean.java:
    ===========

    package database;

    import java.sql.*;
    import javax.sql.*;
    import javax.naming.*;
    import java.util.*;
    import com.pointbase.jdbc.*;

    public class UserBean
    {
    ......

    Connection con;
    private boolean conFree = true;

    public UserBean () throws Exception
    {
    try
    {
    jdbcDataSource ds = new jdbcDataSource();
    ds.setDatabaseName("jdbcointbase:server://localhost/userdb");
    ds.setUser("PBPUBLIC");
    ds.setPassword("PBPUBLIC");
    ds.setCreateDatabase(true);
    con = ds.getConnection();
    }
    catch (Exception ex)
    {
    throw new Exception("Couldn't open connection to database: " + ex.getMessage());
    }
    }

    public UserDetails getUser(String uName, String uPassword)
    {
    try
    {
    getConnection();
    Statement Stmt = con.createStatement();
    ResultSet rs = Stmt.executeQuery("SELECT * " + "FROM users WHERE uname = " +
    uName + " AND upassword = " + uPassword);
    if (rs.next())
    {
    UserDetails ud = new UserDetails(rs.getString(2), rs.getString(3), rs.getString(4),
    rs.getString(5),rs.getString(6),rs.getString(7),
    rs.getString(8),rs.getString(9));
    Stmt.close();
    releaseConnection();
    return ud;
    }
    else
    {
    Stmt.close();
    }
    }
    catch (SQLException ex)
    {
    }
    releaseConnection();
    return null;
    }

    ......
    }


    UserDetails.java:
    ============

    package database;

    public class UserDetails
    {
    private int uid = 0;
    private String uname = null;
    private String upassword = null;
    private String ulastname = null;
    private String ufirstname = null;
    private String ussn = null;
    private String uemailaddr = null;
    private String umailingaddr = null;
    private String uphone = null;

    public UserDetails()
    {
    }

    public UserDetails(String uName, String uPassword, String uLastname, String uFirstname,
    String uSsn, String uEmailaddr, String uMailingaddr, String uPhone)
    {
    this.uname = uName;
    this.upassword = uPassword;
    this.ulastname = uLastname;
    this.ufirstname = uFirstname;
    this.ussn = uSsn;
    this.uemailaddr = uEmailaddr;
    this.umailingaddr = uMailingaddr;
    this.uphone = uPhone;
    }

    public void setUserName(String uName)
    {
    this.uname = uName;
    }

    public int getUserID()
    {
    return this.uid;
    }

    public String getUserName()
    {
    return this.uname;
    }

    public String getUserPassword()
    {
    return this.upassword;
    }

    public String getLastName()
    {
    return this.ulastname;
    }

    public String getFirstName()
    {
    return this.ufirstname;
    }

    public String getUserSSN()
    {
    return this.ussn;
    }

    public String getEmailAddr()
    {
    return this.uemailaddr;
    }

    public String getMailingAddr()
    {
    return this.umailingaddr;
    }

    public String getPhone()
    {
    return this.uphone;
    }
    }


    JSP Page:
    =======

    <%@ page language = "java" %>
    <%@ page import = "java.util.*" %>
    <%@ page import = "database.*" %>
    <%@ include file="EnsureUserLoggedIn.jsp" %>

    <jsp:useBean id="userUp" scope="request" class="database.UserBean" />

    <%
    String userName = (String)session.getAttribute("username");
    String passWord = (String)session.getAttribute("userpassword");
    UserDetails ud = userUp.getUser(userName, passWord);

    String lastName = ud.getLastName(); // get error from here.
    String firstName = ud.getFirstName();
    String ssNum = ud.getUserSSN();
    String emailAddr = ud.getEmailAddr();
    String mailAddr = ud.getMailingAddr();
    String phoneNum = ud.getPhone();

    %>
    .......
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    If 'ud' is null after calling the method userUp.getUser(userName, passWord), then that means that getUser is returning null. Why would getUser return null? Take a close look at it and you'll find that it returns null if there is an SQLException. Since you capture this exception but don't ever write anything out to standard out, there is no way you will know you have an error.

    Concentrate on your sql. You likely have a problem with it. Also, you should rarely catch an exception and do nothing with it. At the very least write something to System.out.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    12
    Rep Power
    0
    Hi Nemi,

    Thanks a lot for your help! Yes, it is my SQL problem which returns null. Thank you very muc!

IMN logo majestic logo threadwatch logo seochat tools logo