nullpointerexception——为什么在我尝试添加字符串时,Java中的树集会给我一个nullpointerexception?
发布时间:2022-04-13 10:53:33 323
相关标签: # 移动端
我正在为一个Java类做一个项目,我似乎无法克服这个NullPointerException。该项目是一个命令行LinkedIn程序。我正在实现的一个方面是向用户配置文件添加技能集的能力。
我有一个LinkedInUser类,我在其中定义了一个树集,以用户输入的字符串形式保存这些技能集。我使用的是TreeSet,因为作业需要对它们进行排序。
我在LinkedInUser类中定义树集:
private Set skillsets = new TreeSet<>();
用户执行的操作在AddSkillsetAction类中定义:
String skillset;
System.out.println("Enter a skillset to add to your list:");
skillset = scanner.nextLine();
loggedInUser.addSkillset(skillset);
System.out.println(skillset + " has been added to your skillsets.");
它们输入的字符串被传递给LinkedInUser类中的addSkillSet函数:
public void addSkillset(String skillset) {
skillsets.add(skillset);
}
我一直收到一条NullPointerException的电话:
skillsets.add(skillset);
我做错了什么?我已经测试了这条线的每一个级别。我甚至用以下代码测试了addSkillset函数中的树集:
if(skillsets == null) {
System.out.println("The TreeSet is null.")
}
它告诉我树集是空的。我想用以下方法来实例化场景:
private Set skillsets = new TreeSet<>();
实际上会创建一个空树集,而不是指向空位置。为什么是我的套装;“技能集”;还是指向空?我做错了什么?
编辑:以下是完整的课程:
package edu.institution.asn2;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class LinkedInUser extends UserAccount implements Comparable, Serializable {
private static final long serialVersionUID = 75648957489235739L;
private String type;
private List connections = new ArrayList<>();
private Set skillsets = new TreeSet<>();
public LinkedInUser(String username, String password) {
super(username, password);
}
@Override
public void setType(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
// Add a connection to user's list
public void addConnection(LinkedInUser user) throws LinkedInException {
int index = connections.indexOf(user);
if (index >= 0) {
throw new LinkedInException("You are already connected with this user.");
}
else {
connections.add(user);
}
}
// Remove a connection from the user's connection list
public void removeConnection(LinkedInUser user) throws LinkedInException {
int index = connections.indexOf(user);
if (index < 0) {
throw new LinkedInException("You are NOT connected to this user.");
}
else {
connections.remove(index);
}
}
// Return a copy of the ArrayList of connections
public List getConnections() {
ArrayList copy = new ArrayList<>(connections);
return copy;
}
// Return the number of connections
public int getNumberOfConnections() {
return connections.size();
}
// Return the skillsets
public Set getSkillsets(){
return skillsets;
}
// Add a skillset
public void addSkillset(String skillset) {
skillsets.add(skillset);
}
// Remove a skillset
public void removeSkillset (String skillset) {
if(skillsets.contains(skillset)){
skillsets.remove(skillset);
} else {
System.out.println(skillset + " is not in your skills list.");
}
}
// Override the compareTo function
@Override
public int compareTo(LinkedInUser user) {
int i = this.getUsername().compareToIgnoreCase(user.getUsername());
return i;
}
}
以及要添加技能集的类:
package edu.institution.actions.asn7;
import java.util.Scanner;
import edu.institution.ApplicationHelper;
import edu.institution.UserRepository;
import edu.institution.actions.MenuAction;
import edu.institution.asn2.LinkedInUser;
public class AddSkillsetAction implements MenuAction {
@Override
public boolean process(Scanner scanner, UserRepository userRepository, LinkedInUser loggedInUser) {
String skillset;
System.out.println("Enter a skillset to add to your list:");
skillset = scanner.nextLine();
loggedInUser.addSkillset(skillset);
System.out.println(skillset + " has been added to your skillsets.");
ApplicationHelper.incrementSkillsetCount(skillset);
return true;
}
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报