精華區beta java 關於我們 聯絡資訊
目前想做一個介面有兩個按鈕,一個是讀入HTML的東西,另一個是HTML讀入後丟到HASH裡 原本這兩個是分別兩隻程式 合到一起後製作介面 想說把讀入的資料都string到SS裡 然後HASH,ss裡的資料這樣 但出現Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 原本分個的時候都可以執行 雖然分開時hash時是 String Str = new String("Welcome to Tutorialspoint.com"); System.out.println("Hashcode for Str :" + Str.hashCode() ); 這樣而已 之後介面的程式碼如下 package com.manze.stu; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.net.*; import java.io.*; public class Testface extends JFrame { String ss; private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Testface frame = new Testface(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Testface() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnHash = new JButton("read htm"); btnHash.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { File fileDir = new File("D:\\ea.htm"); BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(fileDir), "UTF8")); while ((ss = in.readLine()) != null) { System.out.println(ss); } in.close(); }catch (UnsupportedEncodingException e1){ System.out.println(e1.getMessage()); }catch (IOException e1){ System.out.println(e1.getMessage()); }catch (Exception e1){ System.out.println(e1.getMessage()); } } }); btnHash.setBounds(10, 22, 123, 23); contentPane.add(btnHash); JButton btnReadHtml = new JButton("hash"); btnReadHtml.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String Str = new String(ss); System.out.println("Hashcode for Str :" + Str.hashCode() ); } }); btnReadHtml.setBounds(10, 88, 123, 23); contentPane.add(btnReadHtml); JButton btnNewButton = new JButton("Signature"); btnNewButton.setBounds(10, 181, 123, 23); contentPane.add(btnNewButton); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.70.226.118