我正在尝试创建一个代码来从异步任务的输出中填充一个类中的数组。
现在代码给我空引用..将代码放在
下面ARRAY CLASS:
public class ClientDiscovery {
public static ArrayList<InetAddress> clientes;
public static void addclient(InetAddress address)
{
clientes.add(address);
}
public static void removeclient(InetAddress address)
{
clientes.remove(address);
}
public static ArrayList<InetAddress>getclient()
{
Set<InetAddress> noduplicate = new HashSet<>();
if (!(clientes ==null)) {
noduplicate.addAll(clientes);
clientes.clear();
clientes.addAll(noduplicate);
}
return clientes;
}
}
ASYNC任务填充阵列
public class RequestDiscoveryAsyncTask extends AsyncTask<Void,Void,Void>{
Context context;
public RequestDiscoveryAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
@Override
protected Void doInBackground(Void...voids) {
///HERE I INITIALIZE THE SOCKETS ****INTENTIONALLY OMITED TO EASY READING***
/*HERE I INTENT TO GET THE ACK BACK FROM THE CLIENTES "MY CUSTOM ACK"*/
while(!timeout) {
byte[] ready = new byte[5];
DatagramPacket pkgready = new DatagramPacket(ready, 0, 5);
socket.receive(pkgready);
if (new String(pkgready.getData()).contains("READY"))
ClientDiscovery.addclient(pkgready.getAddress());
}
}
return null;
}
}
省略套接字初始化和捕获异常以便于阅读。
logcat的:
07-18 21:50:43.889 4327-5025/fr.xplod.focal E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NullPointerException
at org.cyanogenmod.focal.ClientDiscovery.addclient(ClientDiscovery.java:16)
at org.cyanogenmod.focal.RequestDiscoveryAsyncTask.doInBackground(RequestDiscoveryAsyncTask.java:68)
at org.cyanogenmod.focal.RequestDiscoveryAsyncTask.doInBackground(RequestDiscoveryAsyncTask.java:23)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
答案 0 :(得分:0)
只是创建对象,当前ArrayList对象不存在
添加此行:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<h4>Capital Cities : </h4> <span id="capitals"></span>
<h4>Respective Country's Native Names : </h4> <span id="countries"></span>
</div>