gpt4 book ai didi

java - 为什么我得到 : java. lang.NullPointerException?

转载 作者:行者123 更新时间:2023-11-29 05:31:48 26 4
gpt4 key购买 nike

我是 Java 的新手,我在网上找到了这个脚本(这是我修改过的版本)。我已经创建了一个 Javabean(因此我可以在我的应用程序中使用它),但是当我测试它时,我得到了这个错误。此外,我收到警告 “java 使用或覆盖已弃用的 API”:

Java Plug-in 1.6.0_45
Using JRE version 1.6.0_45-b06 Java HotSpot(TM) Client VM
User home directory = C:\Users\xyz
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
proxyHost=null
proxyPort=0
connectMode=HTTP, native.
Forms Applet version is : 10.1.2.3
NO INGRESA AL PRINT
java.lang.NullPointerException
at JavaGetUrl.setProperty(JavaGetUrl.java:53)
at oracle.forms.handler.UICommon.setFVP(Unknown Source)
at oracle.forms.handler.UICommon.setFVP(Unknown Source)
at oracle.forms.handler.UICommon.onUpdate(Unknown Source)
at oracle.forms.handler.ComponentItem.onUpdate(Unknown Source)
at oracle.forms.handler.JavaContainer.onUpdate(Unknown Source)
at oracle.forms.handler.UICommon.onUpdate(Unknown Source)
at oracle.forms.handler.JavaContainer.onCreate(Unknown Source)
at oracle.forms.engine.Runform.onCreateHandler(Unknown Source)
at oracle.forms.engine.Runform.processMessage(Unknown Source)
at oracle.forms.engine.Runform.processSet(Unknown Source)
at oracle.forms.engine.Runform.onMessageReal(Unknown Source)
at oracle.forms.engine.Runform.onMessage(Unknown Source)
at oracle.forms.engine.Runform.sendInitialMessage(Unknown Source)
at oracle.forms.engine.Runform.startRunform(Unknown Source)
at oracle.forms.engine.Main.createRunform(Unknown Source)
at oracle.forms.engine.Main.start(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我想用这段代码做的是:

  • 捕获(到一个变量)包含在 URL 中的文本(我知道这实际上是它所做的)
  • 如果我传递的链接生成一个 HTML 或 PDF 文件,然后创建它或将其保存在本地机器上(任何关于如何实现这个或任何其他完成它的 JAVA 的建议将非常感激)

这是我实际的 java 源文件:

import java.io.*;
import java.net.*;
import oracle.forms.properties.*;
import oracle.forms.ui.*;

public class JavaGetUrl extends VTextArea {
private static final ID GEN = ID.registerProperty("cre");


//-----------------------------------------------------//
// Step 1: Start creating a few objects we'll need.
//-----------------------------------------------------//

URL u;
InputStream is = null;
DataInputStream dis;
String s;


public boolean setProperty(ID id, Object value) {
boolean retorno = true;
try {
System.out.println("INGRESA AL TRY");
if (id == GEN) {

System.out.println("INGRESA AL IF");

//------------------------------------------------------------//
// Step 2: Create the URL. //
//------------------------------------------------------------//
// Note: Put your real URL here, or better yet, read it as a //
// command-line arg, or read it from a file. //
//------------------------------------------------------------//

u = new URL("http://200.210.220.1:8080/index.shtml");

//----------------------------------------------//
// Step 3: Open an input stream from the url. //
//----------------------------------------------//

is = u.openStream(); // throws an IOException

//-------------------------------------------------------------//
// Step 4: //
//-------------------------------------------------------------//
// Convert the InputStream to a buffered DataInputStream. //
// Buffering the stream makes the reading faster; the //
// readLine() method of the DataInputStream makes the reading //
// easier. //
//-------------------------------------------------------------//

//
dis = new DataInputStream(new BufferedInputStream(is));

//------------------------------------------------------------//
// Step 5: //
//------------------------------------------------------------//
// Now just read each record of the input stream, and print //
// it out. Note that it's assumed that this problem is run //
// from a command-line, not from an application or applet. //
//------------------------------------------------------------//


//
while ((s = dis.readLine()) != null) {
System.out.println(s);
}
}
else {
System.out.println("NO INGRESA AL PRINT");
return false;
}
} catch (MalformedURLException mue) {

System.out.println("Ouch - a MalformedURLException happened.");
mue.printStackTrace();
System.exit(1);

} catch (IOException ioe) {
System.out.println("Oops- an IOException happened.");
ioe.printStackTrace();
System.exit(1);

}

finally {


//---------------------------------//
// Step 6: Close the InputStream //
//---------------------------------//

try {
is.close();
} catch (IOException ioe) {
// just going to ignore this one
}

} // end of 'finally' clause
return retorno;
}
} // end of class definition

最佳答案

is 如果在尝试过程中发生异常,则为 null,但您的 finally 将被调用并尝试关闭它

关于java - 为什么我得到 : java. lang.NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20817707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com