-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: null pointer exception
PostPosted: Sun Mar 14, 2010 9:23 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
i have try with association as u said but i am getting the same null pointer exception.

once again i send the modified outer join query with the foreign key mapping.

help me it is very urgent.where i have done the mistake

my main class
package simple;

import org.hibernate.Query;
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.*;
import simple.Contact;
import simple.Insurance;
public class Inner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try{
SessionFactory fact = new
Configuration().configure().buildSessionFactory();
sess = fact.openSession();
String sql_query = "select emp.firstname,Insurance.InsuranceName,Insurance.InvestmentAmount FROM simple.Contact emp left outer join fetch simple.Insurance Insurance where emp.InsID = Insurance.InsID";
Query query = sess.createQuery(sql_query);
Iterator ite = query.list().iterator();
System.out.println("First Name\t"+"Insurance Name\t"+"InvestmentAmount");
while ( ite.hasNext() ) {
Object[] pair = (Object[]) ite.next();
Contact con = (Contact) pair[0];
Insurance ins = (Insurance) pair[1];
System.out.print(con.getFirstName());
System.out.print("\t"+ins.getInsuranceName());
System.out.print("\t\t"+ins.getInvestementAmount());
System.out.println();
/*System.out.println("First Name:"+pair[0]);
System.out.println("Insurance name:"+pair[1]);
System.out.println("InvestmentAmount:"+pair[2]);*/


}
sess.close();
}
catch(Exception e ){
System.out.println(e.getMessage());
e.printStackTrace();
}

}

}






package simple;
import java.util.*;
public class Contact {
private String firstName;
private String lastName;
private int insid;
private int id;
private List insurance;
public List getInsurance()
{
return insurance;
}
public void setInsurance(List insurance)
{
this.insurance = insurance;
}

/**
* @return insid
*/

/**
* @return First Name
*/

public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}


public int getinsid() {
return insid;
}

/**
* @param string Sets the Insid
*/

public void setinsid(int i) {
insid = i;
}

/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}
public int getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(int i) {
id = i;
}

}

package simple;

import java.util.List;

public class Insurance {

private String insuranceName;
private double investementAmount;
private int id;
private List Contact;
public List getContact()
{
return Contact;
}
public void setContact(List Contact)
{
this.Contact = Contact;
}


public String getInsuranceName() {
return insuranceName;
}

public void setInsuranceName(String insuranceName) {
this.insuranceName = insuranceName;
}

public double getInvestementAmount() {
return investementAmount;
}

public void setInvestementAmount(double investementAmount) {
this.investementAmount = investementAmount;
}


public int getId() {
return id;
}
public void setId(int Id) {
this.id = id;
}
}
contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="simple" >
<class name="Contact" table="emp">
<id name="id" type="int" column="ID" >
<generator class="increment"/>
</id>

<property name="firstName" column ="firstname"/>
<property name="lastName" column ="lastname"/>
<property name="insid" column ="InsID"/>
<bag name="insurance" inverse="
true" cascade="all,delete-orphan">
<key column="InsID"/>
<one-to-many class="simple.Insurance"/>
</bag>
</class>
</hibernate-mapping>
}
insurance.hbm..xml
<?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">
<hibernate-mapping package="simple">
<class name="Insurance" table="Insurance">
<id name="id" column ="InsID" type="int">
<generator class="increment"/>
</id>
<property name="insuranceName" column="InsuranceName type="string" />
<property name="investementAmount" column="InvestmentAmount" type="double"/>

<many-to-one name="Contact" class="simple.Contact" column="InsID"
insert="false" update="false"/>

</class>

</hibernate-mapping>

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">

jdbc:mysql://localhost/sakthi</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: null pointer exception
PostPosted: Mon Mar 15, 2010 5:31 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I have to clue about what you are speaking about, please post follow ups into the same thread!

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: null pointer exception
PostPosted: Tue Mar 16, 2010 4:36 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi saklak,

I believe your mapping is wrong.
If a contact should reference to more insurances then it can't be mapped by a single Contact.insid field.
I think in some way you make confusion between the inverse attribute which should reside on Insurance not on Contact.


Top
 Profile  
 
 Post subject: Re: null pointer exception
PostPosted: Wed Mar 17, 2010 4:13 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I'm absolutely not comfortable with hbm syntax , therefore I show you the correct mapping with annotations.

Code:
package simple;
import java.util.*;

