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

时间:2013-12-28 18:09:18

标签: java javabeans

我是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

2 个答案:

答案 0 :(得分:3)

如果在该尝试的中间发生异常,则

is将为null,但是最终将被调用并尝试关闭它

答案 1 :(得分:0)

似乎第53行的某些变量为空 如果我没有错,第53行是:

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

检查是否(InputStream)在使用之前已初始化,或者已成功创建BufferedInputStream。