在JFRAME上添加几个JTextField。想从中得到数据并插入到ORACLE数据库但总是抛出java.sql.SQLException: ORA-01400: 无法将 NULL 插入 ("SYSTEM"."PROJECT"."PID")异常。所有的数据在数据库中定义为char(10)代码:                    JTextField pidT = new JTextField(); JTextField pnameT = new JTextField(); JTextField psourceT = new JTextField(); JTextField pmoneyT = new JTextField(); JTextField proleT = new JTextField(); JTextField ptimeT = new JTextField();                           pid = pidT.getText();
pname = pnameT.getText();
psource = psourceT.getText();
pmoney = pmoneyT.getText();
prole = proleT.getText();
ptime = ptimeT.getText();
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String s = "insert into project values('" + pid + "','"
+ pname + "','" + psource + "','" + pmoney
+ "','" + prole + "','" + ptime + "')"; st.executeUpdate(s);
JOptionPane.showMessageDialog(null, "添加成功");
} catch (Exception ex)
{
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败");
}
}
});

解决方案 »

  1.   

    同一个问题,我就再帖一次吧      JTextField pidT = new JTextField();JTextField pnameT = new JTextField();JTextField psourceT = new JTextField();JTextField pmoneyT = new JTextField();JTextField proleT = new JTextField();JTextField ptimeT = new JTextField();                          
    b.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    pid = pidT.getText();
    pname = pnameT.getText();
    psource = psourceT.getText();
    pmoney = pmoneyT.getText();
    prole = proleT.getText();
    ptime = ptimeT.getText();

    try
    {
    String s = "insert into project values('" + pid + "','"
    + pname + "','" + psource + "','" + pmoney
    + "','" + prole + "','" + ptime + "')";st.executeUpdate(s);
    JOptionPane.showMessageDialog(null, "添加成功");
    } catch (Exception ex)
    {
    ex.printStackTrace();
    JOptionPane.showMessageDialog(null, "添加失败");
    }
    }
    });
      

  2.   

    无法将 null 插入 PID,应该是你的pid 为空了吧,你看下获取PID值的地方有没有出现错误
      

  3.   

    在执行SQL之前先输出一下PID,看看是不是为空
      

  4.   


    我在PIDT上输入了值,为什么getText()但不到呢?
      

  5.   

    我测试了以下的,getText()可以取到值啊!而且,pid = pidT.getText(); 
    pname = pnameT.getText(); 
    psource = psourceT.getText(); 
    pmoney = pmoneyT.getText(); 
    prole = proleT.getText(); 
    ptime = ptimeT.getText(); 
    只要你在文本框中输入了信息,就能在b的事件中得到数据信息。你看一下是不是你别的代码出问题了。
      

  6.   

    还没搞定,你需要在某个事件中得到值,而不是像你代码里那样,在监听事件之前去获取值(肯定为null)因为没有输入值,那个时候
      

  7.   

    lz的那几个字段都为null,数据库估计有非空的约束
    getText()不能写在那里的,程序已经来就吧那些属性赋值为null了
    在你的监听到动作后取值,可以先判断下是否为空