回 帖 发 新 帖 刷新版面

主题:小妹有礼了 请高手不吝指教

小妹自己写了一个水电费收费记录可是出现异常 无奈 只得求教这里的大哥大姐们知道
先说 这是异常显示:
  java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:436)
    at java.lang.Integer.parseInt(Integer.java:518)
    at Scan1.doPost(Scan1.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:165)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
    at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
    at com.caucho.server.http.Invocation.service(Invocation.java:315)
    at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:253)
    at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:170)
    at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
    at java.lang.Thread.run(Thread.java:534)

我知道是将String 转换int时出错可能出现在session转换中 但就是不知道该如何处理

回复列表 (共10个回复)

沙发

这是我写的代码;
哎呀 不能全发我就分段发送 名字我就不写了。
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WorkerList extends HttpServlet{
    static final private String content_type="text/html;charset=GB2312";
    String[] hot_water;
    String[] cool_water;
    String[] electric_quantity;
    String[] riqi;
    int firstnum=1;
    int count=0;
    public void init() throws ServletException{
        
    }
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        int i=0;

板凳

try{
            ResultSet rs=getWorks();
            while(rs.next()){
                count++;
            }
            setHot_water(count);
            setCool_water(count);
            setElectric_quantity(count);
            setRiqi(count);
            String hotWater[]=new String[count];
            hotWater=hot_water;
            String coolWater[]=new String[count];
            coolWater=cool_water;
            String electricQuantity[]=new String[count];
            electricQuantity=electric_quantity;
            String riqiCount[]=new String[count];
            riqiCount=riqi;
            response.setContentType(content_type);
            PrintWriter out=response.getWriter();
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>数据录入界面</title>");
            out.println("</head>");
            out.println("<body topmargin=5 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=#80a0a0>");
            out.println("<center>");
            out.println("<font face='华文新魏' size=8>鸿源公司水电费统计表</font>");
            out.println("<hr size=5 color=#c0ff40><br>");
            out.println("<br>");
            out.println("<form action=http://localhost:8080/12/servlet/Scan1 method=post>");
            out.println("<table topmargin=0 border=1 bgcolor=#80a0a0 width=780>");
            out.println("<tr>");
            out.println("<td>用户名</td>");
            out.println("<td>楼房号</td>");
            out.println("<td>凉水量</td>");
            out.println("<td>热水量</td>");
            out.println("<td>用电量</td>");
            out.println("<td rowspan=3>");
            out.println("<p align=left>年</p>");
            out.println("<select name=year>");
            out.println("<option value=2006>2006</option>");
            out.println("<option value=2007>2007</option>");
            out.println("<option value=2008>2008</option>");
            
            out.println("</select>");
            out.println("<p align=left>月</p>");
            out.println("<select name=month>");
            out.println("<option value=1>01</option>");
            out.println("<option value=2>02</option>");
            out.println("<option value=3>03</option>");
            out.println("<option value=4>04</option>");
            out.println("<option value=5>05</option>");
            out.println("<option value=6>06</option>");
            out.println("<option value=7>07</option>");
            out.println("<option value=8>08</option>");
            out.println("<option value=9>09</option>");
            out.println("<option value=10>10</option>");
            out.println("<option value=11>11</option>");
            out.println("<option value=12>12</option>");
            out.println("</select>");
            out.println("</td>");
            out.println("</tr>");
            String number=String.valueOf(count);
            HttpSession session=request.getSession();
            session.setAttribute("number",number);
            rs.beforeFirst();
            while(rs.next()){
            String userName=rs.getString("用户名");
            String add=rs.getString("房间号");
            out.println("<tr>");
            out.println("<td align=left >"+userName+"</td>");
            out.println("<td align=left >"+add+"</td>");
            out.println("<td><input type=text name=cool_water["+i+"] ></td>");
            out.println("<td><input type=text name=hot_water["+i+"] ></td>");
            out.println("<td><input type=text name=electric_quantity["+i+"] ></td>");
            i++;

3 楼

}
            out.println("</tr></table></center><br>");
            out.println("<p align=center>");
            out.println("<input type=submit name=sub value=提交>");
            out.println("</p>");
            out.println("</form></body></html>");
    }catch(Exception e){
        e.printStackTrace();
        }
    }
    public ResultSet getWorks(){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:shuidian");
            Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet rs=stm.executeQuery("select * from user");
            return rs;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public void setHot_water(int waterCount){
        if(firstnum==1){
            hot_water=new String[count];
            for(int a=0;a<count;a++){
                hot_water[a]="0";
            }
        }
        firstnum++;
    }
    public void setCool_water(int coolCount){
        if(firstnum==1){
            cool_water=new String[count];
            for(int a=0;a<count;a++){
                cool_water[a]="0";
            }
        }
        firstnum++;
    }

4 楼

public void setElectric_quantity(int ele_count){
        if(firstnum==1){
            electric_quantity=new String[count];
            for(int a=0;a<count;a++){
                electric_quantity[a]="0";
            }
        }
        firstnum++;
    }
    public void setRiqi(int riqinum){
        if(firstnum==1){
            riqi=new String[count];
            for(int a=0;a<count;a++){
                riqi[a]="0";
            }
        }
        firstnum++;
    }
    public void destroy(){}
}

