求救啊 !!!!十万火急!!!!

时间:2022-02-10 00:43:16
小弟在做Java  frame时候老是报空指针,而且还是同一个务必请各位大神帮帮忙!!!





public static void showCenter(JFrame frame, JDialog dialog)  {

Dimension frameSize =new Dimension();

frameSize =frame.getSize();//这是错误所在地,  frame是null




Point frameCoordinate = frame.getLocation();
Dimension dialogSize = dialog.getSize();
dialog.setLocation((frameSize.width - dialogSize.width) / 2
+ frameCoordinate.x, (frameSize.height - dialogSize.height) / 2
+ frameCoordinate.y);
dialog.setVisible(true);
}

6 个解决方案

#1


调用那边出了问题,传了null过来

#2


你好   我代码字数比较多   不知道该怎么上传   请指教一下

#3


package com.jiaotong.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.jiaotong.contrl.ImageGetter;
import com.jiaotong.contrl.MenuFactory;
import com.jiaotong.contrl.WindowCenter;


public class EmpAddingView extends JDialog {

private static final long serialVersionUID = 1L;

// 职员姓名
private JTextField txtName = null;

// 年龄
private JTextField txtAge = null;

//身份证号

private JTextField txtShenfenzhenhao = null;

//籍贯

private JTextField txtJiguan = null;

// 性别
private JComboBox cboSex = null;

// 职务
private JTextField txtPosition = null;

// 备注
private JTextArea txaRemark = null;

// 添加按钮
private JButton btnAdd = null;

// 关闭按钮
private JButton btnClose = null;

// 重置按钮
private JButton btnReset = null;

private MenuFactory frame;




public EmpAddingView(MenuFactory frame, String title) {
super(frame, title);

try {
this.frame = frame;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
//setVisible(true);
WindowCenter.showCenter(frame,this);
} catch (Exception e) {
e.printStackTrace();
}
}


private void init() throws Exception {
txtName = new JTextField();
txtAge = new JTextField();
cboSex = new JComboBox();
txtPosition = new JTextField();
txtShenfenzhenhao = new JTextField();
txtJiguan = new JTextField();


txaRemark = new JTextArea();
btnAdd = new JButton("添加");
btnClose = new JButton("关闭");
btnReset = new JButton("重置");
JLabel lblName = new JLabel("姓名:");
JLabel lblAge = new JLabel("年龄:");
JLabel lblSex = new JLabel("性别:");
JLabel lblPosition = new JLabel("职务:");
JLabel lblShenfenzhenhao = new JLabel("身份证号:");
JLabel lblJiguan = new JLabel("籍贯:");
JLabel lblRemark = new JLabel("备注:");
JScrollPane scp = new JScrollPane();
JLabel lblImg = new JLabel();

getContentPane().setLayout(null);
lblName.setBounds(20, 20, 50, 20);
add(lblName);
txtName.setBounds(70, 20, 100, 20);
add(txtName);
lblAge.setBounds(180, 20, 50, 20);
add(lblAge);
txtAge.setBounds(230, 20, 100, 20);
add(txtAge);
lblSex.setBounds(20, 50, 50, 20);
add(lblSex);
cboSex.addItem("男");
cboSex.addItem("女");
cboSex.setBounds(70, 50, 100, 20);
add(cboSex);
lblPosition.setBounds(180, 50, 50, 20);
add(lblPosition);
txtPosition.setBounds(230, 50, 100, 20);
add(txtPosition);

lblShenfenzhenhao.setBounds(15, 80, 100, 20);
add(lblShenfenzhenhao);
txtShenfenzhenhao.setBounds(70, 80, 100, 20);
add(txtShenfenzhenhao);

lblJiguan.setBounds(180, 80, 50, 20);
add(lblJiguan);
txtJiguan.setBounds(230, 80, 100, 20);
add(txtJiguan);


lblRemark.setBounds(20, 110, 50, 20);
add(lblRemark);
scp.getViewport().add(txaRemark);
scp.setBounds(20, 130, 150, 60);
add(scp);
lblImg.setIcon(ImageGetter.getIcon("Rose_SYSConfiguration" + File.separator
+ "img" + File.separator + "addemp.gif"));
lblImg.setBounds(200, 80, 100, 100);
add(lblImg);
btnAdd.setBounds(50, 200, 60, 20);
add(btnAdd);
btnReset.setBounds(145, 200, 60, 20);
add(btnReset);
btnClose.setBounds(235, 200, 60, 20);
add(btnClose);
txaRemark.setLineWrap(true);
txaRemark.setWrapStyleWord(true);
setSize(350, 260);
setModal(true);
setResizable(false);
btnReset.setActionCommand("btnReset");
btnClose.setActionCommand("btnClose");
btnAdd.setActionCommand("btnAdd");


/* 重置按钮事件处理 */
btnReset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
                    
if (e.getActionCommand().equals("btnReset")) {
txtName.setText("");
txtAge.setText("");

txtPosition.setText(""); 
txtShenfenzhenhao.setText("");  
txtJiguan.setText(""); 

txaRemark.setText("");
txtName.requestFocus();

}
}
});

