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>


转换阅读代码为

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”那里得到了解决方案,但是它只解决了一部分,而现在我在下面的部分中遇到了Null指针异常

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


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

最佳答案

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

该问题可能是由于您在每次方法调用时都在读取Hibernate配置并创建一个带有新连接池的全新会话工厂而产生的。

以下应仅执行一次。然后应在每次方法调用时重用同一会话工厂:

Configuration cfg= new Configuration();
cfg.configure("Hibernate.cfg.xml");
SessionFactory sessionfactory = cfg.buildSessionFactory();

关于java - sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009)处的java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11186755/

10-09 05:26