1

这是我的课程,它是一个由两个选项卡组成的 gui,我的个人资料和编辑个人资料。当索引值更改为1.当标签索引为0时,我在“MyProfiletabStateChanganged”方法中存在问题,'MyProfile()'方法成功执行,但是当索引为1时,'EditProfile()'给出太多错误. editprofile() 的目的只是从 bean 类中提取值并将其设置为适当的文本字段。我究竟做错了什么?请注意使用 setter 方法正确填充了 bean 类变量,但是在这个类中我无法使用 getter 检索值?也许它正在检索,但问题在于无法将其设置为文本字段。

public class MainMenu extends javax.swing.JFrame {

    Academic ac = new Academic();
    academicBean bn = new academicBean();

    /**
     * Creates new form MainMenu
     */
    public MainMenu() {
        initComponents();
        // myProfile(); 
        // editProfile(); 
    }

    public void myProfile() {
        ac.retrieveAcademic();
        nameLabel.setText(""+ac.title+" "+ac.forename+" "+ac.surname);
        roleLabel.setText("Role:    " + ac.role);
        roomLabel.setText("Room:    " + ac.room);
        pageLabel.setText("Page:    " + ac.page);
        hoursLabel.setText("Hours:   " + ac.hours);
        phoneLabel.setText("Phone:   " + ac.phone);
        mobileLabel.setText("Mobile:  " + ac.mobile);
        emailLabel.setText("Email:   " + ac.email);
        imageLabel.setIcon(ac.format);
    }

    public void editProfile() {
        ac.retrieveAcademic();
        idLabel.setText("Academic Id:    "+bn.getAcademicId());
        txt_title.setSelectedItem(bn.getTitle().toString());
        txt_fn.setText(bn.getForename().toString());
        txt_ln.setText(bn.getSurname().toString());
        combo_role.setSelectedItem(bn.getRole().toString());
        txt_room.setText(bn.getRoom().toString());
        txt_page.setText(bn.getPage().toString());
        txt_hours.setText(bn.getHours().toString());
        txt_phone.setText(bn.getPhone().toString());
        txt_mobile.setText(bn.getMobile().toString());
        txt_email.setText(bn.getEmail().toString());
    }

    private void myProfileTabStateChanged(javax.swing.event.ChangeEvent evt) {
        JTabbedPane sourceTabbedPane = (JTabbedPane) evt.getSource();
        int index = sourceTabbedPane.getSelectedIndex();

            if (index == 0) {
            myProfile();
        }

        else if (index == 1) {
            editProfile(); 
        }
    }

//类学术

public class Academic extends javax.swing.JFrame {
   String filename = null;
   int s = 0;
   byte[] person_image = null;
   ImageIcon format = null;

   LoginBean l = new LoginBean();
   Connection con = javaconnect.ConnectDB();
   academicBean bean = new academicBean();

   PreparedStatement pst = null;
   ResultSet rs = null;

   int id;
   String title;
   String titleValue;
   String forename;
   String surname;
   String role;
   String roleValue;
   String room;
   String page;
   String hours;
   String phone;
   String mobile;
   String email;
   byte[] imagedata = null;

   public Academic() {
      initComponents();
   }

   @SuppressWarnings("unchecked")
   public void retrieveAcademic() {

      try {
         pst = con
               .prepareStatement("SELECT * FROM AcademicInfo where Email=? and Password=?");
         pst.setString(1, l.getUsername());
         pst.setString(2, l.getPassword());
         rs = pst.executeQuery();
         while (rs.next()) {
            id = (rs.getInt(1));
            bean.setAcademicId(id);
            title = (rs.getString(2));
            bean.setTitle(title);
            forename = (rs.getString(3));
            bean.setForename(forename);
            surname = (rs.getString(4));
            bean.setSurname(surname);
            role = (rs.getString(5));
            bean.setRole(role);
            room = (rs.getString(6));
            bean.setRoom(room);
            page = (rs.getString(7));
            bean.setPage(page);
            hours = (rs.getString(8));
            bean.setHours(hours);
            phone = (rs.getString(9));
            bean.setPhone(phone);
            mobile = (rs.getString(10));
            bean.setMobile(mobile);
            email = (rs.getString(11));
            bean.setEmail(email);
            imagedata = (rs.getBytes("Image"));
            format = new ImageIcon(imagedata);

         } // end while

      } catch (SQLException ex) {
         Logger lgr = Logger.getLogger(Academic.class.getName());
         lgr.log(Level.SEVERE, ex.getMessage(), ex);

      } finally {

         try {
            if (rs != null) {
               rs.close();
            }
            if (pst != null) {
               pst.close();
            }
            if (con != null) {
               con.close();
            }

         } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Academic.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
         }
      }
   }
}

