In webservice class, it will retrieve database from online and put into list:
public List<List_NewsComment> allCommentList;
public void GetCommentNews( final int gCommentNewsID)
{
Thread networkThread=new Thread(){
@Override
public void run(){
try
{
SoapObject request= new SoapObject(NAMESPACE,get_Comment_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransportSE= new HttpTransportSE(URL,60000);
request.addProperty("itemid", gCommentNewsID);
envelope.setOutputSoapObject(request);
androidHttpTransportSE.call(get_Comment_SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.bodyIn;
RetrieveFromSoap( result);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
networkThread.start();
}
public List<List_NewsComment> RetrieveFromSoap(SoapObject soap)
{
allCommentList= new ArrayList<List_NewsComment>();
Vector<Object> property2 = extracted(soap);
for (int i = 0; i< property2.size();i++){
SoapObject getPropertyD=(SoapObject)property2.get(i);
List_NewsComment addcomment= new List_NewsComment();
addcomment.setcommentDate(getPropertyD.getProperty("date").toString());
addcomment.setUserName( getPropertyD.getProperty("name").toString());
addcomment.setCommentContent(getPropertyD.getProperty("comment").toString());
allCommentList.add(addcomment);
}
webservice.allCommentList.size(); <-- can call here no problem
return allCommentList;
}
private static Vector<Object> extracted(SoapObject soap) {
return (Vector<Object>)soap.getProperty(0);
}
In Main Activity class, i want to check the size of list:
Database_WebService webservice = new Database_WebService(this);
webservice.GetCommentNews(newsid);
webservice.allCommentList.size(); <-- cannot call here, what is the problem
It returns me nullpointerexception.
What is the problem????
Make your
List staticso you can access it anywhere using that class name .Like
Or
As everyone suggest make one function in your class and return the size of array in that function. Please see code below.