/* 关闭按钮事件处理 */
btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("btnClose")) {
dispose();

}




}
});

/* 添加按钮事件处理 */

btnAdd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("btnAdd")) {
dispose();

}


}
});

}


}

#4


package com.jiaotong.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.jiaotong.contrl.ImageGetter;
import com.jiaotong.contrl.MenuFactory;
import com.jiaotong.contrl.WindowCenter;

/**
 * 用户界面类--添加职员界面
 *
 */
public class EmpAddingView extends JDialog {
/**
 *
 */
private static final long serialVersionUID = 1L;

// 职员姓名
private JTextField txtName = null;

// 年龄
private JTextField txtAge = null;

//身份证号

private JTextField txtShenfenzhenhao = null;

//籍贯

private JTextField txtJiguan = null;

// 性别
private JComboBox cboSex = null;

// 职务
private JTextField txtPosition = null;

// 备注
private JTextArea txaRemark = null;

// 添加按钮
private JButton btnAdd = null;

// 关闭按钮
private JButton btnClose = null;

// 重置按钮
private JButton btnReset = null;

private MenuFactory frame;


/*public EmpAddingView( ) {
super();
try {
this.frame = frame;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
setVisible(true);
//WindowCenter.showCenter(this.frame);
} catch (Exception e) {
e.printStackTrace();
}
}
*/



public EmpAddingView(MenuFactory frame, String title) {
super(frame, title);

try {
this.frame = frame;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
//setVisible(true);
WindowCenter.showCenter(frame,this);
} catch (Exception e) {
e.printStackTrace();
}
}


private void init() throws Exception {
txtName = new JTextField();
txtAge = new JTextField();
cboSex = new JComboBox();
txtPosition = new JTextField();
txtShenfenzhenhao = new JTextField();
txtJiguan = new JTextField();


txaRemark = new JTextArea();
btnAdd = new JButton("添加");
btnClose = new JButton("关闭");
btnReset = new JButton("重置");
JLabel lblName = new JLabel("姓名:");
JLabel lblAge = new JLabel("年龄:");
JLabel lblSex = new JLabel("性别:");
JLabel lblPosition = new JLabel("职务:");
JLabel lblShenfenzhenhao = new JLabel("身份证号:");
JLabel lblJiguan = new JLabel("籍贯:");
JLabel lblRemark = new JLabel("备注:");
JScrollPane scp = new JScrollPane();
JLabel lblImg = new JLabel();

getContentPane().setLayout(null);
lblName.setBounds(20, 20, 50, 20);
add(lblName);
txtName.setBounds(70, 20, 100, 20);
add(txtName);
lblAge.setBounds(180, 20, 50, 20);
add(lblAge);
txtAge.setBounds(230, 20, 100, 20);
add(txtAge);
lblSex.setBounds(20, 50, 50, 20);
add(lblSex);
cboSex.addItem("男");
cboSex.addItem("女");
cboSex.setBounds(70, 50, 100, 20);
add(cboSex);
lblPosition.setBounds(180, 50, 50, 20);
add(lblPosition);
txtPosition.setBounds(230, 50, 100, 20);
add(txtPosition);

lblShenfenzhenhao.setBounds(15, 80, 100, 20);
add(lblShenfenzhenhao);
txtShenfenzhenhao.setBounds(70, 80, 100, 20);
add(txtShenfenzhenhao);

lblJiguan.setBounds(180, 80, 50, 20);
add(lblJiguan);
txtJiguan.setBounds(230, 80, 100, 20);
add(txtJiguan);


lblRemark.setBounds(20, 110, 50, 20);
add(lblRemark);
scp.getViewport().add(txaRemark);
scp.setBounds(20, 130, 150, 60);
add(scp);
lblImg.setIcon(ImageGetter.getIcon("Rose_SYSConfiguration" + File.separator
+ "img" + File.separator + "addemp.gif"));
lblImg.setBounds(200, 80, 100, 100);
add(lblImg);
btnAdd.setBounds(50, 200, 60, 20);
add(btnAdd);
btnReset.setBounds(145, 200, 60, 20);
add(btnReset);
btnClose.setBounds(235, 200, 60, 20);
add(btnClose);
txaRemark.setLineWrap(true);
txaRemark.setWrapStyleWord(true);
setSize(350, 260);
setModal(true);
setResizable(false);
btnReset.setActionCommand("btnReset");
btnClose.setActionCommand("btnClose");
btnAdd.setActionCommand("btnAdd");


/* 重置按钮事件处理 */
btnReset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
                    
if (e.getActionCommand().equals("btnReset")) {
txtName.setText("");
txtAge.setText("");

txtPosition.setText(""); 
txtShenfenzhenhao.setText("");  
txtJiguan.setText(""); 

txaRemark.setText("");
txtName.requestFocus();

}
}
});

