• Post Reply Bookmark Topic Watch Topic
  • New Topic

reason of NullPointerException  RSS feed

 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code which generated the NullPointerException, but I don;t find the reason for null point exception.

The code is below, since getSharedFactory is checking null and created a new factory from new MonitoringConnectionFactory(), I am not sure which case it will generate the null point. And the exception is only found in the testing tools, I can't reproduce it in local environment and do the debug with exception.


Any suggestions for the possible reason of nullpointerexception in the line of "(MonitoringConnection)cFactory.createConnection"

Thanks,





 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> public static ConnectionFactory getSharedFactory()

should be synchronized...

public static synchronized ConnectionFactory getSharedFactory()
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, looks like it is a right direction, but I still have a question, the caller function getConnection of getShareFactory (listed below, which is missed in the first post) is synchronized. Should it be able to prevent the synchronization issue?


 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming getConnection( ) is in class XYZ...

It is possible that getConnection( ) can be called simultaneoulsy on two different objects of XYZ.

In that case the synchronization on getConnection( ) will not have any effect.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the stack trace? Because even without synchronization, I cannot see how MonitoringConnectionFactory.getSharedFactory() ever returns null. The synchronization will only ensure that you never create a second instance, but unless you set s_sharedFactory to null somewhere else it can't be null by the time you return its value. Perhaps the NPE is coming from a different source (like inside the createConnection method).
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry.. overlooked.

I agree with Rob.
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stack trace like below, and it point to the line 404 of " (MonitoringConnection)cFactory.createConnection". I am not the expert for the thread issue, but I haven't found other reason which could cause the nullPointerException in this case.

2010-01-25 23:54:55,983 ERROR db.UsageUtil logUsage.141 - java.lang.NullPointerException
java.lang.NullPointerException
at sysman.emo.perf.metric.rt.DbMetricCollectorTarget.getConnection(DbMetricCollectorTarget.java:404)
at sysman.db.uix.DBUtil.getDBConnection(DBUtil.java:872)
at sysman.db.adm.util.UsageUtils.logUsage(UsageUtils.java:118)
..........................
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there other reasons, except synchronization, which could cause the NullPointerErexception ?

Thanks,
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You exception indicates that cFactory is null, which can only be the case if MonitoringConnectionFactory.getSharedFactory() returns null. This can only happen if s_sharedFactory is somewhere set to null again.
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The full code for inner class MonitoringFactory is below.




Is there something wrong for the above code line 5 and line 6 , as new MonitoringConnectionFactory() will call " s_sharedFactory = null" ? I just a little confused what will be result when new MonitoringConnectionFactory() should create a no null pointer , but looks like it also set s_sharedFactory is null in class too.



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!