-2
    public void getEvent(String tableClick) {

    Events e = new Events();
    try {
        pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
        rs = pst.executeQuery();
        while(rs.next()){      
        e.setEventName(rs.getString(2));
        System.out.println(rs.getString(2));
        e.setEventDate(rs.getDate(3));
        e.setEventTime(rs.getString(4));
        e.setEventVenue(rs.getString(5));
        e.setEventDetail(rs.getString(6));
        e.setEventOpportunity(rs.getString(7));
        e.setEventMoreDetails(rs.getString(8));
        e.setEndTime(rs.getString(9));
        rs.close();
        pst.close();
       }
    } 
    catch(SQLException ex){
    ex.printStackTrace();
    } 
} //end getEvent

Whilst the System.out.println(rs.getString(2) print a value from database, I am unable to populate this information in my bean.

I am using mutators to populate the JavaBean, but when I try to use the accessors in my View class of the MVC framework, it displays null.

Here is my Call from VIEW class

Method in VIEW class

     public void changeDisplay() {
       Events e = new Events();
       evTitle.setText(""+e.getEventName());
       evWhen.setText("When: "+ e.getEventDate());
       evWhere.setText("Where: "+ e.getEventVenue());
       evDescription.setText("Description: "+ e.getEventDetail());
       evOpportunity.setText("Opporunity: "+ e.getEventOpportunity());
       evMoreDet.setText("More Details: "+ e.getEventMoreDetails());
   }
MooHa
  • 819
  • 3
  • 11
  • 23
  • 1
    nobody knows, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable with hardcoded value instead of `JDBC` – mKorbel Jan 20 '13 at 15:51
  • @MooHa: Don't neglect to up-vote answers you found helpful. – trashgod Jan 20 '13 at 17:03

3 Answers3

1

This code can't work... Why do you have a method called getEvent with a void return type? What you want is probably something like that :

public Event getEvent(String tableClick) {
Events e = new Events();
try {
    pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
    rs = pst.executeQuery();
    while(rs.next()){      
    e.setEventName(rs.getString(2));
    System.out.println(rs.getString(2));
    e.setEventDate(rs.getDate(3));
    e.setEventTime(rs.getString(4));
    e.setEventVenue(rs.getString(5));
    e.setEventDetail(rs.getString(6));
    e.setEventOpportunity(rs.getString(7));
    e.setEventMoreDetails(rs.getString(8));
    e.setEndTime(rs.getString(9));
    rs.close();
    pst.close();
    return e;
   }
} catch(SQLException ex) {
    ex.printStackTrace();
    return null;
} 

} //end getEvent

I don't know how your code is working but you probably want to called that method from the view and then display the value. Your changeDisplay method is displaying null because you just created the object...

You probably need something like :

public void changeDisplay(Events e) {
    evTitle.setText(""+e.getEventName());
    evWhen.setText("When: "+ e.getEventDate());
    evWhere.setText("Where: "+ e.getEventVenue());
    evDescription.setText("Description: "+ e.getEventDetail());
    evOpportunity.setText("Opporunity: "+ e.getEventOpportunity());
    evMoreDet.setText("More Details: "+ e.getEventMoreDetails());
}
Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
  • `Events e = object.getEvent("string");` is not something I want to do. because I supply this argument in Controller? any other ways? – MooHa Jan 20 '13 at 16:00
  • It's an example, you can get it whatever you want and then pass it to the view as a parameter... I don't know how you have structure your application – Jean-Philippe Bond Jan 20 '13 at 16:01
  • thanks I was able to pass from my Controller to View. But I am curious is it a good practice to pass from Controller. in the controller it has now `Events e = model.getEvent(tableClick);` and `view.changeDisplay(e);`, does it conform to the logic here (http://stackoverflow.com/questions/5217611/the-mvc-pattern-and-swing) – MooHa Jan 20 '13 at 16:03
  • I don't since I didn't saw your code. But if you are not sure, look at this example, it might help you : http://www.oracle.com/technetwork/articles/javase/index-142890.html – Jean-Philippe Bond Jan 20 '13 at 16:14
0

The Events you are creating in changeDisplay() method is a new object, not the one you populated in getEvent. You need to return/store the e from getEvent method and use it where ever required.

RP-
  • 5,827
  • 2
  • 27
  • 46
0

In getEvent function you are creating an object of Events class and you are setting database values in it using setters.

But in changeDisplay Function you are again creating an object of Events class and try to get values using getters.

public void changeDisplay() {
   Events e = new Events();

In place of creating new object of Events class in changeDisplay function you have to use the same object of Events class which you created in getEvent function.

Beacause you set the values in different object and you are trying to get values from different object of Events class thats why it is showing null value to you.

Hunter
  • 820
  • 8
  • 19