5 楼

上面的是一个文件 然后传入下面的文件
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class Scan extends HttpServlet{
    static final private String content_type="text/html;charset=GB2312";
    Vector vector=new Vector();
    int num;
    public void init() throws ServletException{    }
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
            response.setContentType(content_type);
            PrintWriter out=response.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<title>数据浏览界面</title>");
            out.println("</head>");
            out.println("<body topmargin=5 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=#80a0a0>");
            out.println("<center>");
            out.println("<font face='华文新魏' size=8>鸿源公司水电费统计表</font>");
            out.println("<hr size=5 color=#c0ff40><br>");
            out.println("<br>");
            Vector info_vec=new Vector();
            
        
            int coolTotalQuantity=0;
            int hotTotalQuantity=0;
            int eleTotalQuantity=0;

            double hotPrice=3.00;
            double coolPrice=2.95;
            double elePrice=0.50;

            num=0;
            out.println("the dates you inputed are:</br>");
            out.println("<form action=http://localhost:8080/12/servlet/AddInformation method=post>");
            out.println("<table border=1><tr>");
            out.println("<td>用户名</td>");
            out.println("<td>房间号</td>");
            out.println("<td>凉水单价</td>");
            out.println("<td>凉水用量</td>");
            out.println("<td>凉水总费用</td>");
            out.println("<td>热水单价</td>");
            out.println("<td>热水用量</td>");
            out.println("<td>热水总费用</td>");
            out.println("<td>用电单价</td>");
            out.println("<td>用电量</td>");
            out.println("<td>用电总费用</td>");
            out.println("<td>年月份</td>");
            out.println("<td>总费用</td></tr>");
            int i=0;

6 楼

try{
                ResultSet rs=getList();
                while(rs.next()){
                    num++;
                }
                String[] true_value1=new String[num];
                String[] true_value2=new String[num];
                String[] true_value3=new String[num];
                String riqi1=request.getParameter("year");
                String riqi2=request.getParameter("month");
                String riqi=riqi1+"/"+riqi2;
                for(int j=0;j<num;j++){
                    true_value1[j]=request.getParameter("cool_water["+j+"]");
                    true_value2[j]=request.getParameter("hot_water["+j+"]");
                    true_value3[j]=request.getParameter("electric_quantity["+j+"]");
                    
                }
                rs.beforeFirst();
                while(rs.next()){
                    out.println("<tr>");
                    String userName=rs.getString("用户名");
                    String address=rs.getString("房间号");
                        Vector vec=new Vector();
                        vec.addElement(userName);
                        vec.addElement(address);
                        vec.addElement(String.valueOf(coolPrice));
                        vec.addElement(true_value1[i]);
                        double coolWater_price=coolPrice * Double.parseDouble(true_value1[i]);
                        vec.addElement(String.valueOf(coolWater_price));
                        vec.addElement(String.valueOf(hotPrice));
                        vec.addElement(true_value2[i]);
                        double hotWater_price=hotPrice * Double.parseDouble(true_value2[i]);
                        vec.addElement(String.valueOf(hotWater_price));
                        vec.addElement(String.valueOf(elePrice));
                        vec.addElement(true_value3[i]);
                        double ele_price=elePrice * Double.parseDouble(true_value3[i]);
                        vec.addElement(String.valueOf(ele_price));
                        vec.addElement(riqi);
                        double totalPrice=coolWater_price+hotWater_price+ele_price;
                        vec.addElement(String.valueOf(totalPrice));
                        info_vec.addElement(vec);
                        i++;
                }
            }catch(Exception e){
                e.printStackTrace();
            }

7 楼

setvector(info_vec);
            for(int j=0;j<info_vec.size();j++){
               for(int k=0;k<13;k++){
                if(k==12){
                    out.println("<td>"+getValueAt(j,k)+"</td></tr>");
                }else{
                    out.println("<td>"+getValueAt(j,k)+"</td>");
                }
                      }
                
                coolTotalQuantity=coolTotalQuantity+Integer.parseInt(getValueAt(j,3).toString());
                hotTotalQuantity=hotTotalQuantity+Integer.parseInt(getValueAt(j,6).toString());
                eleTotalQuantity=eleTotalQuantity+Integer.parseInt(getValueAt(j,9).toString());
                out.println("<tr>");
            }
            out.println("</tr></table>");
            out.println("</tr></table>");
            
            HttpSession session=request.getSession();
            session.setAttribute("vector",vector);
            out.println("<p align=center>凉水总量:"+coolTotalQuantity+"</p>");
            out.println("<p align=center>热水总量:"+hotTotalQuantity+"</p>");
            out.println("<p align=center>用电总量:"+eleTotalQuantity+"</p>");
            out.println("<p align=center>");
            out.println("<input type=submit value=confim>");
            out.println("<p>");
            out.println("</form>");
            out.println("</body></html>");
        }
        public Object getValueAt(int row,int column){
            if(! vector.isEmpty()){
                return ((Vector)vector.elementAt(row)).elementAt(column);
            }else{
                return null;
            }
        }
    public ResultSet getList(){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:shuidian");
            Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet rs=stm.executeQuery("select * from user");
            return rs;
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return null;
    }
    public void setvector(Vector v){
        vector=v;
    }
    public void destroy(){}
}

