线程“Thread-0”中的异常 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23051028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "Thread-0" java.lang.NullPointerException
提问by Bec Martin
I have a method in a client server program which is meant to create an instance of a citylist class, which is just an array list of cities...
我在客户端服务器程序中有一个方法,它旨在创建一个 citylist 类的实例,它只是一个城市数组列表......
(This method is in server)
(此方法在服务器中)
public void listCities() {
CityList LoadCities = new CityList();
String CityDetails = LoadCities.cities.toString();
try {
dos.writeUTF("These are the cities serviced by the ssystem");
dos.writeUTF(ServerConstants.CR_LF);
dos.writeUTF(CityDetails);
} catch (IOException ex) {
Logger.getLogger(SocketHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
The CityList Class is this:
CityList 类是这样的:
public class CityList {
public ArrayList<City> cities;
City melbourne = new City("Melbourne");
City sydney = new City("Sydney");
City darwin = new City("Darwin");
City launceston = new City("Launceston");
City hobart = new City("Hobart");
//default constructor
public CityList() {
cities.add(melbourne);
cities.add(sydney);
cities.add(darwin);
cities.add(launceston);
cities.add(hobart);
}
//other constructor
public CityList(ArrayList<City> cities) {
this.cities = cities;
}
public int size()
{
return cities.size();
}
}
The client exists in a thread... The server calls a socket handler in its own thread class SocketHandler extends Thread class SocketHandler extends Thread
客户端存在于一个线程中... 服务端在自己的线程类中调用套接字处理程序 SocketHandler extends Thread 类 SocketHandler extends Thread
I continually get this error when I try to call that particular method (list cities)
当我尝试调用该特定方法(列出城市)时,我不断收到此错误
Exception in thread "Thread-0" java.lang.NullPointerException
at hotelbroker.CityList.<init>(CityList.java:30)
at hotelbroker.SocketHandler.listCities(MultiEchoServer.java:153)
at hotelbroker.SocketHandler.run(MultiEchoServer.java:95)
I know this is cuz I'm a n00b and I need to call some kind of this instance thingummy I just have no idea...
我知道这是因为我是 n00b,我需要调用某种这种实例,我只是不知道......
回答by Sotirios Delimanolis
When you don't initialize instance fields, they get initialized to null
by default. In this case, you are trying to use this instance variable
当您不初始化实例字段时,它们会null
被默认初始化。在这种情况下,您正在尝试使用此实例变量
public ArrayList<City> cities;
without having first intialized it. It's therefore null
.
没有首先初始化它。因此是null
.
When you try to invoke a method on a null
reference, like here
当您尝试在引用上调用方法时null
,例如此处
cities.add(melbourne);
you get a NullPointerException
.
你得到一个NullPointerException
.
You have to initialize it, either where it is declared
你必须初始化它,无论是在它被声明的地方
public ArrayList<City> cities = new ArrayList<>();
or before you use it
或在您使用它之前
cities = new ArrayList<>();
cities.add(melbourne);
回答by Jason
public CityList() {
cities = new ArrayList<City>();
cities.add(melbourne);
cities.add(sydney);
cities.add(darwin);
cities.add(launceston);
cities.add(hobart);
}