-1

I'm stuck on a simple problem that I just can't solve. I have two classes (Fruits.java with main and FruitDetails.java).

Fruits.java is a small program with tons of stuff, really. It has a ComboBox and I need to transfer its currently selected option to FruitDetails.

The problem is... my understanding of setters and getters seems to be very flawed. I've researched it online for the last 2 hours and this is the closest I could get to something. I'm really tight on time and I can't help but ask you now...

Inside class Fruits.java

public void selectedFruit() {
    currentFruit = (String) fruitList.getSelectedItem();
}

public String getSelectedFruit() {
    return currentFruit;
}

Inside class FruitDetails.java

public void fruitChoice() {
    Fruits fruitChoice = new Fruits();
    String chosenFruit = fruitChoice.getSelectedFruit();
    System.out.println(chosenFruit);
    // Rest of the code
}

Not only this opens another copy of my program(which I really don't want), system prints out "null" for the result.

I really need to get this working and hopefully it'll help fix my understanding of encapsulation a bit. There's a ton of online resources I've found, but using them seems to be too hard for the thick head of mine.

Thanks in advance for any help.

Governator
  • 25
  • 1
  • 5
  • Can you post the stack trace? – Prabhav Mar 30 '18 at 08:19
  • 1
    In FruitDetails you are creating new Fruits() and getting chosenFruit from that.. But you haven't assigned any values yes(never called any setters). So null pointer is obvious.. – DhaRmvEEr siNgh Mar 30 '18 at 08:22
  • You should try to strip your code down to a [mcve]. Or at least show more of `Fruits` class. If (just wild guess) a single instance of it contains the UI, you should make it a singleton (i.e.: have a static method able to return the unique instance) and use it in `FruitDetails.fruitChoice`: `Fruits fruitChoice = Fruits.getInstance();` – Serge Ballesta Mar 30 '18 at 08:32
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Apr 09 '18 at 23:59
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Apr 09 '18 at 23:59
  • [When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. Click this comment to find out how to provide what we need to help you.](https://stackoverflow.com/help/mcve) –  Apr 10 '18 at 00:00

3 Answers3

2
public void fruitChoice() {
    Fruits fruitChoice = new Fruits();
    String chosenFruit = fruitChoice.getSelectedFruit();
    System.out.println(chosenFruit);
    // Rest of the code
}

In second line you are creating new object that's why you are getting null when you try to get the value of currentFruit.

Abhishek Singh
  • 236
  • 1
  • 8
0

it looks like you method selectedFruit() sets currentFruit but your not actually calling selectedFruit()?

Unless your missing some code above that calls selectedFruit() elsewhere?

Try calling selectedFruit() after instantiating your Fruit object.

GordonW
  • 1,120
  • 2
  • 16
  • 36
0

This is because you have not actually linked your currentFruit to your combo box. You need to do two things - call selectedFruit when you first populate the combo box, then attach a listener that calls selectedFruit everytime the combo box selection changes.

If you are using JComboBox, insert this code after you have created the JComboBox.

combo.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        selectedFruit();
    }
})
selectedFruit();
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • Thank you for trying, but an appropriate action listener exists. If I sout currentFruit in selectedFruit, I do get the proper result. Apologies, but I can't post the full code - this probably caused the confusion. – Governator Mar 30 '18 at 09:14