抛出空指针异常&我不明白为什么这样 [英] Throws Null pointer Exception & i'm not getting why this so

查看:119
本文介绍了抛出空指针异常&我不明白为什么这样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码&我通过jsp页面调用它,但它在这一行给我

NullPointerException

i am having the following code & i'm calling it through the jsp page but it giving me the
NullPointerException at this line

cs.setDate(6,(Date)df.parse(ptechdatabean.getFrom_Date()));



因为im使用java,jsp,jdbc,oracle 10g可以有人告诉我为什么会这样....




as i m using java, jsp, jdbc, oracle 10g can someone tell me why this is so....

package Ptech;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.sql.Date;

public class User_Master_procedure {

    public ArrayList Call_User_Master_procedure(PtechDataBean ptechdatabean) throws ClassNotFoundException, SQLException, ParseException {

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
        DatabaseConnection db = new DatabaseConnection();
        Connection Connect = db.Oracle_Connect();
        CallableStatement cs = Connect.prepareCall("{call Pr_User_Master_Insert(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
        cs.setString(1, ptechdatabean.getCompany_id());
        cs.setString(2, ptechdatabean.getBranch_id());
        cs.setString(3, ptechdatabean.getUser_id());
        cs.setString(4, ptechdatabean.getName());
        cs.setString(5, ptechdatabean.getPassword());
        cs.setDate(6,(Date)df.parse(ptechdatabean.getFrom_Date()));
        cs.setDate(7,(Date)df.parse(ptechdatabean.getTo_Date()));
        cs.setString(8,ptechdatabean.getStatus());
        cs.setInt(9, ptechdatabean.getEntered_By());
        cs.setDate(10,(Date)df.parse(ptechdatabean.getEntered_Date()));
        cs.setInt(11, ptechdatabean.getUpdated_By());
        cs.setDate(12, (Date)df.parse(ptechdatabean.getUpdate_Date()));
        cs.setString(13, ptechdatabean.getIp_Address());
        cs.setString(14, ptechdatabean.getMac_Address());
        cs.setString(15, ptechdatabean.getCard_Id());
        cs.setString(16, ptechdatabean.getUser_Name());
        cs.setString(17, ptechdatabean.getFlag());
        cs.registerOutParameter(18, Types.VARCHAR);
        cs.registerOutParameter(19, Types.VARCHAR);
        cs.executeUpdate();
        Connect.commit();
        ArrayList al = new ArrayList();
        {
            al.add(cs.getString(18));
            al.add(cs.getString(19));
        }

        return al;
    }
}

推荐答案

您可能无法解析日期。



请多加一行并检查格式。

最好保持默认格式:



You are probably failing at parsing the date.

Please make that an extra line and check the format.
It's probably best to keep it to the default format:

DateFormat df = new SimpleDateFormat();//"MM/dd/yyyy"); 

// otherCode();

String dateValue = ptechdatabean.getFrom_Date();
Date date = df.parse(dateValue);
cs.setDate(6, date);


确保你的字段'尝试来自包含您正在格式化的格式的日期。如果date字段为null,则抛出NullPointerException,否则抛出ParseException。在你的情况下,我认为你的bean没有传递日期,检查你的代码抽象,看看问题到底在哪里然后尝试以下。

Ensure that the field you're trying to get from contains a date in the format you are formatting. If the date field is null, the NullPointerException is thrown, otherwise it throws the ParseException. In your case, I think your bean is not passing the date, check your code abstraction to see where the problem really is and then try the following.
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// You don't have to cast to Date as the returned value is already formatted as a date.
cs.setDate(6, df.parse(ptechdatabean.getFrom_Date());


这篇关于抛出空指针异常&我不明白为什么这样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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