标题:求助,程序显示没有错误,但是不能运行。
只看楼主
暗静暗静呢
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2015-6-26
结帖率:50%
已结贴  问题点数:20 回复次数:1 
求助,程序显示没有错误,但是不能运行。
package com.ruanko.view;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.JTextComponent;

import com.ruanko.business.FileOperation;

public class JMainFrame extends JFrame{
     private JTextField pathTextField=null;
    private JTextField numberTextField=null;
    private JTextField nameTextFiel=null;
    private JTextField phoneTextField=null;
    private JTextField emailTextField=null;
    private JTextField addressTextField=null;
    private JButton addButton=null;
    private JRadioButton maleButton=null;
    private JRadioButton femaleButton=null;
    private JComboBox relationshipBox=null;

    public  JMainFrame(){
        
        this.setSize(620,500);
        this.setTitle("通讯录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label=new JLabel("欢迎进入通讯录系统");
        this.getContentPane().add(label);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screenSize.width-600)/2,(screenSize.height-500)/2);
        ImageIcon imageIcon=new ImageIcon("image//Contact.png");
        Image image=imageIcon.getImage();
        this.setIconImage(image);
        this.setContentPane(getContentPane());
    }
    public JPanel getContentPane(){
        JPanel mainPanel=new JPanel();
        mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        mainPanel.add(getAddPanel1());
        
        mainPanel.add(getAddPanel2());
        
        mainPanel.add(getAddPanel3());
        
        mainPanel.add(getAddPanel4());
        
        return mainPanel;
    }
    private JPanel getAddPanel1(){
        JPanel addPanel1=new JPanel();
        
        addPanel1.add(new JLabel("联系人路径"));
        JTextField pathTextField = new JTextField(43);
        String defaultPath="F:/contacts.txt";
        pathTextField.setText(defaultPath);
        addPanel1.add(pathTextField);
        
        return addPanel1;
    }
   
    private JPanel getAddPanel2(){
        JPanel addPanel2=new JPanel();
        
        addPanel2.add( new Label("编号:"));
        JTextField numberTextField=new JTextField(12);
        addPanel2.add(numberTextField);
      
        addPanel2.add( new Label("姓名:"));
        JTextField nameTextField=new JTextField(12);
        addPanel2.add(nameTextField);
        
        addPanel2.add( new Label("手机号:"));
        JTextField phoneTextField=new JTextField(12);
        addPanel2.add(phoneTextField);
   
        return addPanel2;
    }
   
    private JPanel getAddPanel3(){
        JPanel addPanel3=new JPanel();
        
        addPanel3.add( new Label("email:"));
        JTextField emailTextField=new JTextField(12);
        addPanel3.add(emailTextField);
        
        addPanel3.add( new Label("地址:"));
        JTextField addressTextField=new JTextField(30);
        addPanel3.add(addressTextField);
        
        return addPanel3;
    }
   
    private JPanel getAddPanel4(){
        JPanel addPanel4=new JPanel();
        
        addPanel4.add(new JLabel("性别"));
        maleButton=new JRadioButton("男");
        femaleButton=new JRadioButton("女");
        addPanel4.add(maleButton);
        addPanel4.add(femaleButton);
        ButtonGroup buttonGroup=new ButtonGroup();
        buttonGroup.add(maleButton);
        buttonGroup.add(femaleButton);
        maleButton.setSelected(true);
        
        addPanel4.add(new JLabel("关系:"));
        
        String[] relationship={"同事","同学","亲戚","朋友"};
        relationshipBox=new JComboBox(relationship);
        addPanel4.add(relationshipBox);
        
        
        addPanel4.add(addButton);
        
        return addPanel4;
    }
   
    private JButton getAddButton(){
        if(addButton==null){
            addButton=new JButton("新增联系人");
        
            addButton.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    String number=numberTextField.getText();
                    JTextComponent nameTextField = null;
                    String name=nameTextField.getText();
                    String phone=phoneTextField.getText();
                    String email=emailTextField.getText();
                    String address=addressTextField.getText();
                    
                    String gender=null;
               
                    if(maleButton.isSelected()){
                        gender="男";
                    }else if(femaleButton.isSelected()){
                        gender="女";
                    }
                    
                    String relationship=(String)relationshipBox.getSelectedItem();
                    
                    
                    String contactInfor=number+"##"+name+"##"+phone+"##"+email+"##"+address+"##"+gender+"##"+relationship;// TODO Auto-generated method stub
                    String filePath=pathTextField.getText();
                    FileOperation fileoperation=new FileOperation();
                    if(fileoperation.saveContact(contactInfor,filePath)){
                        JOptionPane.showMessageDialog(null,"联系人保存成功");
                    }else{
                        JOptionPane.showMessageDialog(null,"联系人保存失败");
                    }
                }
               
            });
        }
        return addButton;
    }
      public static void main(String[] args){
        JMainFrame frame=new JMainFrame();
        frame.setVisible(true);
      }
     }
下面的显示这些:
Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1043)
    at java.awt.Container.add(Container.java:363)
    at com.ruanko.view.JMainFrame.getAddPanel4(JMainFrame.java:116)
    at com.ruanko.view.JMainFrame.getContentPane(JMainFrame.java:48)
    at com.ruanko.view.JMainFrame.<init>(JMainFrame.java:30)
    at com.ruanko.view.JMainFrame.main(JMainFrame.java:162)


搜索更多相关主题的帖子: public import package private null 
2015-11-27 18:32
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
得分:14 
報的是空指針異常可能是那幾個面板定義的是局部變量

一片落叶掉进了回忆的流年。
2015-11-27 23:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-459663-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019175 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved