当前位置: 首页 > 知识库问答 >
问题:

使用bean类填充Jtextfield时的java.lang.NullPointerException

赵君植
2023-03-14

这是我的类,它是一个由两个选项卡组成的gui,my profile和edit profile。当索引值更改为1时,'My ProfiletabStateChanged'方法出现问题。当tab索引为0时,'my profile()'方法成功执行,但当索引为1时,'edit profile()'错误太多。editprofile()的目的只是从bean类中提取值,并适当地将其设置为textfields。我做错了什么?请注意bean类变量是使用setter方法正确填充的,但在这个类中,我无法使用getter来检索值?也许它正在检索,但问题在于无法将其设置为TextField。

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;
}
}

//Stacktrace

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

共有1个答案

弘浩博
2023-03-14

只是一个疯狂的人会猜测:您似乎至少有两个完全不相关的academicBean对象(注意,该类应该命名为aCademicBean),一个在学术中,一个在MainMenu中。我怀疑他们应该是一体的。

也许MainMenu应该有:

academicBean bn = ac.getAcademicBean(); 

和学术上有:

public academicBean getAcademicBean() {
   return bean;
}

这样MainMenu就可以提取Actional的bean,使bean变量引用同一个bean对象

 类似资料:
  • 问题内容: 我接下来还有几粒豆: 我正在尝试执行下一个Hibernate查询: 问题: 我了解Hibernate正在寻找类似的东西: 代替: 关于如何在不创建新bean的情况下解决此问题的想法? 谢谢! 问题答案: 我写了一个ResultTransformer可以解决您的问题。它的名称是AliasToBeanNestedResultTransformer,请在github上查看。

  • 问题内容: 嗨,我非常需要一些帮助,我已经在jbutton启用之前搜索了有关要填充的Jtextfield的文档,大多数人都使用DocumentListener来确定是否填充了Jtextfield。我尝试了DocumentListener,它可以工作,但是我想要的是所有Jtextfield都不能为空,然后Jbutton启用,这是我的代码。 问题答案: 表示所有字段必须为空。有时您需要大声朗读此逻辑…

  • 这里有一个关于我要做的事情的参考...访问JavaFX TableView ObservableRayList中的子类属性 但是仅仅使用示例代码,我基本上重写了2个方法,每个3次...在实际的程序中还有更多的东西。(再多一点点)

  • 我正在尝试从相应的字符串值填充Kotlin数据类。我使用GSON查看了来自Json的:Kotlin数据类,但我试图做的并不是跟踪完全相同的: 调用此函数并尝试填充以下类: 但没有欢乐。stringValue是gson.toJson(value)生成的东西,其中value是一个JSONObject。 有人知道我的断线是什么吗?

  • 问题内容: 我如何使用该字段注入包含Map的属性文件,以用作其他构造函数arg。 从属性文件加载地图 该bean当前使用以下命令进行设置: 相当于Java: 谢谢 问题答案: 像这样: 这利用了使用任意静态工厂方法创建bean的能力,在这种情况下,使用工厂工厂方法来创建bean,然后将其注入到的适当构造函数中。

  • /views/fornecedor/ordersearch.xhtml@34,84 value=“#{order.id}”:类“java.util.HashSet”没有属性“id”。 和: