I am unable to execute the editProfile() method in MainMenu class. The purpose of the method is to be able to extract the bean values and display them on the empty textfield. I am not sure if i did it correctly. any recommendations for any changes to editProfile() method.

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

//Class Academic

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

//Bean Class

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







}

Recommended Answers

All 8 Replies

The NullPointerException nessage tells you the exact line where it happened. It's a lot easier if you share that with us! You can also help by printing the values of all the variables and method calls used on that exact line so we all know which one was null.

Here is the stacktrace, all values of the variables are null. i think im missing something small. the problem is really in these lines txt_hours.setText(bn.getHours().toString());, where bn is a reference to the AcademicBean class object. the AcademicBean class has the values and I just cant access them :(

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at eecsCRM.MainMenu.myProfileTabStateChanged(MainMenu.java:570)
    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)
BUILD SUCCESSFUL (total time: 13 seconds)

So what was the result of the print? WHich of the folllowing candidates on that line is the null value?
txt_hours
bn
bn.getHours() // probably this one?

plus (just a guess) is there any connection anywhere between "bean" in Academic (which gets populated wih values) and "bn" in MainMenu (which is where you try to get your values)?

all variable of the class AcademicBean returns null, whether its bn.forename, bn.surname or whatever. This implies I am probably missing something key.

The Academic class instantiates an object of AcadamicBean class (bean) to give a value to the variables in AcademicBean class. and in MainMenu I instantiate again an object of the AcademicBean class but this time i try to use getter method (bn.getEmail()) which returns null, this is the problem... its already has values but why is it not returning?

It looks like you have 2 beans, as I guessed before. One gets populated with values, the other is never populated, so it's all nulls. It's the second one you try to use in your form. Remember that those values belong to each instance of the class, not to the class itself. Every instance has its own values.
You need a single instance of the bean that is populated in Academic then used in MainMenu. One way to do that would be for retrieveAcademic() to return the bean it has created (instead of being void). Then MainMenu can use that bean.

would it just be easier have just one bean which is Academic and ignore AcademicBean totally? and thereofre have the getter setter method within Academic rather than AcademicBean?

would you be kind enough to show how to return the bean in retrieveAcademic() ?

Your class structure looks OK. There's the Bean, which holds info about one (whatever it is), and another class that handles the database access to get the data for a bean, and a GUI class that displays stuff from a bean. That's all good structure.

I'm sure you already know how to return an object from a method. You've done it many times. It's as simple as

public Thing getThing() {
   Thing theThing = new Thing();
   theThing.setBlahBlahBlah... ... ...
   return theThing;
}

thankyou James

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.