@Entity
public class Contact {
...

@OneToMany(mappedBy="contact")
private List insurance;

public List getInsurance()
{
return insurance;
}
public void setInsurance(List insurance)
{
this.insurance = insurance;
}

...

Code:

package simple;

import java.util.List;

@Entity
public class Insurance {

...
// private List Contact;                WRONG not use List when you except just one result !
// public List getContact()

@ManyToOne
private Contact contact;

public Contact getContact()
{
return contact;
}
public void setContact(Contact contact)
{
this.contact = contact;
}




It's your job to correct translate it into hbm format.
Please read and reread the documentation of Hibernate, and stop bumping this forum with new threads!


Top
 Profile  
 
 Post subject: Re: null pointer exception
PostPosted: Wed Mar 17, 2010 6:23 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
thank u but i solve the foreign key mapping with inner join .
now i am getting in left outer join could u please help me

sorry for disturbing you.
it is very urgent.i am waiting for ur reply.

my exception is as follows:
java.lang.NullPointerException
at org.hibernate.hql.ast.LiteralProcessor.lookupConstant(LiteralProcessor.java:67)
at org.hibernate.hql.ast.DotNode.resolve(DotNode.java:171)
at org.hibernate.hql.ast.FromReferenceNode.resolve(FromReferenceNode.java:87)
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:266)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:2475)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2323)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2232)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:498)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:356)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:184)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:140)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:814)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:773)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at sample.Inner.main(Inner.java:24)
my main class
import sample.Employee;
import sample.EmpDetail;
public class Inner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try{
SessionFactory fact = new
Configuration().configure().buildSessionFactory();
sess = fact.openSession();


String sql_query = " select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM sample.EmpDetail empdet left outer join sample.Employee empmail where empdet.Empid = empmail.Empid";
Query query = sess.createQuery(sql_query);
Iterator ite = query.list().iterator();

System.out.println("FirstName\t"+"Email\t"+"FirstName\t"+"City");
while ( ite.hasNext() ) {
Object[] pair = (Object[]) ite.next();
/* Employee mail = (Employee) pair[0];
EmpDetail det = (EmpDetail) pair[1];
System.out.print(mail.getfirstname());
System.out.print(mail.getemail());
System.out.print("\t"+det.getfirstname());
System.out.print("\t\t"+det.getcity());
System.out.println();*/

System.out.println(pair[0]+"\t"+pair[1]+"\t"+pair[2]+"\t"+pair[3]+"\n");

}
sess.close();
}
catch(Exception e ){
System.out.println(e.getMessage());
e.printStackTrace();

}

}

}

my EmpDetail
package sample;

public class EmpDetail{

private String firstname;
private String city;
private int Empid;


public String getfirstname() {
return firstname;
}

public void setfirstname(String firstname) {
this.firstname =firstname;
}

public String getcity() {
return city;
}

public void setcity(String city) {
this.city = city;
}


public int getEmpid() {
return Empid;
}

public void setEmpid(int Empid) {
this.Empid =Empid;
}
}
my Employee.java
package sample;

public class Employee {
private String firstname;

private String email;
private int Empid;

/**
* @return Email
*/


/**
* @return First Name
*/
public String getfirstname() {
return firstname;
}

public void setfirstname(String string) {
firstname = string;
}

public String getemail() {
return email;
}


/**
* @param string Sets the Email
*/
public void setemail(String string) {
email = string;
}

/**
* @param string Sets the First Name
*/


/**
* @param string sets the Last Name
*/

public int getEmpid() {
return Empid;
}

/**
* @param l Sets the ID
*/
public void setEmpid(int i) {
Empid = i;
}

contact.hbm.xml }

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="sample.Employee" table="empmail">
<id name="Empid" type="int" column="Empid" >
<generator class="increment"/>
</id>

<property name="firstname">
<column name="firstname" />
</property>

<property name="email">
<column name="email"/>
</property>
</class>
</hibernate-mapping>


hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">

jdbc:mysql://localhost/sakthi</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">value="update"</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
</hibernate-configuration>

insurance.hbm.xml
<?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">
<hibernate-mapping>
<class name="sample.EmpDetail" table="empdet">
<id name="Empid" type="int" column ="Empid">
<generator class="increment"/>
</id>

<property name="firstname">
<column name="firstname" />
</property>

<property name="city">
<column name="city"/>
</property>

</class>

</hibernate-mapping>

tell where is the error i have mapped correctly
i am not using foreign key only primary key


Top
 Profile  
 
 Post subject: Re: null pointer exception
PostPosted: Wed Mar 17, 2010 6:39 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Your hbm.xml files names don't match with the class they are containig, that's very confusing:
contact.hbm.xml -> sample.Employee
insurance.hbm.xml -> sample.EmpDetail

