No of visitors who read this post: 199
Type: Question
Author: Milodiaz
Your rating: None Average: 5 (1 vote)

I make a simulation using IDE NETBEANS but there is a warning: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
on source code :

if(!mapRouter.containsKey(routerA))
addRouter(routerA);

Anybody know how to finish it?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

#

Null Pointer Exception is normally thrown when a NULL value is somehow assigned. 

For example, there may be no RouterA to add. 

This is a simple thing, whereever you get such problem, you need to use null OBJECTS INSTEAD OF ONLY null. 

The another wayout is to check using a if-else construct for NULL values. 

This helps in overcoming the error. 

You may submit the rest of the code for us to debug. 

Only two lines of code is not enough to identify the problem. 

So, please try out the above and if not overcomed, post the entire code. 

Thanks.

# (Solution Accepted)

Hello Milodiaz,
 
 
I suggest you post the full error message (Null pointer exception) here. That is much concerned when resolving the problem. 
'Null pointer exception' is by far a most common error which can be seen in Java. They are exceptions thrown out when you attempt to use a reference that directs to no location existing in the memory. 
Hit this link to learn more about it [http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/NullPointerException.html]. 
 
And also the debugging of null pointer exceptions is very simple most of the times.
 
Identify the line where it occurs, check if there are any null references there. You may add 'if', 'else' statements to do that. Once you identify the specific reference with the exception, it will be simple to fix the code. 
 
In this case, there seems to be an issue with the "routerA". Check where routerA is declared and initiated, check its value changes. Like i said, its difficult to state the location of the bug because you haven't posted the full error message. But i do hope you will get some idea about which i have been talking about. 
 
Good luck with debugging! 
 
Tip: Do not use try catch scopes to catch the exception, its not entirely practical.