• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NULL POINTER EXCEPTION IN HIBERNATE

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone
i have an issue, its an coding problem and i know its an silly mistake but i am new to this i am not able to solve so please any one help me in solving this,

I made a query to the DB and got an object which contains 2 data, i tried retreiving it using string array the compiles without an error but while executing it gives NULL POINTER EXCEPTION. I think i did some mistake while retreiving it so please can anyone give proper code to retreive data ???
The error message is as below,

[SRC]# java UDPServer
Opening a connection to database
Starting to listen for data to persist to database.
entering main
configuration is done
connection got
query executed
Coefficent 1 is :0.032 // this 2 are retrieved data from DB
Coefficent 0 is :-23.82
Device ID is : 201001000006
Sensor type is : HUM
exception occured
java.lang.NullPointerException
at FetchCoeff.testing(FetchCoeff.java:51)
at UDPServer.main(UDPServer.java:70)
Exception in thread "main" java.lang.NullPointerException
at UDPServer.main(UDPServer.java:72)



The code is


import java.util.Iterator;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;



public class FetchCoeff {
public String[] testing(String x, String y) { ///here x=DevID and y= sensor type
Session session= null ;
Transaction tx= null;
String c[] = null;


System.out.println("entering main");
Logger.getRootLogger().setLevel(Level.OFF);
Configuration cfg= new Configuration();
cfg.configure("Hibernate.cfg.xml");
System.out.println("configuration is done");
session = cfg.buildSessionFactory().openSession();
System.out.println("connection got");
try{
tx = session.beginTransaction();
String q= "SELECT d.c1, d.c0, d.DevID, d.SensorType FROM SensorReadings d WHERE d.DevID= evID AND d.SensorType= :SensorType";
Query query = session.createQuery(q);
query.setParameter("DevID",x);
query.setParameter("SensorType", y);
System.out.println("query executed");
for(Iterator it=query.iterate();it.hasNext(){
Object[] ob = (Object[]) it.next();
System.out.println("Coefficent 1 is :" + ob[0]);
System.out.println("Coefficent 0 is :" + ob[1]);
System.out.println("Device ID is : " + ob[2]);
System.out.println("Sensor type is : " + ob[3]);
c[1]= (String)ob[0]; // contains coeff 1 c1 here i am getting NULL POINTER EXCEPTION
c[0]= (String)ob[1]; // contains coeff 0 c0
tx.commit();
}
}catch(HibernateException e){
System.out.println("Hibernate exception occured");
e.printStackTrace();
}catch(Exception d){
System.out.println("exception occured");
d.printStackTrace();
}
finally
{
session.close();
}
return c ;
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic