Results 1 to 11 of 11
  1. #1
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default problem while using return statement

    Hi all,
    First of all Hello to everyone.i am a beginner in java.i tried to develop a program
    to set values from two classes and get it in second class.i have developed two forms using comobox.From the first form i set the corresponding value to a bean called 'Data'.After that i had written the code to go to the second form.From there again i set the value of second form to the same bean but using different name .But when i tried to get the two values there.for the first value it is showing as"null".for second value its ok.why is it so?plz help me...i will copy my code below...........
    //FIRST FORM//
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Ernakulam extends JFrame implements ItemListener
    {
    Container c;
    JComboBox cb1;
    JLabel l1,l2;
    public Ernakulam()
    {
    c=getContentPane();
    cb1=new JComboBox();
    c.add(cb1);
    cb1.addItem("Select Here");
    cb1.addItem("Aluva");
    cb1.addItem("Cochin");

    cb1.addItem("Kalamassery");

    cb1.setBounds(200,200,200,30);
    l1=new JLabel();
    c.add(l1);
    l1.setBounds(300,300,150,30);
    l2=new JLabel();
    c.add(l2);
    cb1.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie)
    {
    Object aa=ie.getItem();
    String s1=aa.toString();
    l1.setText(s1);
    if(s1.equals("Select Here"))
    {
    l1.setText("Select your location in Thiruvananthapuram");
    }
    else
    {
    this.setVisible(false);
    TVPM e=new TVPM();
    e.setSize(1200,1200);
    e.setVisible(true);
    Data d=new Data();
    d.setSource(s1);
    }
    }

    public static void main(String args[])
    {
    Ernakulam e=new Ernakulam();
    e.setSize(1200,1200);
    e.setVisible(true);
    }
    }
    //Second form//
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TVPM extends JFrame implements ItemListener
    {
    Container c;
    JComboBox cb1;
    JLabel l1,l2;
    public TVPM()
    {
    c=getContentPane();
    cb1=new JComboBox();
    c.add(cb1);
    cb1.addItem("Select Here");
    cb1.addItem("Trivandrum Central");
    cb1.addItem("Attingal");

    cb1.addItem("Neyyatinkara");

    cb1.setBounds(200,200,200,30);
    l1=new JLabel();
    c.add(l1);
    l1.setBounds(300,300,150,30);
    l2=new JLabel();
    c.add(l2);
    cb1.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie)
    {
    Object aa=ie.getItem();
    String s1=aa.toString();
    l1.setText(s1);
    if(s1.equals("Select Here"))
    {
    l1.setText("Select your location in Thiruvananthapuram");
    }
    else
    {
    Data d=new Data();
    d.setDestination(s1);
    String a=d.getSource();
    System.out.println(a);
    String b=d.getDestination();
    System.out.println(b);
    }
    }

    public static void main(String args[])
    {

    }
    }
    //DATA BEAN//
    public class Data
    {
    String source;
    String destination;
    public void setDestination(String destination)
    {
    this.destination=destination;

    }

    public String getDestination()
    {
    return destination;
    }
    public void setSource(String source)
    {
    this.source=source;


    }
    public String getSource()
    {

    return source;
    }
    }
    pls help me.Tell me the error.Also help me to change the code so that i can get the result.WAITING FOR THE EARLIEST REPLY.....................

  2. #2
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default Plz Help

    Somebody please help me.I am waiting for the last 2 days.still waiting.plz help me.plz reply

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You set yourData object d's destination here
    Java Code:
        else
        {
          Data d = new Data();
          d.setDestination(s1);
          String a = d.getSource();
          System.out.println("a: " + a);  //TODO: why is a null?
          String b = d.getDestination();
          System.out.println("b: " + b);
        }
    but where do you set its source? So of course it will be null.

    I think that your confusion is that you're thinking that the Data object set in first class is the same as that of the second class, but of course this can't be true because in the second class you're creating a new object, one totally unrelated to the previous Data object. To fix this you probably should pass a reference of the Data object created in the first class's object to the second class's object rather than create a new Data object in the second class. Make sense?

    One place to pass this is via the TVPM"s constructor, or your could have a setData(Data data) method in TVPM and set it there. If you do it via the constructor, you'll have to call the constructor after you've created the data object of course.
    Last edited by Fubarable; 12-11-2008 at 05:33 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    This is yet another good reason why GUI and logic code should be kept separate. Presently there's so much GUI code that it buries your other code. If they were kept separate, you'd probably see this error in a minute.

    Also, if you get a chance you should read up on how to use layout managers with Swing. It will make your coding a lot easier, more maintainable and your GUI's prettier.
    Last edited by Fubarable; 12-11-2008 at 05:37 PM.

  5. #5
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default @ Fubarable

    dear sir,
    can u pls change my code and paste it here.so that i can understand it easily

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    No, sorry, but I strongly feel that changing the code is your responsibility not mine. It would be much better for you to make some changes, try to pass a reference, and see what happens. In the end you will learn much more.

    If it doesn't work or if you don't understand something we've posted, then post back. Either way, best of luck.

  7. #7
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default Thank You

    @ Fubarable
    Thank you very much for your kind reply sir.i had passed the value s2 to tvpm constructor.Now my problem is solved.Once again thank you very much sir.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Good deal. I'm guessing that s2 is a String? If so, myself, I'd probably pass the Data object itself, but if this works for you, then so be it. Congratulations.

  9. #9
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Default @ Fubarable

    Dear sir,
    i have also done the program by passing reference data d.Its also working.Now the doubt is cleared 100%.Thanks once again.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Now that you have a solution, I will ask if it would be simpler to just have all the combo boxes on one main panel. Something like so:
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class FubarMain2
    {
      private static final String[] SOURCES =
      {
        "Chicago", "Detroit", "New York City", "Miami"
      };
      private static final String[] DESTINATIONS =
      {
        "Los Angeles", "San Francisco", "Seattle", "Denver"
      };
      private static final String SOURCE_TITLE = "Select Source City:";
      private static final String DESTINATION_TITLE = "Select Destination City:";
      private static final String CREATE_TRIP_TITLE = "Press to Create New Trip";
      private static final String CREATE_TRIP_BTN_LABEL = "Create Trip";
    
      private JPanel mainPanel = new JPanel();
      private Data myData = new Data();
      private JTextArea textArea = new JTextArea(10, 40);
      private DefaultComboBoxModel sourceModel = new DefaultComboBoxModel(SOURCES);
      private DefaultComboBoxModel destinationModel = new DefaultComboBoxModel(DESTINATIONS);
    
      public FubarMain2()
      {
        JButton createTripBtn = new JButton(CREATE_TRIP_BTN_LABEL);
        createTripBtn.addActionListener(new CreateTripListener());
        JPanel btnPanel = new JPanel();
        btnPanel.setBorder(BorderFactory.createTitledBorder(CREATE_TRIP_TITLE));
        btnPanel.add(createTripBtn);
    
        int ebGap = 4;
        mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(createSourceDestPanel(sourceModel, SOURCE_TITLE));
        mainPanel.add(createSourceDestPanel(destinationModel, DESTINATION_TITLE));
        mainPanel.add(btnPanel);
        mainPanel.add(new JScrollPane(textArea));
      }
    
      private JPanel createSourceDestPanel(ComboBoxModel model, String title)
      {
        JComboBox combo = new JComboBox(sourceModel);
        combo.setSelectedIndex(-1);
        JPanel sourceDestPanel = new JPanel();
        sourceDestPanel.setBorder(BorderFactory.createTitledBorder(title));
        sourceDestPanel.add(combo);
        return sourceDestPanel;
      }
    
      public JComponent getPanel()
      {
        return mainPanel;
      }
      
      private class CreateTripListener implements ActionListener
      {
        public void actionPerformed(ActionEvent arg0)
        {
          if (sourceModel.getSelectedItem() != null && destinationModel.getSelectedItem() != null)
          {
            String source = sourceModel.getSelectedItem().toString();
            String destination = destinationModel.getSelectedItem().toString();
            myData.setSource(source);
            myData.setDestination(destination);
            
            textArea.append("New Trip:\n");
            textArea.append("Source: " + myData.getSource() + "\n");
            textArea.append("Destination: " + myData.getDestination() + "\n\n");
          }
          else
          {
            JOptionPane.showMessageDialog(mainPanel, 
              "Error: Source or Destination not Selected", 
              "Error", JOptionPane.ERROR_MESSAGE);
          }
        }
      }
    
      private static void createAndShowGUI()
      {
        JFrame frame = new JFrame("FubarMain2 Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new FubarMain2().getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
      }
    }

  11. #11
    shaluchandran is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

Similar Threads

  1. return new variable -problem
    By Hevonen in forum New To Java
    Replies: 7
    Last Post: 12-08-2008, 06:07 AM
  2. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM
  3. [SOLVED] return statement
    By Nakira in forum New To Java
    Replies: 8
    Last Post: 11-12-2008, 11:00 PM
  4. If-Else statement problem
    By MomenT in forum New To Java
    Replies: 9
    Last Post: 10-23-2008, 08:06 AM
  5. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •