Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic


kiran gk
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
at FetchCoeff.testing(
at UDPServer.main(
Exception in thread "main" java.lang.NullPointerException
at UDPServer.main(

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");
Configuration cfg= new Configuration();
System.out.println("configuration is done");
session = cfg.buildSessionFactory().openSession();
System.out.println("connection got");
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("SensorType", y);
System.out.println("query executed");
for(Iterator it=query.iterate();it.hasNext(){
Object[] ob = (Object[]);
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
}catch(HibernateException e){
System.out.println("Hibernate exception occured");
}catch(Exception d){
System.out.println("exception occured");
return c ;
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!