/* 关闭按钮事件处理 */
btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("btnClose")) {
dispose();

}

}
});

/* 添加按钮事件处理 */

btnAdd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("btnAdd")) {
dispose();

}


}
});

}


}

#5


代码太多了,空指针就弄点断点调试下呀!
一般异常都会报告在那发生的空指针,去那看看就知道了!

#6


我用断点找着空值真了  但不知道该怎么改   

#1


调用那边出了问题,传了null过来

#2


你好   我代码字数比较多   不知道该怎么上传   请指教一下

#3


package com.jiaotong.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.jiaotong.contrl.ImageGetter;
import com.jiaotong.contrl.MenuFactory;
import com.jiaotong.contrl.WindowCenter;


public class EmpAddingView extends JDialog {

private static final long serialVersionUID = 1L;

// 职员姓名
private JTextField txtName = null;

// 年龄
private JTextField txtAge = null;

//身份证号

private JTextField txtShenfenzhenhao = null;

//籍贯

private JTextField txtJiguan = null;

// 性别
private JComboBox cboSex = null;

// 职务
private JTextField txtPosition = null;

// 备注
private JTextArea txaRemark = null;

// 添加按钮
private JButton btnAdd = null;

// 关闭按钮
private JButton btnClose = null;

// 重置按钮
private JButton btnReset = null;

private MenuFactory frame;




public EmpAddingView(MenuFactory frame, String title) {
super(frame, title);

try {
this.frame = frame;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
//setVisible(true);
WindowCenter.showCenter(frame,this);
} catch (Exception e) {
e.printStackTrace();
}
}


private void init() throws Exception {
txtName = new JTextField();
txtAge = new JTextField();
cboSex = new JComboBox();
txtPosition = new JTextField();
txtShenfenzhenhao = new JTextField();
txtJiguan = new JTextField();


txaRemark = new JTextArea();
btnAdd = new JButton("添加");
btnClose = new JButton("关闭");
btnReset = new JButton("重置");
JLabel lblName = new JLabel("姓名:");
JLabel lblAge = new JLabel("年龄:");
JLabel lblSex = new JLabel("性别:");
JLabel lblPosition = new JLabel("职务:");
JLabel lblShenfenzhenhao = new JLabel("身份证号:");
JLabel lblJiguan = new JLabel("籍贯:");
JLabel lblRemark = new JLabel("备注:");
JScrollPane scp = new JScrollPane();
JLabel lblImg = new JLabel();

getContentPane().setLayout(null);
lblName.setBounds(20, 20, 50, 20);
add(lblName);
txtName.setBounds(70, 20, 100, 20);
add(txtName);
lblAge.setBounds(180, 20, 50, 20);
add(lblAge);
txtAge.setBounds(230, 20, 100, 20);
add(txtAge);
lblSex.setBounds(20, 50, 50, 20);
add(lblSex);
cboSex.addItem("男");
cboSex.addItem("女");
cboSex.setBounds(70, 50, 100, 20);
add(cboSex);
lblPosition.setBounds(180, 50, 50, 20);
add(lblPosition);
txtPosition.setBounds(230, 50, 100, 20);
add(txtPosition);

lblShenfenzhenhao.setBounds(15, 80, 100, 20);
add(lblShenfenzhenhao);
txtShenfenzhenhao.setBounds(70, 80, 100, 20);
add(txtShenfenzhenhao);

lblJiguan.setBounds(180, 80, 50, 20);
add(lblJiguan);
txtJiguan.setBounds(230, 80, 100, 20);
add(txtJiguan);


lblRemark.setBounds(20, 110, 50, 20);
add(lblRemark);
scp.getViewport().add(txaRemark);
scp.setBounds(20, 130, 150, 60);
add(scp);
lblImg.setIcon(ImageGetter.getIcon("Rose_SYSConfiguration" + File.separator
+ "img" + File.separator + "addemp.gif"));
lblImg.setBounds(200, 80, 100, 100);
add(lblImg);
btnAdd.setBounds(50, 200, 60, 20);
add(btnAdd);
btnReset.setBounds(145, 200, 60, 20);
add(btnReset);
btnClose.setBounds(235, 200, 60, 20);
add(btnClose);
txaRemark.setLineWrap(true);
txaRemark.setWrapStyleWord(true);
setSize(350, 260);
setModal(true);
setResizable(false);
btnReset.setActionCommand("btnReset");
btnClose.setActionCommand("btnClose");
btnAdd.setActionCommand("btnAdd");


/* 重置按钮事件处理 */
btnReset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
                    
if (e.getActionCommand().equals("btnReset")) {
txtName.setText("");
txtAge.setText("");

txtPosition.setText(""); 
txtShenfenzhenhao.setText("");  
txtJiguan.setText(""); 

txaRemark.setText("");
txtName.requestFocus();

}
}
});

