Android program, encountered the most error, one of the null pointer problem, empty pointer said: Good solution! is difficult to solve.
Well solved is said: Obvious null pointer problem, no instantiation class, control or ID wrong. A null pointer problem will occur.
A hard to solve null pointer is: this is usually the case, in a request, and then write a network request. is equivalent to opening a thread inside a thread. Because we are not sure if the first thread has finished executing, or if half of it is executed, it is not possible to ensure that the thread finishes executing. Of course, this is not necessarily the case that the network request will appear. It is also possible to perform a very time-consuming task. Therefore, it is not recommended to open another time-consuming thread in one thread. If you have a special case in which you want to turn on a thread, make sure that all two threads are ready to execute.
Encountered the problem of NULL pointers, how to solve is the most critical: usually we found that the problem of NULL pointer, the program crashes stop running, print through log log, we can from the console, directly to a row, that is the line reported the problem of NULL pointer. Because it's a line of code: the example is as follows:
dosetlocation (res_city_id,latitude,lontitude);
Like the above line of code, a total of three parameters, since this line reported null pointer, some parameter value is empty, did not get the value, or did not assign the value of success. The solution is: first you can determine for yourself which parameters are definitely not empty, and which are likely to be empty. As for how to look, according to the actual situation, can also rely on experience, which may be empty.
When we are not sure which parameter is empty, we need manual debugging, and breakpoint debugging is the most commonly used debugging means, and is the most effective means. It can help you navigate through the values of each parameter, so that you can find out the problem in a step-by-step way.
There is also a situation where the breakpoint is not found in the error. Because breakpoint debugging, jump out of the method, do not execute. This is also common, but must also solve the problem.
Before, the following code is as follows:
Private list<namevaluepair> params;
Public Jsonparser (Context Context,boolean isloginorregister) {
this.isloginorregister=isloginorregister;
This.context=context;
Init ();
}
public string makeHttpRequest2 (string url, string method, String jsonstr) {
try {
else if (method = = "Get") {
//Request method was get
String paramstring = Urlencodedutils.format (params, "utf-8");
URL + "?" + paramstring;
HttpGet httpget = new HttpGet (URL);
} catch (Exception e) {
e.printstacktrace ();
}
return JSON;
}
The use is as follows when I go to the "get" method. Throw the null pointer exception directly, there is no way to go inside. When I locate the error message, the params is displayed. At that time saw this is also a monk. For fear of what comes, network requests are often used, but the encapsulation of network requests is not very understanding. Because params is empty, go to find params where to initialize, this time, will discover. The top two construction methods,
There is a method of constructing a parameter with params, but a construction method with no params. And I'm using a construction method with no params parameters. At that time I found that there is no such parameter, the parameters can not be added to it, and later found that it is wrong to add.
First, what is the construction method? The construction method is that when you call it, he helps you instantiate the parameter, or instantiate some classes that need to be new.
If you find this, you'll find that the second construction method, although it uses params, is not instantiated. It is convenient to find a problem to solve the problem. You only need to instantiate within the construction method. No instantiation of parameter mode is used.
Public Jsonparser (Context Context,boolean isloginorregister) {
this.isloginorregister=isloginorregister;
This.context=context;
Params=new arraylist<namevaluepair> ();
Init ();
}