Furthermore I cannot see any relation set in this hbm.xml files.
Nordborg posted recently in this forum, that sess.createQuery(sql_query) with sql_query containing joins,
only works if also the mapping (the relation) between the concerning classes is correctly defined.


Quote:
thank u but i solve the foreign key mapping with inner join .

You always must use foreign keys as soon you begin to work with relations.


Top
 Profile  
 
 Post subject: Re: null pointer exception
PostPosted: Wed Mar 17, 2010 7:11 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
thank u for ur answer but i have try with foreign key also the left outer join same exception is raising

once again i give all my xml files and pojo class,main class

please reply me i want to correct this exception so that i will finish my task.

advance thanks those who reply

point out in the code where i had done the mistake .



package simple;

import org.hibernate.Query;
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.*;
import simple.Contact;
import simple.Insurance;
public class Inner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try{
SessionFactory fact = new
Configuration().configure().buildSessionFactory();
sess = fact.openSession();
String sql_query = "select emp.firstName,Insurance.insuranceName,Insurance.investementAmount FROM simple.Contact emp left outer join simple.Insurance Insurance where emp.insid = Insurance.insid";
Query query = sess.createQuery(sql_query);
Iterator ite = query.list().iterator();
System.out.println("First Name\t"+"Insurance Name\t"+"InvestmentAmount");
while ( ite.hasNext() ) {
Object[] pair = (Object[]) ite.next();
/*Contact con = (Contact) pair[0];
Insurance ins = (Insurance) pair[1];
System.out.print(con.getFirstName());
System.out.print("\t"+ins.getInsuranceName());
System.out.print("\t\t"+ins.getInvestementAmount());
System.out.println();*/

System.out.println(pair[0]+"\t"+pair[1]+"\t"+pair[2]+"\n");
/*System.out.println("First Name:"+pair[0]);
System.out.println("Insurance name:"+pair[1]);
System.out.println("InvestmentAmount:"+pair[2]);*/


}
sess.close();
}
catch(Exception e ){
System.out.println(e.getMessage());
e.printStackTrace();
}

}

}

Contact.java
package simple;
import java.util.*;
public class Contact {
private String firstName;
private String lastName;
private int insid;
private int id;
private List insurance;
public List getInsurance()
{
return insurance;
}
public void setInsurance(List insurance)
{
this.insurance = insurance;
}

/**
* @return insid
*/

/**
* @return First Name
*/

public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}


public int getinsid() {
return insid;
}

/**
* @param string Sets the Insid
*/

public void setinsid(int i) {
insid = i;
}

/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}
public int getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(int i) {
id = i;
}

}

Insurance.java
package simple;

import java.util.List;

public class Insurance {

private String insuranceName;
private double investementAmount;
private int insid;
private Contact contact;
public Contact getContact()
{
return contact;
}
public void setContact(Contact contact)
{
this.contact = contact;
}


public String getInsuranceName() {
return insuranceName;
}

public void setInsuranceName(String insuranceName) {
this.insuranceName = insuranceName;
}

public double getInvestementAmount() {
return investementAmount;
}

public void setInvestementAmount(double investementAmount) {
this.investementAmount = investementAmount;
}


public int getinsid() {
return insid;
}

public void setinsid(int Id) {
this.insid = insid;
}
}
contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="simple" >
<class name="Contact" table="emp">
<id name="id" type="int" column="ID" >
<generator class="increment"/>
</id>

<property name="firstName" column ="firstname"/>
<property name="lastName" column ="lastname"/>
<property name="insid" column ="InsID"/>
<bag name="insurance" inverse="
true" cascade="all,delete-orphan">
<key column="InsID"/>
<one-to-many class="simple.Insurance"/>
</bag>
</class>
</hibernate-mapping>
insurance.hbm.xml
<?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">
<hibernate-mapping package="simple">
<class name="Insurance" table="Insurance">
<id name="insid" column ="InsID" type="int">
<generator class="increment"/>
</id>
<property name="insuranceName" column="InsuranceName type="string" />
<property name="investementAmount" column="InvestmentAmount" type="double"/>

<many-to-one name="Contact" class="simple.Contact" column="InsID"
insert="false" update="false"/>

</class>

</hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">

jdbc:mysql://localhost/sakthi</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: null pointer exception
PostPosted: Wed Mar 17, 2010 7:38 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
why there is no reply from u i am waiting for u

please help me it is very urgent

sorry for disturbing u.

advance thanks those who reply me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.