1
       java.lang.NullPointerException
       at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009)
       at java.lang.Float.parseFloat(Float.java:439)
       at ConvertReadings.Convertd(ConvertReadings.java:39)
       at UDPServer.main(UDPServer.java:73)

休眠cfg文件

<hibernate-configuration>
             <session-factory>

        <property name="connection.driver_class">
            com.mysql.jdbc.Driver</property>
        <property name="connection.url">
        jdbc:mysql://localhost:1111/DB</property>
        <property name="connection.username">username</property>
        <property name="connection.password">password</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.autoReconnect"> true</property>
            <property name="connection.autoReconnectForPools">true</property>
        <!--  <property name="c3p0.validate">true</property> -->
            <property name="current_session_context_class">thread</property>
            <property name="hibernate.format_sql">false</property>
            <property name="hibernate.connection.pool_size">5</property> 
            <property name="hibernate.c3p0.acquire_increment">1</property> 
        <property name="hibernate.c3p0.idle_test_period">1000</property> 
        <property name="hibernate.c3p0.max_size">5</property> 
        <property name="hibernate.c3p0.max_statements">0</property> 
        <property name="hibernate.c3p0.min_size">3</property> 
        <property name="hibernate.c3p0.timeout">5000</property>

ConvertReadings 代码是

public class ConvertReadings {
public String Convertd(String a, String b, String c1, String c0 ) throws Exception
{
    Converter con= new Converter();
    int x;
    double i = Float.parseFloat(a);
    if (b.equalsIgnoreCase("HUM"))
    {
        double c=Float.parseFloat(c1);
        double d=Float.parseFloat(c0);
        x=con.convert(c, d, i);
        return String.valueOf(x);
    }
    else if (b.equalsIgnoreCase("CO"))
    {
        double c=Float.parseFloat(c1);;
        double d=Float.parseFloat(c0);
        x=con.convert(c, d, i);
        return String.valueOf(x);
}
    return "Invalid sensor reading";
}
}

我遇到异常的代码是

import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
public class FetchCoeff {
public SessionFactory factory;
public String[] testing(String x, String y) { ///here x=DevID and y= sensor type
//Session session= null ;
String c[] = new String[30];
Session session = factory.openSession(); 
    try{
String q= "SELECT d.c1, d.c0, d.DevID, d.SensorType FROM SensorReadings d WHERE d.DevID= :DevID 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();       // I am Facing problem here
        c[1]= (String)ob[0];  // contains coeff 1 c1
        c[0]= (String)ob[1];  // contains coeff 0 c0    
     }
}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;
}
}

几周前我已经发布了它,我从“JB Nizet”那里得到了解决方案,但它只解决了一部分,现在我在下面的部分面临空指针异常

                for(Iterator it=query.iterate();it.hasNext();){
             Object[] ob = (Object[]) it.next();
                c[1]= (String)ob[0];
                c[0]= (String)ob[1];

我认为这个对象正在抛出空指针异常,但我不知道如何解决它。任何人都可以在这方面帮助我.. 在这里我创建了查询和迭代结果集并将其传递给字符串数组。

4

1 回答 1

0

如果我理解正确,您正在多次执行上述方法,并且在一些调用之后,它会引发上述异常。

问题可能来自这样一个事实,即您正在阅读 Hibernate 配置并在每次方法调用时创建一个带有新连接池的全新会话工厂。

以下应该做一次且只做一次。然后应该在每次方法调用时重用相同的会话工厂:

Configuration cfg= new Configuration();
cfg.configure("Hibernate.cfg.xml");
SessionFactory sessionfactory = cfg.buildSessionFactory();
于 2012-06-25T10:59:38.073 回答