Hi:
The query string is returning a list which I am trying to display using System.out.println but I am unable to do so. Can someone please help.
I am trying to access the list using the following code, the list size is correct as per the database, I verified it, but how do I get the last name or first name?
I will really appreciate if someone can help. I respect everybody's time and effort
Quote:
I am getting the following error:
Exception in thread "main" java.lang.NullPointerException
at com.nb.siebel.framework.hibernate.HibernateTest.listAccountContact(HibernateTest.java:136)
at com.nb.siebel.framework.hibernate.HibernateTest.main(HibernateTest.java:24)
Hibernate version:3.1
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.nb.siebel.framework.hibernate.Contacts" table="Contacts" schema="dbo" catalog="siebeldb">
<composite-id name="id" class="com.nb.siebel.framework.hibernate.ContactsId">
<key-property name="contactId" type="java.lang.Double">
<column name="ContactID" precision="15" scale="0" />
</key-property>
<key-property name="firstName" type="java.lang.String">
<column name="FirstName" />
</key-property>
<key-property name="middleName" type="java.lang.String">
<column name="MiddleName" />
</key-property>
<key-property name="lastName" type="java.lang.String">
<column name="LastName" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
Transaction tx = contactsDAO.getSession().beginTransaction();
List contact = contactsDAO.findAll();
tx.commit();
System.out.println("List size: " + contact.size());
//ContactsId localContactsId = new ContactsId();
for(int i=0;i<contact.size();i++){
System.out.println(i + ") " + "----------------");
ContactsId localContacts = (ContactsId)contact.get(i);
System.out.println(localContacts.getContactId());
}
contactsDAO.getSession().close();
Full stack trace of any exception that occurs:
I have provided the error above.
Name and version of the database you are using:
MS SQL Server 2005
The generated SQL (show_sql=true):
13:53:04,171 DEBUG SQL:346 - select contacts0_.ContactID as ContactID2_, contacts0_.FirstName as FirstName2_, contacts0_.MiddleName as MiddleName2_, contacts0_.LastName as LastName2_ from siebeldb.dbo.Contacts contacts0_
DAO Code:
----------------------------------
Code:
public List findAll() {
log.debug("finding all Contacts instances");
try {
String queryString = "from Contacts";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
Contacts.java
----------------------------------
Code:
public class Contacts extends AbstractContacts implements java.io.Serializable {
// Constructors
/** default constructor */
public Contacts() {
}
/** full constructor */
public Contacts(ContactsId id) {
super(id);
}
}
AbstractContacts.java
-----------------------------
Code:
public abstract class AbstractContacts implements java.io.Serializable {
// Fields
private ContactsId id;
// Constructors
/** default constructor */
public AbstractContacts() {
}
/** full constructor */
public AbstractContacts(ContactsId id) {
this.id = id;
}
// Property accessors
public ContactsId getId() {
return this.id;
}
public void setId(ContactsId id) {
this.id = id;
}
}
ContactsId.java
------------------------
Code:
public class ContactsId extends AbstractContactsId implements
java.io.Serializable {
// Constructors
/** default constructor */
public ContactsId() {
}
/** full constructor */
public ContactsId(Double contactId, String firstName, String middleName,
String lastName) {
super(contactId, firstName, middleName, lastName);
}
}
AbstractContactsId.java
-----------------------------
Code:
public abstract class AbstractContactsId implements java.io.Serializable {
// Fields
private Double contactId;
private String firstName;
private String middleName;
private String lastName;
// Constructors
/** default constructor */
public AbstractContactsId() {
}
/** full constructor */
public AbstractContactsId(Double contactId, String firstName,
String middleName, String lastName) {
this.contactId = contactId;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
// Property accessors
public Double getContactId() {
return this.contactId;
}
public void setContactId(Double contactId) {
this.contactId = contactId;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof AbstractContactsId))
return false;
AbstractContactsId castOther = (AbstractContactsId) other;
return ((this.getContactId() == castOther.getContactId()) || (this
.getContactId() != null
&& castOther.getContactId() != null && this.getContactId()
.equals(castOther.getContactId())))
&& ((this.getFirstName() == castOther.getFirstName()) || (this
.getFirstName() != null
&& castOther.getFirstName() != null && this
.getFirstName().equals(castOther.getFirstName())))
&& ((this.getMiddleName() == castOther.getMiddleName()) || (this
.getMiddleName() != null
&& castOther.getMiddleName() != null && this
.getMiddleName().equals(castOther.getMiddleName())))
&& ((this.getLastName() == castOther.getLastName()) || (this
.getLastName() != null
&& castOther.getLastName() != null && this
.getLastName().equals(castOther.getLastName())));
}
public int hashCode() {
int result = 17;
result = 37 * result
+ (getContactId() == null ? 0 : this.getContactId().hashCode());
result = 37 * result
+ (getFirstName() == null ? 0 : this.getFirstName().hashCode());
result = 37
* result
+ (getMiddleName() == null ? 0 : this.getMiddleName()
.hashCode());
result = 37 * result
+ (getLastName() == null ? 0 : this.getLastName().hashCode());
return result;
}
}