bugs that are often encountered in Java primary development
The error message is as follows:
Attempt to invoke interface the ' Boolean Java.util.List.add (Java.lang.Object) ' on a null Object reference;
Attempt to refer to the Boolean Java.util.List.add () method on an empty object reference;
Error Example:
List<employee> employeelist = null;
EmployeeList = new arraylist<employee> ();
Employeelist.add (New Employee ("w8623", "Yang Maosheng", one));
Employeelist.add (New Employee ("w8624", "Zhang San");
Employeelist.add (New Employee ("w8625", "Li Si", 2));
Because the EmployeeList Add method is invoked, the employlist must be initialized, otherwise the above error is reported, and a null object is not present in the Add method, so a valid object reference is used; The modification principle is: Initialize the object:
list<employee> employeelist = new arraylist<employee> ();
Employeelist.add (New Employee ("w8623", "Yang Maosheng", one));
Employeelist.add (New Employee ("w8624", "Zhang San");
Employeelist.add (New Employee ("w8625", "Li Si", 2));
2. The error message is as follows:
Cannot refer to a non-final variable position inside a inner class defined in a different method
Initialize an inner class in another method, the inner class cannot use a non final variable in the method;
Error Example:
The 16th line prompts for this error
public class Hellotp {public static void main (string[] args) {//TODO
auto-generated method Stub test ();
if (args.length!= 0) {System.out.println (args[0]);
The public void Test () {String str = ' Hello world ';
Class Inner {public void out () {System.out.println (str); }
}
}
The modification is to change the 12th line of string str = "Hello World" to final string str = "Hello world";
Principle Analysis:
In fact, you can follow the Java compiler principle to analyze: HELLOTP and inner were compiled into two class files, it can not access the other classes in the method of local variables;
In the final analysis, it can be understood that the life cycle of local variables is limited, because the local variables exist in the stack, it is the survival of the method; When an instantiated inner class accesses a local variable (it no longer exists, is ejected by the stack, or is not in the stack at all), there is an error, and Java, in order to prevent this type of error, Local variables that require internal class access should be final type;
The local inner class object contains a copy of the final-type local variable to be accessed and becomes its data member. Thus, it is in this sense that the lifetime of the final-type local variable exceeds one call to its method. Strictly speaking, the method call ends, and all local variables (including final) are all dead. However: the local internal class object has the final type local variable's copy;
The specific principles of understanding the discussion can be seen in the following two posts:
http://blog.csdn.net/mydreamongo/article/details/8983132
http://bbs.csdn.net/topics/220029494/
3. The error message is as follows:
Java.lang.NullPointerException:lock ==null
Null pointer error, detailed error as follows:
12-17 13:06:53.833:e/androidruntime (1778): FATAL exception:thread-195 12-17 13:06:53.833:e/androidruntime (1778): Process:com.tplink.javatraining2, pid:1778 12-17 13:06:53.833:e/androidruntime (1778): Java.lang.NullPointerException:lock = = null 12-17 13:06:53.833:e/androidruntime (1778): at Java.io.writer.<init> ;(writer.java:62) 12-17 13:06:53.833:e/androidruntime (1778): At Java.io.bufferedwriter.<init> ( bufferedwriter.java:66) 12-17 13:06:53.833:e/androidruntime (1778): At Java.io.bufferedwriter.<init> ( bufferedwriter.java:54) 12-17 13:06:53.833:e/androidruntime (1778): at Com.tplink.javatraining4.model.JsonFileHelper.saveJsonToSdCard (jsonfilehelper.java:62) 12-17 13:06:53.833:e/ Androidruntime (1778): At Com.tplink.javatraining4.model.JsonFileHelper.readJsonFromSdCard (Jsonfilehelper.java : 117) 12-17 13:06:53.833:e/androidruntime (1778): At Com.tplink.javatraining4.mainlistactivity$readjsonthread.run (
mainlistactivity.java:126)
Here to share with you a little experience of error:
1, do not be afraid of mistakes, error helps to better understand the program, you can deepen their skill
2, learn to grasp the point of the scene and to speculate where the error occurred
A Java error is a bottom-up mechanism: starting from where the runtime is wrong, and then tracing back to its place (method), traced back to the top of the application.
This error is the writer->bufferedwriter->jsonfilehelper->mainlistactivity; focus of the line of code to view the errors and its front and rear lines, and the following error code is posted:
jsonfilehelper.java:62
if (!file.getparentfile (). exists ()) {
file.getparentfile (). Mkdirs ();
}
BufferedWriter bw = null;
BufferedWriter bw = new BufferedWriter (null);//62nd line
try {
* * JSON file Write stream
/bw = new BufferedWriter (New FILEWR ITER (file));
String jsonsting = Employeehelper
. Getjsonfromemplyee (employees);
/* Writes the contents of the parsing JSON file to the file
/bw.write (jsonsting);
Bw.flush ();
jsonfilehelper.java:117
if (!file.exists ()) {
arraylist<employee> mtempemployeess = Employeehelper
. getemployeelist ();
Savejsontosdcard (mtempemployeess);//Line 117th
employees = mtempemployeess;
}
mainlistactivity.java:126
public class Readjsonthread extends Thread {@Override public vo ID run () {//TODO auto-generated method stub arraylist<employee> Re
Ademployees = new arraylist<employee> ();
Reademployees = Jsonfilehelper.readjsonfromsdcard ();//Line 126th if (!reademployees.isempty ()) {
Mtempeployees.clear ();
Mtempeployees.addall (reademployees);
msg = new Message ();
Msg.what = message_raed;
Readhandler.sendmessage (msg); }
}
}
You can start the readjsonthread thread in mainlistactivity, and then invoke the Readjsonfromsdcard () method of the Jsonfilehelper class (what exactly is the function in this method that is not described in detail), according to the code. The Savejsontosdcard (mtempemployeess) method is invoked in the Readjsonfromsdcard method; Savejsontosdcard (mtempemployeess) method to go to bufferedwriter bw = new BufferedWriter (null); This is an error, the error message can be inferred to be caused by the line of code, and then read the line of code and the previous lines.
The incoming writer in file read and write causes an error, should be corrected to bufferedwriter BW = NULL, and then initialized for subsequent use. This is also the error principle analysis.
Continue to add ...