问题描述
我有2个Java程序分别位于
中,一个位于c:\test中,另一个位于c:\test\new
我可以编译它们都没有任何错误\javac
但是当我尝试执行文件\java
时,它显示出这样的错误
线程 AWT-EventQueue-0中的异常java.lang.NullPointerException
在ButtonFrame.makeButton(ButtonTest3.java:42) < init>(ButtonTest3.java:29)
在ButtonTest $ 1.run(ButtonTest.java:17)
在java.awt.event.InvocationEvent.dispatch(未知来源)java.awt.EventQueue.dispatchEvent(未知来源)
(java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)
在java.awt.EventDispatchThread.pumpEvents(未知来源)java.awt.EventDispatchThread.pumpE在
java.awt.EventDispatchThread.run上的vents(未知源)
(未知源)
我把它放在我的类路径中
但是如果我将CLASSPATH中的值顺序更改为此
错误根本消失了
为什么?
仅顺序重要吗?
您在两个文件夹中都有一个具有相同名称的类。在 C:\test 中,有一个 ButtonTest3 类的版本,其中包含导致此<$ c $的编程错误。 c> NullPointerException 。在 C:\test\new 中,有一个不同版本的 ButtonTest3 类,其中不包含此错误。 ,或者可能有一个 ButtonTest 类,它的作用与 C:\test 中的完全不同。
清理您的类路径。在类路径中重复具有相同签名的不同版本的类是不好的。如果您的意图是 new 应该是程序包标识符,则需要将其远离类路径。但是,这样的程序包名称会导致编译错误,所以不能这样。
该错误, NullPointerException 对于确定和修复相对而言是微不足道的。首先看一下堆栈跟踪的第一行:
在ButtonFrame.makeButton(ButtonTest3.java:42)
这表明它已发生在 ButtonTest3 类的第42行中,在 makeButton()方法中。现在转到 ButtonTest3.java 的第42行,它将类似于:
someObject.doSomething();
在哪里找到点运算符。用于调用方法或访问某些对象的字段。 NullPointerException 表示 someObject 在特定时刻为 null 。没有实例!
这是一个简单的解决方法:只需确保它不是 null 目前正在调用/访问它:
someObject = new SomeObject();
// ...
someObject.doSomething();
I have 2 java program located seperatelyOne 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 \javait 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
but if i change the order of the value in CLASSPATH to this
the error is simply gone
Why?? this could happeningOnly 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();
这篇关于CLASSPATH中的值顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!