I have 2 java program located seperately
One in c:\test and the other in c:\test\new
I can compile both of it without any error \javac
But when i try to execute the file \java
it shows the error like this
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ButtonFrame.makeButton(ButtonTest3.java:42)
at ButtonFrame.<init>(ButtonTest3.java:29)
at ButtonTest$1.run(ButtonTest.java:17)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
i put this in my classpath
CLASSPATH value- C:\test;C:\test\new
but if i change the order of the value in CLASSPATH to this
CLASSPATH value- C:\test\new;C:\test
the error is simply gone
Why?? this could happening
Only the order matters?
You’ve a class with the same name in the both folders. In
C:\testthere’s a version of theButtonTest3class which contains a programming bug causing thisNullPointerException. InC:\test\newthere’s a different version of theButtonTest3class which doesn’t contain this bug, or probably there’s aButtonTestclass which does entirely different things than the one inC:\test.Cleanup your classpath. It’s not good to have duplicate different versioned classes with the same signature in the classpath. If your intent is that
newis supposed to be a package identifier, then you need to leave it away from the classpath. However, such a package name would have resulted in a compilation error, so that can’t be it.As to the bug, a
NullPointerExceptionis relatively trivial to naildown and fix. First look at the first line of the stacktrace:It’s telling that it has occurred in line 42 of
ButtonTest3class, inside themakeButton()method. Now go to line 42 ofButtonTest3.java, it’ll look something like:Look there where a dot operator
.is been used to invoke a method or access a field of some object. TheNullPointerExceptionmeans thatsomeObjectisnullat the particular moment. There is no instance!It’s an easy fix: just ensure that it is not
nullat the moment you’re invoking/accessing it: