0

我正在Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException运行通过 JNLP 启动的小程序。

我的小程序代码是

package com.oprs.common;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.jnlp.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.jfree.util.Log;
/**
 *
 * @date Aug 29, 2012 11:33:16 AM
 * @version 1.0
 */
public class OprsJNLP {

    /**
     * @param args
     */
    static BasicService basicService = null;

    public static void main(String[] args) {
    JFrame frame = new JFrame("OPRS");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel();
    Container content = frame.getContentPane();
    content.add(label,BorderLayout.CENTER);
    String message = "Download OPRS Application";
    label.setText(message);

    try {
        basicService = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
    } catch (UnavailableServiceException e) {
        Log.error("service not available", e);
    }

    JButton button = new JButton("http://google.com/");

    ActionListener listener = new ActionListener() {

        public void actionPerformed(ActionEvent event) {
        URL url;
        try {
            url = new URL(event.getActionCommand());
            basicService.showDocument(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }       
        }
    };

    button.addActionListener(listener);

    content.add(button, BorderLayout.SOUTH);
    frame.pack();
    frame.show();
    }
}

我尝试调试,发现 basicService 总是返回 null。代替 google.com,我尝试使用其他网址,但仍然遇到相同的错误。该行basicService.showDocument(url);是它抛出空指针的地方,因为我的 basicService 为空。有人能指出我到底哪里出错了吗?

包括我的 .jnlp

<?xml version='1.0' encoding='UTF-8' ?>
<jnlp spec='1.0'
      codebase='http://localhost:9999/'
      href='oprs.jnlp'>
  <information>
    <title>OPRS</title>
    <vendor>OPRS</vendor>
    <description kind='one-line'>
      OPRS WEB START
    </description>
    <shortcut online='false'>
      <desktop/>
    </shortcut>
  </information>
  <resources>
    <j2se version='1.4+' />
    <jar href='oprs.jar' main='true' />
  </resources>
  <application-desc main-class='com.oprs.OprsJNLP' />
</jnlp>
4

0 回答 0