tengqianan 2017-04-09 14:06 采纳率: 0%
浏览 817
已结题

JList和BefaultListModel获取一直报null问题

我已经困扰了好几天,往能给个解答
错误是:Exception in thread "main" java.lang.NullPointerException
可是我怎么调的不行,希望大神给我修改一下

 package globleGet.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import globleGet.download.*;



public class MainFrame extends JFrame{


    private DefaultListModel model ;
    private JList lst_downfiles ;
    private ItemPane itp_fileProperity;
    private ToolPane toolpane;
    private int lst_pos = -1;


    //关闭窗口监听器类
    class JFrameCloseListener extends WindowAdapter{
        public void windowClosing(WindowEvent e){

            saveModel();
            System.exit(0);
        }
    }


    public MainFrame(){
        super("GloabalGet 多线程下载程序");
        ini();
        setGUI();
        setEvent();
        Displace dis = new Displace();
        dis.setDaemon(true);
        dis.setPriority(7);
        dis.start();

    }

    //加载序列化文件
    private void ini() {

            File myDownfiles = new File("E.\\downfiles.data");
            if(myDownfiles.exists()){
                ObjectInputStream ois = null;
                try{
                    ois = new ObjectInputStream(new FileInputStream(myDownfiles));                                                         
                    model = (DefaultListModel)(ois.readObject());
                    ois.close();
                    DownLoadInfo tmp_info;
                    int len = model.getSize();
                    for(int i=0;i<len;i++){
                        tmp_info = (DownLoadInfo)(model.getElementAt(i));
                        if(!tmp_info.isFinished()){
                            MultiDownLoad md = new MultiDownLoad(tmp_info);
                            md.MultiDown();
                        }
                    }
                    lst_pos = len-1;
                }catch(Exception e){
                    try{
                        ois.close();
                    }catch(Exception e1){}
                    JOptionPane.showMessageDialog(this, "加载历史下载文件出错!");
                    return ;
                    }
                }
            else {
                model = new DefaultListModel();
            }

        }

    //保存序列化文件