/* 关闭按钮事件处理 */
btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("btnClose")) {
dispose();

}




}
});

/* 添加按钮事件处理 */

btnAdd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("btnAdd")) {
dispose();

}


}
});

}


}

#4


package com.jiaotong.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.jiaotong.contrl.ImageGetter;
import com.jiaotong.contrl.MenuFactory;
import com.jiaotong.contrl.WindowCenter;

/**
 * 用户界面类--添加职员界面
 *
 */
public class EmpAddingView extends JDialog {
/**
 *
 */
private static final long serialVersionUID = 1L;

// 职员姓名
private JTextField txtName = null;

// 年龄
private JTextField txtAge = null;

//身份证号

private JTextField txtShenfenzhenhao = null;

//籍贯

private JTextField txtJiguan = null;

// 性别
private JComboBox cboSex = null;

// 职务
private JTextField txtPosition = null;

// 备注
private JTextArea txaRemark = null;

// 添加按钮
private JButton btnAdd = null;

// 关闭按钮
private JButton btnClose = null;

// 重置按钮
private JButton btnReset = null;

private MenuFactory frame;


/*public EmpAddingView( ) {
super();
try {
this.frame = frame;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
setVisible(true);
//WindowCenter.showCenter(this.frame);
} catch (Exception e) {
e.printStackTrace();
}
}
*/



public EmpAddingView(MenuFactory frame, String title) {
super(frame, title);

try {
this.frame = frame;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
//setVisible(true);
WindowCenter.showCenter(frame,this);
} catch (Exception e) {
e.printStackTrace();
}
}


private void init() throws Exception {
txtName = new JTextField();
txtAge = new JTextField();
cboSex = new JComboBox();
txtPosition = new JTextField();
txtShenfenzhenhao = new JTextField();
txtJiguan = new JTextField();


txaRemark = new JTextArea();
btnAdd = new JButton("添加");
btnClose = new JButton("关闭");
btnReset = new JButton("重置");
JLabel lblName = new JLabel("姓名:");
JLabel lblAge = new JLabel("年龄:");
JLabel lblSex = new JLabel("性别:");
JLabel lblPosition = new JLabel("职务:");
JLabel lblShenfenzhenhao = new JLabel("身份证号:");
JLabel lblJiguan = new JLabel("籍贯:");
JLabel lblRemark = new JLabel("备注:");
JScrollPane scp = new JScrollPane();
JLabel lblImg = new JLabel();

getContentPane().setLayout(null);
lblName.setBounds(20, 20, 50, 20);
add(lblName);
txtName.setBounds(70, 20, 100, 20);
add(txtName);
lblAge.setBounds(180, 20, 50, 20);
add(lblAge);
txtAge.setBounds(230, 20, 100, 20);
add(txtAge);
lblSex.setBounds(20, 50, 50, 20);
add(lblSex);
cboSex.addItem("男");
cboSex.addItem("女");
cboSex.setBounds(70, 50, 100, 20);
add(cboSex);
lblPosition.setBounds(180, 50, 50, 20);
add(lblPosition);
txtPosition.setBounds(230, 50, 100, 20);
add(txtPosition);

lblShenfenzhenhao.setBounds(15, 80, 100, 20);
add(lblShenfenzhenhao);
txtShenfenzhenhao.setBounds(70, 80, 100, 20);
add(txtShenfenzhenhao);

lblJiguan.setBounds(180, 80, 50, 20);
add(lblJiguan);
txtJiguan.setBounds(230, 80, 100, 20);
add(txtJiguan);


lblRemark.setBounds(20, 110, 50, 20);
add(lblRemark);
scp.getViewport().add(txaRemark);
scp.setBounds(20, 130, 150, 60);
add(scp);
lblImg.setIcon(ImageGetter.getIcon("Rose_SYSConfiguration" + File.separator
+ "img" + File.separator + "addemp.gif"));
lblImg.setBounds(200, 80, 100, 100);
add(lblImg);
btnAdd.setBounds(50, 200, 60, 20);
add(btnAdd);
btnReset.setBounds(145, 200, 60, 20);
add(btnReset);
btnClose.setBounds(235, 200, 60, 20);
add(btnClose);
txaRemark.setLineWrap(true);
txaRemark.setWrapStyleWord(true);
setSize(350, 260);
setModal(true);
setResizable(false);
btnReset.setActionCommand("btnReset");
btnClose.setActionCommand("btnClose");
btnAdd.setActionCommand("btnAdd");


/* 重置按钮事件处理 */
btnReset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
                    
if (e.getActionCommand().equals("btnReset")) {
txtName.setText("");
txtAge.setText("");

txtPosition.setText(""); 
txtShenfenzhenhao.setText("");  
txtJiguan.setText(""); 

txaRemark.setText("");
txtName.requestFocus();

}
}
});

/* 关闭按钮事件处理 */
btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("btnClose")) {
dispose();

}

}
});

/* 添加按钮事件处理 */

btnAdd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("btnAdd")) {
dispose();

}


}
});

}


}

#5


代码太多了,空指针就弄点断点调试下呀!
一般异常都会报告在那发生的空指针,去那看看就知道了!

#6


我用断点找着空值真了  但不知道该怎么改