每次我点击JEditorPane中的超链接时,hyperlinkUpdate()给我NullPointerException? [英] hyperlinkUpdate() gives me NullPointerException every time i click a hyperlink in the JEditorPane?

查看:224
本文介绍了每次我点击JEditorPane中的超链接时,hyperlinkUpdate()给我NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想补充说,加载的初始文件是一个包含所有类名称的javadoc,当我点击带有它名字的超链接时,我希望它显示每个类的相应页面,当我打印时在控制台中我没有看到收集相应url的任何问题,但每次我尝试将它添加到JEditorPane时我都会得到NullPointerException。

I just want to add that the initial file loaded is a javadoc that has the names of all the classes, I want it to display the corresponding page for each class when I click on the hyperlink with it's name, when i did prints in the console I didn't see any problems with gathering the corresponding url however I get a NullPointerException every time I try to add it to the JEditorPane.

这是我的程序:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class Help implements HyperlinkListener
 {

     JEditorPane htmlPane;
       String url = "file:///F:/java%2012/Isp/help%20file%20try/doc%202/allclasses-frame.html";
    public void hyperlinkUpdate(HyperlinkEvent event) {
    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      try {

        htmlPane.setPage(event.getURL());
         //url.setText(event.getURL().toExternalForm());
        //System.out.println(event.getURL().toString());

      } 
      catch(IOException ioe) {
       // System.out.print("err");
        // Some warning to user
      }
    }
  }
    public void frame()
    {
       JFrame frame = new JFrame("asdd");
     JLabel l = new JLabel("asdsada");

     try 
         {

  JEditorPane htmlPane = new JEditorPane(url);
  htmlPane.addHyperlinkListener(this);
       htmlPane.setEditable(false);
       frame.add(new JScrollPane(htmlPane));


         } 
catch(IOException ioe) {
  System.err.println("Error displaying " + url);
  } 
frame.setSize(1200,800);
       frame.setVisible(true);


    }

   public static void main(String[] args)
   {
     Help h =new Help();
    h.frame();
   }
 }


推荐答案

但是你在哪里得到这个 NullPointerException

But where are you getting this NullPointerException?

我运行你的示例代码,得到 NullPointerException on:

I run your example code, and got NullPointerException on:

htmlPane.setPage(event.getURL());

所以 htmlPage 字段 null

添加行时:

this.htmlPane = htmlPane;

in:

public void frame() {
    JFrame frame = new JFrame("asdd");
    JLabel l = new JLabel("asdsada");
    try {
        JEditorPane htmlPane = new JEditorPane(url);
        this.htmlPane = htmlPane;

我现在可以点击任何链接(在我的案例中是http://www.google。 com /)

I can now click on any link (in my case in "http://www.google.com/")

这篇关于每次我点击JEditorPane中的超链接时,hyperlinkUpdate()给我NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