    private void saveModel(){
        ObjectOutputStream oos = null;
        try{
            oos = new ObjectOutputStream(new FileOutputStream(".\\downfiles.data"));
            oos.writeObject(model);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "系统关闭时出错,可能丢失部分下载信息!");
        }
        finally{
            try{
                oos.close();
            }catch(Exception e2){}
        }
    }
    public void setGUI() {

        //构造list列表布局
        JList lst_downfiles = new JList(model);
        lst_downfiles.setBorder(BorderFactory.createEtchedBorder());
        JScrollPane sp_lst = new JScrollPane(lst_downfiles);
        sp_lst.setPreferredSize(new Dimension(200,360));

        //创建文件下载属性面板
        itp_fileProperity = new ItemPane();
        itp_fileProperity.setPreferredSize(new Dimension(400,360));

        //构建窗口上方布局
        JPanel top_pane = new JPanel();
        top_pane.setLayout(new BorderLayout());
        top_pane.add(sp_lst, BorderLayout.WEST);
        top_pane.add(itp_fileProperity,BorderLayout.CENTER);
        top_pane.setBorder(BorderFactory.createEtchedBorder());

        //创建toolbar面板
        toolpane = new ToolPane();

        //创建Frame布局
        this.setLayout(new BorderLayout());
        this.getContentPane().add(top_pane, BorderLayout.CENTER);
        this.getContentPane().add(toolpane, BorderLayout.SOUTH);

        //设置Frame显示属性
        this.setSize(600,400);
        this.setLocation(200, 200);
        this.setVisible(true);
        this.setResizable(false);
    }

    public void setEvent() {

        this.toolpane.btnTask.addActionListener(new AddTaskListener(this));
        this.toolpane.btnClose.addActionListener(new CloseListener());

        //以下这行代码是我错误的源头,一个给我报Null 
        this.lst_downfiles.addListSelectionListener(new SelectTaskListener());

        this.addWindowListener(new JFrameCloseListener());
    }

    //关闭按钮监听器类
    class CloseListener implements ActionListener{
        public void actionPerformed(ActionEvent e){

            saveModel();
            System.exit(0);
        }

    }



    //列表框选择内容改变监听器
    class SelectTaskListener implements ListSelectionListener{
        public void valueChanged(ListSelectionEvent e){
            int index = lst_downfiles.getSelectedIndex();
            setFileProperity(index);
            lst_pos = index;
        }
    }


    //添加下载任务监听器类
    class AddTaskListener implements ActionListener{
        MainFrame f;
        public AddTaskListener(MainFrame f){
            this.f = f;
        }

        public void actionPerformed(ActionEvent e){
            //调用添加任务对话框
            TaskDialog taskInfo = new TaskDialog(f);

            //创建下载任务
            if(taskInfo.getBtn()){
                addTask(taskInfo);
            }
        }
    }

    class Displace extends Thread{
        public void run(){
            while(true){
                if(lst_pos != -1){
                    setFileProperity(lst_pos);
                }
                try{
                    Thread.sleep(100);
                }catch(InterruptedException e){}
            }
        }
    }

    //设置下载文件属性
    private void setFileProperity(int index){

        DownLoadInfo tmp_info = (DownLoadInfo)(model.elementAt(index));
        String tmp_file = tmp_info.getSaveFileName();
        float tmp_len = tmp_info.getFileLength();
        itp_fileProperity.lab_filename.setText(tmp_file.substring(tmp_file.lastIndexOf("\\")+1));
        if(tmp_len<500){
            itp_fileProperity.lab_filesize.setText(tmp_len+"bytes");
        }
        else{
            if(tmp_len < 1024*1024){
                tmp_len = Math.round(tmp_len/1024*100)/100.0f;
                itp_fileProperity.lab_filesize.setText(tmp_len+"k");
            }
            else {
                tmp_len = Math.round(tmp_len/1024/1024*100)/100.0f;
                itp_fileProperity.lab_filesize.setText(tmp_len+"M");
            }
        }
        itp_fileProperity.pbar_model.setValue(tmp_info.getDownProgress());
        itp_fileProperity.pbar.setString("下载进度"+itp_fileProperity.pbar.getValue()+"%");

        tmp_len = tmp_info.getDownSpeed();
        itp_fileProperity.lab_downspeed.setText(tmp_len+"k/s");
        itp_fileProperity.lab_threadnum.setText(tmp_info.getThreadNum()+"");

        String tmp_str = tmp_file;
        if(tmp_file.length()>20){
            tmp_str = tmp_file.substring(0,19)+".....";
        }

        itp_fileProperity.lab_filepath.setText(tmp_str);
        itp_fileProperity.lab_filepath.setToolTipText(tmp_file);

        if(tmp_info.getDownURL().length()>20){
            tmp_str = tmp_info.getDownURL().substring(0,19)+".....";
        }
        itp_fileProperity.lab_url.setText(tmp_str);
        itp_fileProperity.lab_url.setToolTipText(tmp_info.getDownURL());

        if(tmp_info.isFinished()){
            itp_fileProperity.lab_filestate.setText("下载完成");
        }else{
            if(tmp_info.isError()){
                itp_fileProperity.lab_filestate.setText("下载错误");
            }else{
                itp_fileProperity.lab_filestate.setText("正在下载中....");
            }
        }
    }

    //创建一个下载任务
    private void addTask(TaskDialog taskInfo){
        DownLoadInfo tmp_info = new DownLoadInfo(taskInfo.getUrlStr(),taskInfo.getFileStr(),3);
        if(!tmp_info.getIsValidate()){
            JOptionPane.showMessageDialog(this, "文件无法下载!");
            return ;
        }
        MultiDownLoad md = new MultiDownLoad(tmp_info);
        md.MultiDown();

        model.addElement(tmp_info);//将指定组件添加到此类表的末尾。

        int index = model.getSize() - 1;

        setFileProperity(index);
        lst_pos = index;
    }

    public static void main(String[] args)
    {

       new MainFrame();
    }
}
  • 写回答

2条回答 默认 最新

  • warmcore 2017-04-09 14:54
    关注

    具体点问题是什么。JList无法用model里的数据建列表?

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题