空指针异常问题!!! 在线等待大师答疑解决!

咚咚来了 2013-10-24 05:10:23
package xiaomo;

import java.sql.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


class BuildButton extends JDialog {

connect a = new connect();
public Connection connection;
static PreparedStatement sql;

public BuildButton(MessageFace messageface,String title){

super(messageface,"Please finish writing .",true);
System.out.println("_______________________________");
//利用箱式布局管理器设计Name栏mb
//Birthday标签
Box IDBox = Box.createHorizontalBox();
IDBox.add(Box.createHorizontalStrut(30));
JLabel IDLabel = new JLabel("ID:");
IDBox.add(IDLabel);

IDBox.add(Box.createHorizontalStrut(20));
final JTextField IDTextField = new JTextField(30);
IDBox.add(IDTextField);
IDBox.add(Box.createHorizontalStrut(200));

//Name标签
Box NameBox = Box.createHorizontalBox();
// container.add(NameBox,BorderLayout.NORTH);
NameBox.add(Box.createHorizontalStrut(30));
JLabel NameLabel = new JLabel("NAME:");
NameBox.add(NameLabel);


NameBox.add(Box.createHorizontalStrut(20));
final JTextField NameTextField = new JTextField(30);
NameBox.add(NameTextField);
NameBox.add(Box.createHorizontalStrut(200));

//Number标签
Box NumberBox = Box.createHorizontalBox();
NumberBox.add(Box.createHorizontalStrut(30));
JLabel NumberLabel = new JLabel("MobilePhone:");
NumberBox.add(NumberLabel);

NumberBox.add(Box.createHorizontalStrut(20));
final JTextField NumberTextField = new JTextField(30);
NumberBox.add(NumberTextField);
NumberBox.add(Box.createHorizontalStrut(200));

//QQNumber标签
Box QQNumberBox = Box.createHorizontalBox();
QQNumberBox.add(Box.createHorizontalStrut(30));
JLabel QQNumberLabel = new JLabel("QQNumber:");
QQNumberBox.add(QQNumberLabel);

QQNumberBox.add(Box.createHorizontalStrut(20));
final JTextField QQNumberTextField = new JTextField(30);
QQNumberBox.add(QQNumberTextField);
QQNumberBox.add(Box.createHorizontalStrut(200));

//Company标签
Box CompanyBox = Box.createHorizontalBox();
CompanyBox.add(Box.createHorizontalStrut(30));
JLabel CompanyLabel = new JLabel("Company:");
CompanyBox.add(CompanyLabel);

CompanyBox.add(Box.createHorizontalStrut(20));
final JTextField CompanyTextField = new JTextField(30);
CompanyBox.add(CompanyTextField);
CompanyBox.add(Box.createHorizontalStrut(200));

JButton b1 = new JButton("Finish");
JButton b2 = new JButton("Continue");

/*
* 要想使得JPanel面板划分窗体空间,
* 就必须使用网格布局管理器GridLayout和边界布局管理器BorderLayout
* 而且在添加组件是要注意:
* 先在container里对面板进行整体设置
* 而后在个面板中设置GridLayout
* 最后再添加到container中...
*/


//对container进行设置
Container container = getContentPane();
container.setLayout(new GridLayout(6,2,8,9));
setBounds(900, 50, 415, 220);
container.setBackground(Color.pink);

//对面板(容器的一种)进行设置
JPanel p1 = new JPanel(new GridLayout(1,2,8,9)); //行,列,水平,垂直
p1.setBackground(Color.pink);
JPanel p2 = new JPanel(new GridLayout(1,2,8,9));
p2.setBackground(Color.pink);
JPanel p3 = new JPanel(new GridLayout(1,2,8,9));
p3.setBackground(Color.pink);
JPanel p4 = new JPanel(new GridLayout(1,2,8,9));
p4.setBackground(Color.pink);
JPanel p5 = new JPanel(new GridLayout(1,2,8,9));
p5.setBackground(Color.pink);
JPanel p6 = new JPanel(new GridLayout(1,2));

//将组件添加到面板,再添加到container
p1.add(IDBox,BorderLayout.NORTH);
p1.add(IDTextField);

p2.add(NameBox,BorderLayout.NORTH);
p2.add(NameTextField);

p3.add(NumberBox,BorderLayout.NORTH);
p3.add(NumberTextField);

p4.add(QQNumberBox,BorderLayout.NORTH);
p4.add(QQNumberTextField);

p5.add(CompanyBox,BorderLayout.NORTH);
p5.add(CompanyTextField);

p6.add(b1,BorderLayout.NORTH);
p6.add(b2,BorderLayout.NORTH);

container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
container.add(p5);
container.add(p6);


///////////////////////////////////////////////提交添加
// if (e.getSource() == b1[5]) {
// if ((classidT.getText().trim()).equals("") ||
// (nameT.getText().trim()).equals("") ||
// (sexT.getText().trim()).equals("") ||
// (ageT.getText().trim()).equals("") ||
// (callT.getText().trim()).equals("") ||
// (departmentT.getText().trim()).equals(""))
// {
// JOptionPane.showMessageDialog(null, "请先点击添加键");
// } else {
// classid = classidT.getText();
// name = nameT.getText();
// sex = sexT.getText();
// age = ageT.getText();
// call = callT.getText();
// department = departmentT.getText();
// String strSQL ="insert into stuinfo(classid,name,sex,age,call,department)"+ "values( '"+
// classid +"' , '"+name +"' , '"+ sex +"' , '"+ age +"' , '"+ call +"' , '"+
// department +"' )";
// try {
// st.executeUpdate(strSQL);
// } catch (Exception exx) {
// JOptionPane.showMessageDialog(null, "添加失败");
// return;
// }
// JOptionPane.showMessageDialog(null, "添加成功");
// classidT.setText("");
// nameT.setText("");
// sexT.setText("");
// ageT.setText("");
// callT.setText("");
// departmentT.setText("");
//
// }
// }

b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){


System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
try {
System.out.println("**************************************************************");
sql = connection.prepareStatement("insert into xiaomotable" + "values(?,?,?,?,?)");
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
sql.setString(1, IDTextField.getText());
sql.setString(2, NameTextField.getText());
sql.setString(3, NumberTextField.getText());
sql.setString(4, QQNumberTextField.getText());
sql.setString(5, CompanyTextField.getText());
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

});
}
}


