开发者

Is the order of the value inside the CLASSPATH matter?

开发者 https://www.devze.com 2023-01-17 09:30 出处:网络
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

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开发者_Go百科 happening Only the order matters?


You've a class with the same name in the both folders. In C:\test there's a version of the ButtonTest3 class which contains a programming bug causing this NullPointerException. In C:\test\new there's a different version of the ButtonTest3 class which doesn't contain this bug, or probably there's a ButtonTest class which does entirely different things than the one in C:\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 new is 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 NullPointerException is relatively trivial to naildown and fix. First look at the first line of the stacktrace:

at ButtonFrame.makeButton(ButtonTest3.java:42)

It's telling that it has occurred in line 42 of ButtonTest3 class, inside the makeButton() method. Now go to line 42 of ButtonTest3.java, it'll look something like:

someObject.doSomething();

Look there where a dot operator . is been used to invoke a method or access a field of some object. The NullPointerException means that someObject is null at the particular moment. There is no instance!

It's an easy fix: just ensure that it is not null at the moment you're invoking/accessing it:

someObject = new SomeObject();
// ...
someObject.doSomething();


Well, I don't believe you can have two classes defined in a single source file. You can have them defined as a subclass.

According to the Java spec:

Each class file contains the definition of a single class or interface. Although a class or interface need not have an external representation literally contained in a file (for instance, because the class is generated by a class loader), we will colloquially refer to any valid representation of a class or interface as being in the class file format.format.

You could place ButtonFrame inside ButtonTest2.

public class ButtonTest2
{
    public static void main(String[] args)
    {
       ...
       ButtonFrame frame = new ButtonFrame();
    }

    class ButtonFrame extends JFrame {
       ....
   }
}

Or, put them in different java files.


You have two classes at top level in program, thats wrong. But keeping that aside, your program is not getting compiled at first place. To successfully compile the program use the following NppExec script:

cmd /c cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\javac" "$(FULL_CURRENT_PATH)"
cmd /k cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\java" "$(NAME_PART)" && exit

Make sure that you have your JDK folder set to JAVA_HOME environment variable. and try again.

0

精彩评论

暂无评论...
验证码 换一张
取 消