java.lang.NullPointerException在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009) [英] java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009)

查看:2610
本文介绍了java.lang.NullPointerException在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  java.lang.NullPointerException $ b $在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009)
在java.lang.Float.parseFloat(Float (UDPServer.java:73)



hibernate 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>用户名< / property>
< property name =connection.password>密码< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =connection.autoReconnect>真LT; /性>
< property name =connection.autoReconnectForPools> true< / property>
<! - < property name =c3p0.validate> true< / property> - >
< property name =current_session_context_class>线程< / 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);
}
返回传感器读数无效;
}
}

其中我收到异常的代码是

  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];
会话会话= 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(); //我在这里面临问题
c [1] =(String)ob [0]; //包含coeff 1 c1
c [0] =(String)ob [1]; //包含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;


$ / code $ / pre

我几周前发布了它,对于这个来自JB Nizet,但它只解决了一部分,现在我正面临空指针异常

  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.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)

hibernate cfg file

<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 code is

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";
}
}

code where i am getting exception is

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;
}
}

I had posted it few weeks before and i got solution for this from "JB Nizet" but it solved only one part now i am facing Null pointer exception in below part

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

I think this object is throwing null pointer exception but i don't know how solve it. can anyone help me in this.. here i am created query and iterated result set and passing it to the string array.

解决方案

If I understand correctly, You're executing the above methods several times, and after some invocations, it throws the above exception.

The problem probably comes from the fact that you're reading the Hibernate configurationand creating a whole new session factory, with a new connection pool, at each method invocation.

The following should be done once and only once. The same session factory should then be reused at each method invocation:

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

这篇关于java.lang.NullPointerException在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1009)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