8 楼

关键的错误就出现在这一个里面了 还望大哥大姐们带着雷锋的精神帮俺看看怎么处理
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AddInformation extends HttpServlet{
    static final private String content_type="text/html;charset=GB2312";
    Connection con=null;
    Vector vector=new Vector();
    public void init() throws ServletException{}
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        response.setContentType(content_type);
        PrintWriter out=response.getWriter();
        HttpSession session=request.getSession();
        vector=(Vector)session.getAttribute("vector");
        out.println("<html>");
        out.println("<head><title>添加页面</title></head>");
        out.println("<body>");
        Vector ve=new Vector();
        ve=vector;
        String number=(String)session.getAttribute("number");
        int num=Integer.parseInt(number);
        
        
        int coolTotalQuantity=0;
        int hotTotalQuantity=0;
        int eleTotalQuantity=0;


        for(int i=0;i<ve.size();i++){
            
            String user=String.valueOf(getValueAt(i,0).toString());
            String addr=String.valueOf(getValueAt(i,1).toString());
            double coolWater_price=Double.parseDouble(getValueAt(i,2).toString());
            int coolWater_quantity=Integer.parseInt(getValueAt(i,3).toString());
            double coolWater_Tprice=Double.parseDouble(getValueAt(i,4).toString());
            double hotWater_price=Double.parseDouble(getValueAt(i,5).toString());
            int hotWater_quantity=Integer.parseInt(getValueAt(i,6).toString());
            double hotWater_Tprice=Double.parseDouble(getValueAt(i,7).toString());
            double ele_price=Double.parseDouble(getValueAt(i,8).toString());
            int ele_quantity=Integer.parseInt(getValueAt(i,9).toString());
            double ele_Tprice=Double.parseDouble(getValueAt(i,10).toString());
            String riqi=String.valueOf(getValueAt(i,11).toString());
            double totalPrice=Double.parseDouble(getValueAt(i,12).toString());
            coolTotalQuantity+=coolWater_quantity;
            hotTotalQuantity+=hotWater_quantity;
            eleTotalQuantity+=ele_quantity;
            addRecord(user,addr,coolWater_price,coolWater_quantity,coolWater_Tprice,hotWater_price,hotWater_quantity,hotWater_Tprice,ele_price,ele_quantity,ele_Tprice,riqi,totalPrice);
            
            
        }

9 楼

addTotalQuantity(coolTotalQuantity,hotTotalQuantity,eleTotalQuantity);
        out.println("<p align=center>sucess</p>");
    }
    public void addRecord(String user,String addr,double coolWater_price,int coolWater_quantity,double coolWater_Tprice,double hotWater_price,int hotWater_quantity,double hotWater_Tprice,double ele_price,int ele_quantity,double ele_Tprice,String riqi,double totalPrice){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:shuidian");
            Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql="insert into information(用户名,房间号,凉水单价,凉水用量,凉水总费用,热水单价,热水用量,热水总费用,用电量,用电单价,用电总费用,年月份,总费用) values ('"+user+"','"+addr+"','"+coolWater_price+"','"+coolWater_quantity+"','"+coolWater_Tprice+"','"+hotWater_price+"','"+hotWater_quantity+"','"+hotWater_Tprice+"','"+ele_quantity+"','"+ele_price+"','"+ele_Tprice+"','"+riqi+"','"+totalPrice+"')";
            stm.executeUpdate(sql);
            stm.close();
            
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    public void addTotalQuantity(int coolTotalQuantity,int hotTotalQuantity,int eleTotalQuantity){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:shuidian");
            Statement stm2=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql2="insert into totalQuantity(凉水总量,热水总量,用电总量) values ('"+coolTotalQuantity+"','"+hotTotalQuantity+"','"+eleTotalQuantity+"')";
            stm2.executeUpdate(sql2);
            stm2.close();
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    public Object getValueAt(int row,int column){
        if(! vector.isEmpty()){
            return ((Vector)vector.elementAt(row)).elementAt(column);
        }else{
            return null;
        }
    }
    public void destroy(){}
}

10 楼

发完了。反正我估计错误就是出现在8楼的String number=(String)session.getAttribute("number");
        int num=Integer.parseInt(number);
里面 有什么好的解决方案吗?我曾经尝试过将后2个文件合并可还是出现那种该死的错误。小妹先谢过大家了 ~.~

我来回复

您尚未登录,请登录后再回复。点此登录或注册