我有一個對象,這是學生的學生;其中有一些字段如姓名,家庭......我想爲這個學生創建一個用戶名,並通過點擊一個按鈕,我想在控制檯上顯示學生的用戶名,但它會在控制檯上顯示這些句子(我複製並粘貼僅有的那部分)----->它爲什麼顯示NullPointerException?
顯示java.lang.NullPointerException 在registration.Students.nextUserName(Students.java:149) 在ui.AddStudent.submit( AddStudent.java:254) at ui.AddStudent.jButton1ActionPerformed(AddStudent.java:186) at ui.AddStudent.access $ 000(AddStudent.java:24) at ui.AddStudent $ 1.actionPerformed(AddStudent.java:76) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19 95) 在javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2318) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java: (javax.swing.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6038) at javax.swing.JComponent.processMouseEvent(JComponent。 java:3265) 。 。 。
我的學生類的一部分,這也延伸人:
public String nextUserName() {
StringBuffer user = new StringBuffer();
SystemManagement students = new SystemManagement();
String name1 = students.getName();
char char1 = name1.charAt(0);
String family1 = students.getFamily();
char char2 = family1.charAt(1);
setUserName(name1 + "." + family1);
if (user.length() != 0 && user.toString().equals(getUserName())) {
char[] allowedCharacters1 = {char1, char2, '1', '.'};
SecureRandom random = new SecureRandom();
for (int i = 0; i < user.length(); i++) {
user.append(allowedCharacters1[random.nextInt(allowedCharacters1.length)]);
}
setUserName(user.toString());
}
return getUserName();
}
public String currentUserName() {
return getUserName();
}
public static Students getInstance() {
if (instance == null) {
instance = new Students();
}
return instance;
}
我的人等級:
private String userName;
public Human(String userName) {
this.userName = Students.getInstance().nextUserName();
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
謝謝,我已經改變了我netUserName()方法,這一點,但它返回NULL,而不是例外。
public String nextUserName() {
StringBuffer user = new StringBuffer();
SystemManagement students = new SystemManagement();
String name1 = students.getName();
String family1 = students.getFamily();
if(name1 !=null && family1 !=null) {
char char1 = name1.charAt(0);
char char2 = family1.charAt(1);
setUserName(name1 + "." + family1);
if (user.length() != 0 && user.toString().equals(getUserName())) {
char[] allowedCharacters1 = {char1, char2, '1', '.'};
SecureRandom random = new SecureRandom();
for (int i = 0; i < user.length(); i++) {
user.append(allowedCharacters1[random.nextInt(allowedCharacters1.length)]);
}
setUserName(user.toString());
}
}
return getUserName();
}
哪一行會引發空指針? – 2009-11-25 05:50:37
@Johanna - 您一直在使用Java ...和SO ...足夠長的時間,以至於您不再需要提醒您包含堆棧跟蹤和相關行號。讓你的行爲一起好友! – 2009-11-25 06:11:14