java.lang.NullPointerException该处理
package com.jd.sql;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import com.jd.system.*;
public class sunsql {
private static Statement ste = null;
private static Connection conn = null;
static {
try {
if(sunini.getIniKey ("Default_Link").equals ("1")) { //JDBC连接方式
String user = sunini.getIniKey ("UserID");
String pwd = sunini.getIniKey ("Password");
String ip = sunini.getIniKey ("IP");
String acc = sunini.getIniKey ("Access");
String dbf = sunini.getIniKey ("DBFname");
String url = "jdbc:mysql://localhost:3306/" + dbf;
//注册驱动
DriverManager.registerDriver (new com.mysql.jdbc.Driver());
//获得一个连接
conn = DriverManager.getConnection (url, user, pwd);
} else {
// //注册驱动 //JDBCODBC连接方式
// DriverManager.registerDriver (new sun.jdbc.odbc.JdbcOdbcDriver());
// //获得一个连接
// conn = DriverManager.getConnection ("jdbc:odbc:" + sunini.getIniKey("LinkName"));
}
//设置自动提交为false
conn.setAutoCommit (false);
//建立高级载体
ste = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
catch (Exception ex) {
}//End try
}
private sunsql(){
}
public static int executeUpdate(String sql) {
// System.out.println ("Update SQL : " + sql);
int i = 0 ;
try {
i = ste.executeUpdate(sql) ;
conn.commit();
}catch(Exception e) {
e.printStackTrace() ;
}//End try
return i ;
}
public static int runTransaction (String updateCode[]) {
int ok = 0, i = 0;
int row = updateCode.length; //更新语句的数量
try {
for (i = 0; i < row; i++) {
ok = ste.executeUpdate (updateCode[i]); //执行SQL语句
if(ok == 0) { //如果不成功,则跳出循环
break;
}
}
//根据变量 ok 判断上面循环是否正常运行完毕
if(ok == 0) {
conn.rollback (); //(ok == 0)表示更新过程中出错,回滚数据
}
else {
conn.commit (); //(ok != 0)基本上是所有SQL语句运行成功, 则提交给数据库
}
}
catch (Exception ex) {
}
return i;
}
public static ResultSet executeQuery(String sql) {
// System.out.println ("Query SQL : " + sql);
ResultSet rs = null ;
try {
rs = ste.executeQuery(sql) ;
}catch(Exception e) {
e.printStackTrace() ;
}//End try
return rs ;
}
public static int recCount(ResultSet rrs) {
int i = 0;
try {
if(rrs.getRow() != 0)
rrs.beforeFirst();
//while用于计算rs的记录条数
while(rrs.next())
i++;
rrs.beforeFirst();
}catch(Exception ex) {
ex.printStackTrace();
}//End try
return i;
}
public static long getPrimaryKey() {
long pk = 0;
try {
//获得服务器时间
ResultSet rs = executeQuery("select getdate()");
rs.next();
pk = rs.getTimestamp(1).getTime();
}
catch (Exception ex) {
System.out.println ("sunsql.getPrimaryKey (): false");
}
return pk;
}
public static void initJComboBox (JComboBox cb, String sqlCode) {
cb.removeAllItems();
try {
ResultSet rs = executeQuery (sqlCode);
int row = recCount (rs);
rs.beforeFirst ();
//从结果集中取出Item加入JComboBox中
for (int i = 0; i < row; i++) {
rs.next();
cb.addItem (rs.getString (1));
}
}
catch (Exception ex) {
System.out.println ("sunsql.initJComboBox (): false");
}
}
public static void initJList (JList jt, String sqlCode) {
try {
ResultSet rs = executeQuery (sqlCode);
int row = recCount (rs);
String list[] = new String[row];
//从结果集中取出数据存入数组中
for (int i = 0; i < row; i++) {
rs.next();
list[i] = rs.getString(1);
}//Endfor
jt.setListData(list); //初始化List
}
catch (Exception ex) {
System.out.println ("sunsql.initJList(): false");
}//Endtry
}
public static void initDTM (DefaultTableModel fdtm, String sqlCode) {
try {
ResultSet rs = executeQuery(sqlCode); //获得结果集
int row = recCount( rs ); //获取查询结果数量
ResultSetMetaData rsm =rs.getMetaData(); //获得列集
int col = rsm.getColumnCount(); //获得列的个数
String colName[] = new String[col]; //定义表格头名数组
for (int i = 0; i < col; i++) {
colName[i] = rsm.getColumnName( i + 1 ); //将表头信息设置到数组中
}
rs.beforeFirst();
String data[][] = new String[row][col];
for (int i = 0; i < row; i++) { //遍历获取查询数据
rs.next();
for (int j = 0; j < col; j++) {
data[i][j] = rs.getString (j + 1);
}
}
fdtm.setDataVector (data, colName); //设置到表格中
}
catch (Exception ex) {
System.out.println ("sunsql.initDTM (): false");
}
}
}
———
/**
package com.jd.login;
import javax.swing.*;
public class Login extends JFrame implements
ActionListener, KeyListener, ItemListener, FocusListener {
JLabel top, bott;
JComboBox cb;
JPasswordField pf;
JButton bt1, bt2;
JPanel panelMain, panelInfo;
String clue = " 提 示 : ";
int flag = 0; //登记次数记数器
public Login() {
super("系 统 登 录");
top = new JLabel (new ImageIcon("pic/login_top.jpg"));
bott = new JLabel();
panelMain = new JPanel(new BorderLayout(10, 10));
bott.setBorder(new LineBorder (new Color(184, 173, 151)));
buildCenter();
panelMain.add("North", top);
panelMain.add("South", bott);
panelMain.add(panelInfo);
//加事件监听
bt1.addActionListener(this);
bt2.addActionListener(this);
bt1.addFocusListener (this);
bt2.addFocusListener (this);
bt1.addKeyListener (this);
bt2.addKeyListener (this);
cb.addItemListener (this);
cb.addFocusListener(this);
pf.addFocusListener(this);
cb.addKeyListener (this);
pf.addKeyListener (this);
//加窗口监听 new WindowAdapter适配器类
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
quit();
}
});
this.setContentPane(panelMain); //设置窗口面板
this.setSize(350, 235);
this.setResizable (false); //设置窗口不可放大缩小
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
sunswing.setWindowCenter(this);
this.setVisible(true);
pf.requestFocus(true); //设置焦点给密码框
}
private void buildCenter() {
JLabel lb1, lb2;
JPanel pa1, pa2, pa3;
lb1 = new JLabel("用 户 名 :");
lb2 = new JLabel("登录密码 :");
cb = new JComboBox();
pf = new TJPasswordField (15);
bt1 = new TJButton ("pic/key.gif", "登 录", "登录系统");
bt2 = new TJButton ("pic/exit.gif", "退 出", "关闭系统");
sunsql.initJComboBox (cb, "select userid from pwd where delmark=0");
//定义面板为无部局
panelInfo = new JPanel(null);
//加入组件
panelInfo.add(lb1);
panelInfo.add(lb2);
panelInfo.add(cb);
panelInfo.add(pf);
panelInfo.add(bt1);
panelInfo.add(bt2);
lb1.setBounds(50,14,60,20);
lb2.setBounds(50,48,60,20);
bt1.setBounds(68,77,86,28);
bt2.setBounds(186,77,86,28);
cb.setBounds (115,12,168,23);
pf.setBounds (115,46,170,23);
//设定边框线
panelInfo.setBorder(BorderFactory.createTitledBorder(""));
}
private void quit() {
int flag = 0;
String msg = "您 现 在 要 关 闭 系 统 吗 ?";
flag = JOptionPane.showConfirmDialog(null, msg, "提示", JOptionPane.YES_NO_OPTION);
if(flag == JOptionPane.YES_OPTION) {
this.setVisible(false);
System.exit(0);
}//End if(flag == JOptionPane.YES_OPTION)
return;
}
private void login() {
String user = cb.getSelectedItem() + "";
String pwd = String.valueOf(pf.getPassword());
String code = "select pwd,puis from pwd where delmark=0 and userid='" + user + "'";
ResultSet rs = sunsql.executeQuery (code);
try {
if(rs.next()) { //用户名存在
if(pwd.equals(rs.getString(1))) {
bott.setText(clue + "登录成功,正在进入系统 ...");
String puis = rs.getString(2); //获得操作员权限
boolean flag = Journal.writeJournalInfo(user, "登录本系统", Journal.TYPE_LG);
if(flag) { //记录日志
new com.jd.mainframe.HotelFrame(user, puis); //进入主程序窗口(用户名, 权限)
this.setVisible(false);
}else {
String msg = "写日志错误,请与系统管理员联系 ...";
JOptionPane.showMessageDialog(null, msg, "错误", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
else {
bott.setText(clue + "用户 [ " + user + " ] 的密码不正确,请重新输入 ...");
flag++;
if(flag == 3) { //三次密码验证
JOptionPane.showMessageDialog(null, "您不是本系统的管理员,系统关闭 ...", "警告", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}//End if(flag == 3)
return;
}//End if(pwd.equals(rs.getString(1)))
}
else {
bott.setText(clue + "用户ID [ " + user + " ] 不存在 ...");
}//End if(rs.next())
}
catch (Exception ex) {
ex.printStackTrace();
}//End try
}
public void actionPerformed (ActionEvent ae) {
//动作监听
if(ae.getSource() == bt1) { //登录按键
login();
}
else {
quit();
}//End if(ae.getSource() == bt1)
}
public void itemStateChanged (ItemEvent ie) {
pf.requestFocus(true);
}
public void keyPressed (KeyEvent ke) {
//键盘按下监听
int key = ke.getKeyCode();
if(key == KeyEvent.VK_ENTER) {
if(ke.getSource() == cb)
pf.requestFocus(true); //将焦点从用户名给密码
if(pf.getPassword().length > 0)
login(); //按Enter键登录系统
}
else if(key == KeyEvent.VK_ESCAPE) { //按ESC键退出系统
quit();
}//End if
}
public void keyReleased (KeyEvent ke) {
//键盘释放监听
}
public void keyTyped (KeyEvent ke) {
//按键型监听
}
public void focusGained (FocusEvent fe) {
//焦点监听
if(fe.getSource() == cb) //窗口最下方的功能提示
bott.setText(clue + "请选择操作员名称 ...");
else
if(fe.getSource() == pf)
bott.setText(clue + "请输入登录密码 ...");
else
if(fe.getSource() == bt1)
bott.setText(clue + "登录系统 ...");
else
if(fe.getSource() == bt2)
bott.setText(clue + "退出系统 ...");
}
public void focusLost (FocusEvent fe) {
//失去焦点监听
}
public static void main(String sd[]) {
sunswing.setWindowStyle(sunini.getIniKey("Sys_style").charAt(0));
new FStartWindow ("pic/Login.gif", new Frame(), 1200);
new Login();
}
}
报错:
java.lang.NullPointerException
at com.jd.sql.sunsql.executeQuery(sunsql.java:92)
at com.jd.login.Login.buildCenter(Login.java:81)
sunsql.initJComboBox (): false
写不下就距离俩个,红色为错误地方
求帮忙。。 异常 --------------------编程问答-------------------- 你需要对你所要操作的对象作判空处理... --------------------编程问答-------------------- 能具体举个例子不。。。 --------------------编程问答-------------------- 晕死 ,debug一下,看哪个对象为空,依次往上找就可以解决。
你粘的这些代码,让人情可以堪 --------------------编程问答-------------------- if(obj == null)
return XXX; --------------------编程问答-------------------- 贴代码的时候不要直接ctrl C & V 看着很不舒服 对楼主问题的解决办法:1.操作对象之前 判断是否为空 2.给每个需要用的对象赋初值 --------------------编程问答-------------------- 建议单步调试。 --------------------编程问答-------------------- 空指针异常。一个一个设断点去查吧。 --------------------编程问答-------------------- 5楼,不用复制粘贴要怎么做,第一次搞这东西,求解。。。
补充:Java , Java SE