程序执行到
("**************************************************************");
sql = connection.prepareStatement("insert into xiaomotable" + "values(?,?,?,?,?)");
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
这里的sql=...就停止了,求各位大师高人指点迷津,帮忙调试,小弟不甚感激!!!
...全文
1210 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
咚咚来了 2013-10-24
  • 打赏
  • 举报
回复
结贴是时现故障,如何在被挡住的小文本框里输入分数,郁闷,刷了n次也不管用,各位大师,原谅!!!
咚咚来了 2013-10-24
  • 打赏
  • 举报
回复
谢谢各位师父,错误没了!但是为什么检查mysql数据库时,却没有添加进去!数据库xiaomo,数据库表是xiaomotable. 谢谢
咚咚来了 2013-10-24
  • 打赏
  • 举报
回复
没!!!小弟刚学,惭愧
失落夏天 2013-10-24
  • 打赏
  • 举报
回复
昨天记得好像和你说过 public Connection connection;的获取方法,貌似你方法中没用到。 connection为空。
白开水MD5 2013-10-24
  • 打赏
  • 举报
回复
String driver = "com.mysql.jdbc.Driver";//驱动程序名  
    String url = "jdbc:mysql://127.0.0.1:3306/xiaomo";  
    String user = "root";     
    String password = "";      
    try {         // 加载驱动程序  
    Class.forName(driver);  
    Connection con = DriverManager.getConnection(url, user, password);// 连续数据库  
昨天就是这个问题,还没搞定啊
末日哥 2013-10-24
  • 打赏
  • 举报
回复
jdbc哪几个基本步骤你还没记住啊,赶紧去看会儿书复习一下再做这个。。
咚咚来了 2013-10-24
  • 打赏
  • 举报
回复
public static Connection connection = DriverManager.getConnection(url, user, password);
即使是这样加上去,也是有问题的!!!
zhuweisyyc 2013-10-24
  • 打赏
  • 举报
回复
try { // 加载驱动程序 Class.forName(driver); // 连续数据库 Connection conn = DriverManager.getConnection(url, user, password);
咚咚来了 2013-10-24
  • 打赏
  • 举报
回复
各位大神师父,可以具体点吗? 最好是怎么给的也写上, 麻烦了 .... 谢谢
末日哥 2013-10-24
  • 打赏
  • 举报
回复
你没有得到连接啊。
teemai 2013-10-24
  • 打赏
  • 举报
回复
connection对象没有实例化。
zhuweisyyc 2013-10-24
  • 打赏
  • 举报
回复
你下面的 connection没有初始化。 connect a = new connect(); public Connection connection;

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