// 豆类

public class AcademicBean {

private int academicid;
private String title;
private String forename;
private String surname;
private String role;
private String room;
private String page;
private String hours;
private String phone;
private String mobile;
private String email;
private byte []  image;
private String pass; 

 //Setters

public void setAcademicId (int academicid) {
    this.academicid = academicid;
}   
public void setTitle(String title) {
    this.title = title;
}   
public void setForename(String forename) {
    this.forename = forename;
}
public void setSurname(String surname) {
    this.surname = surname;
}
public void setRole(String role) {
    this.role = role;
}
public void setRoom(String room) {
    this.room = room;
}
public void setPage(String page) {
    this.page = page;
}
public void setHours(String hours) {
    this.hours = hours;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public void setMobile(String mobile) {
    this.mobile = mobile;
}

public void setEmail(String email) {
    this.email = email;
}
public void setImage (byte [] image) {

     this.image = image; 
 }
public void setPassword (String pass) {

     this.pass= pass;
 }



//Gettters 

public String getPassword () {

     return pass;  
 }

public int getAcademicId() {

     return academicid; 
 }


public byte [] getImage() {
     return image;
 }
public String getTitle() {
    return title;
}
public String getForename() {
    return forename;
}
public String getSurname() {
    return surname;
}
public String getRole() {
    return role;
}
public String getRoom() {
    return room;
}
public String getPage() {
    return page;
}
public String getHours() {
    return hours;
}
public String getPhone() {
    return phone;
}
public String getMobile() {
    return mobile;
}
public String getEmail() {
    return email;
}
}

//堆栈跟踪

线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
  在 org.sqlite.PrepStmt.(PrepStmt.java:37)
  在 org.sqlite.Conn.prepareStatement(Conn.java:231)
  在 org.sqlite.Conn.prepareStatement(Conn.java:224)
  在 org.sqlite.Conn.prepareStatement(Conn.java:213)
  在 eecsCRM.Academic.retrieveAcademic(Academic.java:68)
  在 eecsCRM.MainMenu.editProfile(MainMenu.java:50)
  在 eecsCRM.MainMenu.myProfileTabStateChanged(MainMenu.java:569)
  在 eecsCRM.MainMenu.access 300 美元(MainMenu.java:13)
  在 eecsCRM.MainMenu$4.stateChanged(MainMenu.java:194)
  在 javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:400)
  在 javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:253)
  在 javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:116)
  在 javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:50)
  在 javax.swing.JTabbedPane.setSelectedIndexImpl (JTabbedPane.java:599)
  在 javax.swing.JTabbedPane.setSelectedIndex(JTabbedPane.java:574)
  在 javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(BasicTabbedPaneUI.java:3628)
  在 javax.swing.plaf.synth.SynthTabbedPaneUI$1.mousePressed(SynthTabbedPaneUI.java:279)
  在 java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262)
  在 java.awt.Component.processMouseEvent(Component.java:6264)
  在 javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
  在 java.awt.Component.processEvent(Component.java:6032)
  在 java.awt.Container.processEvent(Container.java:2041)
  在 java.awt.Component.dispatchEventImpl(Component.java:4630)
  在 java.awt.Container.dispatchEventImpl(Container.java:2099)
  在 java.awt.Component.dispatchEvent(Component.java:4460)
  在 java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
  在 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
  在 java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
  在 java.awt.Container.dispatchEventImpl(Container.java:2085)
  在 java.awt.Window.dispatchEventImpl(Window.java:2478)
  在 java.awt.Component.dispatchEvent(Component.java:4460)
  在 java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
  在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
  在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
  在 java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:174)
  在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
  在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
  在 java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
4

1 回答 1

1

只是一个疯狂的猜测:您似乎至少有两个完全不相关的 AcademicBean 对象(请注意,该类应命名为AcademicBean),一个在 Academic 中,一个在 MainMenu 中。我怀疑它们应该是相同的。

也许 MainMenu 应该有:

academicBean bn = ac.getAcademicBean(); 

和学术有:

public academicBean getAcademicBean() {
   return bean;
}

以便 MainMenu 可以提取 Academic 的 bean,使 bean 变量引用同一个 bean 对象。

于 2012-12-29T13:02:03.957 回答