CLASSPATH 中的值的顺序重要吗?
我有 2 个单独的 java 程序 一个在 c:\test 中,另一个在 c:\test\new 中,
我可以编译它们而不会出现任何错误 \javac
但是当我尝试执行文件 \java 时 它显示这样的错误
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)
我把它放在我的类路径中
CLASSPATH 值 - C:\test;C:\test\new
但如果我将 CLASSPATH 中的值的顺序更改为此
CLASSPATH 值 - C:\test\new;C:\test
错误就消失了
为什么?这可能会发生 只有顺序重要吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个文件夹中都有一个同名的类。在
C:\test
中,有一个ButtonTest3
类版本,其中包含导致此NullPointerException
的编程错误。在C:\test\new
中,有一个不同版本的ButtonTest3
类,它不包含此错误,或者可能有一个ButtonTest
类它所做的事情与 C:\test 中的完全不同。清理你的类路径。在类路径中存在具有相同签名的重复的不同版本类是不好的。如果您的意图是 new 应该是包标识符,那么您需要将其远离类路径。然而,这样的包名会导致编译错误,所以不可能是这样。
至于该错误,
NullPointerException
相对来说比较容易确定和修复。首先查看堆栈跟踪的第一行:它表明它发生在
ButtonTest3
类的第 42 行,在makeButton()
方法内。现在转到ButtonTest3.java
的第 42 行,它看起来像这样:查看点运算符
.
用于调用方法或访问字段的位置某个对象。NullPointerException
意味着someObject
在特定时刻为null
。没有实例啊!这是一个简单的修复:只需确保在您调用/访问它时不
null
即可:You've a class with the same name in the both folders. In
C:\test
there's a version of theButtonTest3
class which contains a programming bug causing thisNullPointerException
. InC:\test\new
there's a different version of theButtonTest3
class which doesn't contain this bug, or probably there's aButtonTest
class 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
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:It's telling that it has occurred in line 42 of
ButtonTest3
class, 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. TheNullPointerException
means thatsomeObject
isnull
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:好吧,我不相信您可以在单个源文件中定义两个类。您可以将它们定义为子类。
根据 Java 规范:
您可以将
ButtonFrame
放置在ButtonTest2
内。或者,将它们放在不同的 java 文件中。
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:
You could place
ButtonFrame
insideButtonTest2
.Or, put them in different java files.
你的程序中有两个顶级课程,这是错误的。但撇开这一点不谈,你的程序一开始就没有被编译。
要成功编译程序,请使用以下 NppExec 脚本:
确保将 JDK 文件夹设置为
JAVA_HOME
环境变量。然后再试一次。
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:
Make sure that you have your JDK folder set to
JAVA_HOME
environment variable.and try again.