Home > Back-end >  Multi-threaded null pointer exception, bosses know how broken
Multi-threaded null pointer exception, bosses know how broken

Time:12-07

The Exception in the thread "thread - 0" Java. Lang. NullPointerException
At com. Cdly. Test. FileListener. OnFileCreate (55) FileListener. Java:
At org.apache.com mons. IO. Monitor. FileAlterationObserver. DoCreate (FileAlterationObserver. Java: 379)
At org.apache.com mons. IO. Monitor. FileAlterationObserver. CheckAndNotify (FileAlterationObserver. Java: 330)
At org.apache.com mons. IO. Monitor. FileAlterationObserver. CheckAndNotify (FileAlterationObserver. Java: 304)
At org.apache.com mons. IO. Monitor. FileAlterationMonitor. Run (182) FileAlterationMonitor. Java:
at java.lang.Thread.run(Unknown Source)

CodePudding user response:

Talk is being, show me the code

CodePudding user response:

No code?

CodePudding user response:

Public class Test {

Public static void main (String [] args) throws the Exception {
//monitor directory
String rootDir="D: \ \ baidu";
//polling interval 5 seconds
The Integer time=3;
Long interval=TimeUnit. SECONDS. ToMillis (time);
//to create a file is used to handle file format,
//FileFilterUtils. SuffixFileFilter (". TXT ")
FileAlterationObserver _observer=new FileAlterationObserver (
RootDir,
FileFilterUtils. And (
FileFilterUtils. FileFileFilter ()),//filter file format
Null);
FileAlterationObserver observer=new FileAlterationObserver (rootDir);

The observer. AddListener (new FileListener ());//Settings file change listener
//create a file change listener
FileAlterationMonitor monitor=new FileAlterationMonitor (interval, the observer).
//start monitoring
Try {
The monitor. The start ();
} the catch (Exception e) {
e.printStackTrace();
}
}

}

CodePudding user response:

Public class FileListener extends FileAlterationListenerAdaptor {
Private DbosService dboService;
Public void onFileCreate (File File) {
The String fileName=file. The getName ();
String [] str1=fileName. The split (" _ ");
StringBuffer sb=new StringBuffer ();
String serialNum="";
String dnOrUp="";
String communTime="";
String imsi="";
String imei="";
String tmsi="";
String fileLen=String. The valueOf (file. The length ());
String filePath=file. GetPath ();
for(int i=0; iSb. Append (str1 [I] + "");
}
Sb. DeleteCharAt (sb. Length () - 1);
String str3=sb. ToString ();
String str2 []=str3. Split (" ");
for(int j=0; jSerialNum=str2 [0];
DnOrUp=str2 [1];
CommunTime=str2 [2];
Imsi=str2 [3];
Imei=str2 [4];
Tmsi=str2 [5];
}
Dbos Dbos=new Dbos ();
Dbos. SetSerialNum (serialNum);
Dbos. SetDnOrUp (dnOrUp);
Dbos. SetCommunTime (communTime);
Dbos. SetImsi (imsi);
Dbos. SetImei (imei);
Dbos. SetTmsi (tmsi);
Dbos. SetFileLen (fileLen);
Dbos. SetFilePath (filePath);
System. The out. Println (dbos getSerialNum ());

DboService. AddDbos (dbos);

}
These are the main two class code

CodePudding user response:

See this FileListener near line 55, post code, so you couldn't see what is 55

CodePudding user response:

Positioning in: ats com cdly. Test. FileListener. OnFileCreate (55) FileListener. Java:
See you at FileListener. Java line55
But I thought you didn't 55 it stick out like line,,,

CodePudding user response:

55 line is dboService. AddDbos (dbos);

CodePudding user response:

refer to 7th floor fingertips technology reply:
lines 55 is dboService. AddDbos (dbos);

Call the method using object attribute when the object is null value should be no initialization? Check yourself

CodePudding user response:

Thanks for your bosses, I after carefully checked

CodePudding user response:

Question is very simple, you in FileListener statement dboService object, not initialize, so it is null,
Write a constructor, the dboService assignment (or initial),

CodePudding user response:

The observer. AddListener (new FileListener ());//Settings file change listener
This line of code, using the empty constructor parameters,

You're FileListener class, there is no empty constructor parameters, the need to define an empty constructor parameters, inside the dboService created,

Note DbosService spring whether this class is used for assembly, and if so, can't new directly, will lack the spring context, throws other null pointer exception,
Ah ~ ~ ~

CodePudding user response:

Ok thank you bosses

CodePudding user response:

Private DbosService dboService;
You this no injection. No initialization, dboService=null

CodePudding user response:

That I specific how should operate

CodePudding user response:

DboService not initialized

CodePudding user response:

Private DbosService dboService; Uninitialized

The simplest method is to add a @autowired labels or DbosService dboService=new DbosService ()


CodePudding user response:

nullnullnullnullnullnullnullnullnull
  • Related