首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

Exception in thread "main" java.lang.NullPointerException有关问题求教

2012-05-22 
Exception in thread main java.lang.NullPointerException问题求教!下面是源代码出现的异常是Exception

Exception in thread "main" java.lang.NullPointerException问题求教!
下面是源代码出现的异常是
Exception in thread "main" java.lang.NullPointerException
at com.qq.server.db.Text10.<init>(Text10.java:58)
at com.qq.server.db.Text10.main(Text10.java:39)
58行是 jp2.add(jb2);,39行是Text10 text= new Text10();求教这问题怎么解决??



package com.qq.server.db;
import javax.swing.*;
import javax.xml.soap.Text;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
//完成一个迷你版的用户信息管理系统
public class Text10 extends JFrame implements ActionListener{

JPanel jp1,jp2;

JLabel jl1;
  JButton jb1,jb2,jb3,jb4;
  JTable jt;
JScrollPane jsp;
JTextField jtf;


Vector rowData, columnNames;
//rowData用户来存放行数据
//后者用来存放列明


//数据库操作需要定义的变量
PreparedStatement ps= null;
Connection ct = null;
ResultSet rs= null;

String url="jdbc:sqlserver://127.0.0.1:1433;databaseName=QQ";
String user="sa";
String passwd="322008";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Text10 text= new Text10();
}

public Text10(){
 
jp1=new JPanel();
jtf=new JTextField(10);
jb1 = new JButton("查询");
jb1.addActionListener(this);
jl1 = new JLabel("请输入名字:");
 
jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);
 
jb2= new JButton("添加");
jb3= new JButton("修改");
jb4= new JButton("注销");
 
 
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
 
//中间
//设置列名
columnNames= new Vector();
columnNames.add("号码");
columnNames.add("昵称");
columnNames.add("性别");
columnNames.add("密码");
columnNames.add("状态");

rowData= new Vector();
//从数据库读取数据
 
try{
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct=DriverManager.getConnection(url,user,passwd);

ps=ct.prepareStatement("select * from personalinfo ");
rs=ps.executeQuery();

while(rs.next()){
Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));

rowData.add(hang);
}


}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(ct!=null)
ct.close();

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
 
jt = new JTable( rowData,columnNames);
 
jsp= new JScrollPane(jt);
 
this.add(jsp);
this.add(jp1,"North");
this.add(jp2,"South");

this.setVisible(true);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 
}


[解决办法]
jp2 没有new 所以是null
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);

热点排行