在我的聊天软件中,我想更新 ListView 但是当我调用一个方法时,我得到如下所示的空指针异常
public class RoomController implements Initializable {
/**
* Initializes the controller class.
*/
public static ObservableList<String> members = FXCollections.observableArrayList();
@FXML public static Button send;
@FXML static ListView names;
@FXML static HTMLEditor outmsg;
@FXML static HTMLEditor showBox;
@FXML static ContextMenu listMenu;
static void updateRemove(String newuser) {
try
{
showBox.setHtmlText(showBox.getHtmlText()+newuser+"<br> has left room");
System.out.println(newuser+"has left room");
members.remove(newuser);
System.out.println("helloji update remove");
}
catch(Exception e)
{
e.printStackTrace();
}
}
@FXML public void listMenuAction(ActionEvent event)
{
MenuItem item = (MenuItem)event.getSource();
LoginController.getInstance().c.sendPrivateRequest((String)names.getSelectionModel().getSelectedItem());
}
@FXML public void sendAction(ActionEvent event)
{
LoginController.instance.c.sendMessage(outmsg.getHtmlText());
}
public static void updateList(String name)
{
try
{
showBox.setHtmlText(showBox.getHtmlText()+"<br> new user entered in room");
System.out.println("new user enterd"+Client.message.getNewuser());
System.out.println("beginning update list");
members.add(name);
System.out.println(members);
System.out.println("add user request fullfilled");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void initList(Vector<String> name)
{
try
{
System.out.println("list initializing");
members.setAll(name);
System.out.println(members);
System.out.println("list initialized");
System.out.println("public room created");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void showMessage(String msg)
{
try
{
showBox.setHtmlText(showBox.getHtmlText()+""+msg);
System.out.println("showing msg "+msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
try
{
names.setItems(members);
showBox.setHtmlText(showBox.getHtmlText()+"<br> welcome "+Client.username);
System.out.println("welcome "+Client.username);
System.out.println(names.getItems());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
在上面的代码中 initList() ,updateRemove() , listUpdate() 方法在我传递值时显示参数值 null !!
请帮我!!!
下面是我调用上述方法的代码
public void run()
{
while(true)
{
try
{
if(in==null)
{
in = new ObjectInputStream(socket.getInputStream());
System.out.println("inputstream created:::::::::::::::::thread");
}
message = new Message();
message = (Message)in.readObject();
if(message.getMode().equals("addUser"))
{
System.out.println("add user request"+message.getNewuser());
Platform.runLater(new Runnable() {
@Override
public void run() {
RoomController.updateList(message.getNewuser());
}
});
}
else if(message.getMode().equals("getmsg"))
{
// message.setMessage(message.getMessage());
Platform.runLater(new Runnable() {
@Override
public void run() {
RoomController.showMessage(message.getMessage());
}
});
}
else if(message.getMode().equals("addallUser"))
{
System.out.println("join room response");
for(String names1 : message.getOlUsers())
{
System.out.println("add all users "+names1);
}
Platform.runLater(new Runnable() {
@Override
public void run() {
RoomController.initList(message.getOlUsers());
}
});
}
else if(message.getMode().equals("leaveUser"))
{
Platform.runLater(new Runnable() {
@Override
public void run() {
RoomController.updateRemove(message.getNewuser());
}
});
}
else if(message.getMode().equals("privateChatRequest"))
{
System.out.println("private request rediraction get from"+message.getNewuser());
Platform.runLater(new Runnable() {
@Override
public void run() {
CreateRoom.createPrivateroom("privateWindow.fxml", message.getNewuser());
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
以下是我得到的错误
list initializing
[]
list initialized
public room created
add user requestsanddeep
new user enterdnull
开始更新列表
[null]
add user request fullfilled