I have a method that I use to recieve some information. I send to this method one string (the server direction), and two strings that contains the user and the password, and I recieve an String[] with all the information, but I have a NullPointerException at
values = request("http://myIp/XXX/post.php?request=1", user, password);
line. The complete code is the next:
String[] values;
@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
setContentView(R.layout.activity_select_environment);
myBundle = getIntent().getExtras();
user = myBundle.getString("user");
password = myBundle.getString("password");
values = request("http://myIp/XXX/post.php?request=1", user, password);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1, values);
}
The method is the next:
@SuppressWarnings("null")
public String[] request(String requesturl, String user, String password)
{
String result[] = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(requesturl);
String results = "";
//Varibles POST
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", user));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
response.getAllHeaders();
response.getEntity();
result[0] = "";
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
results = EntityUtils.toString(response.getEntity());
if(!results.equals("-1"))
{
JSONObject jsonResponse = new JSONObject(results);
JSONArray jsonMainNode = jsonResponse.optJSONArray("edificios");
int lengthJsonArr =jsonMainNode.length();
for(int i = 0; i < lengthJsonArr; i++)
{
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
result[i] = jsonChildNode.optString("nombre");
}
}
}
return result;
}
catch(Exception ex)
{
result[0] = "error";
return result;
}
}
I recieved a NullPointerException on the "values=request("....");" line and I don't know why, because user and password have values and the other value is a string... What could be do?
You have declared your result
String array but did not initialize it.
Try this:
@SuppressWarnings("null")
public String[] request(String requesturl, String user, String password)
{
String result[] = new String[ <Insert value> ];
// Code...
}
Let me explain further to avoid downvotes. This is why I think this:
You first initialize the array to null, so it can't be used yet. Further down the line you have this statement String results = ""
but your array is still null
. Then you do this result[0] = ""
, oh dear. That is where the nullPointerException
comes from, I think.
You have to initialize your array and you are right if you say that you need to know the size of your array up front. Use the maximum value you expect for the size or, better yet, use a list
and not an array
.
Here is a nice source for the list
datastructure: List in Android Development .
There is also a nice discussion about list
versus arrays
here , it is for C# but the principles remain more or less the same.
You got the exception because you are trying to access the first element of the
result , which is not even initialized.
So you need to initialize the array first and then use it
result = new String[<some size>];
If size of the array is not know in advance, then ArrayList
can be a option for you.
ArrayList<String> result = new ArrayList<>();
usage
result.add(<some string value>);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.