0

I got a small Problem with a JComboBox and the MCV Pattern.

In my View package i got a class gui. This contains the combobox.

    package view;
    public class Gui {
    .........
    public JComboBox<OceanObject> oceanBoxDelete = new ComboBox<OceanObject> 
    ();
    this.down.add(this.oceanBoxDelete);

    this.oceanBoxDelete.setSize(40, 1);
    .......

This also contains a button. If the button gets hit it triggers a swith trough a button listener/action listener. This switch is supposed to trigger a procedure with removes an object from the list. If i put the action listener into the same java file it works but I'm not allowed to do so. If i put it into the button listener file i get an null pointer exeception when entering the combobox with the comment unknown source and .

My button listener looks like:

    package control;
    import view.Gui;

    public class ButtonListener implements ActionListener{

    private Gui gui;

    public ButtonListener() {
    this.ocean = ocean;
    }

   @Override
   public void actionPerformed(ActionEvent ae) {

    switch (ae.getActionCommand()) {
    ....
    case "Delete":
        System.out.println("deleteButton wurde gedrueckt.");

        OceanObject oObject = (OceanObject)   
        gui.oceanBoxDelete.getSelectedItem());
        go.removeOceanOjectFromOcean(oObject);
    .......

I can see that the right switch is triggered since the println is displayed right in the console.

So i think the problem is that the problem is, that the getSelectedItem cant see the combobox and i need to make it known to it, but how?

1 Answers1

0

In the ButtonListener class you aren't initializing gui in the constructor. Unless you're passing it through some other method which isn't listed, it's null when you're accessing it in actionPerformed().

You can pass gui and anything else the ButtonListener needs to the constructor:

public ButtonListener(Gui gui, Ocean ocean, Go go) {
    this.gui = gui;
    this.ocean = ocean;
    this.go = go;
}

Then when you are creating the ButtonListener in the Gui class, you need to provide the actual values:

this.deleteButton.addActionListener(new ButtonListener(
        this,
        this.oceanPanel.getOcean(),
        this.go
));

I'm guessing oceanPanel.getOcean() provides the Ocean instance - if not, change it as needed.

Cinnam
  • 1,892
  • 1
  • 15
  • 23
  • So I need to initialize gui @the 'code' (public ButtonListener()) line? How do i do it right? – Kai Stümer Sep 14 '15 at 00:18
  • @KaiStümer You could pass the reference of either `gui` or `oceanBoxDelete` to the `ButtonListener` constructor and set the private field there. – Cinnam Sep 14 '15 at 00:32
  • Tried that, it didn't change anything. Maybe there is an other solution for this. Can i substitute the 'gui.oceanBoxDelete.getSelectedItem())' somehow to an other method or modify it, so that the combobox gets accessed and the actual selected object read? – Kai Stümer Sep 14 '15 at 01:43
  • @KaiStümer Can you post the whole code or is it too big? Maybe upload it somewhere? – Cinnam Sep 14 '15 at 01:57
  • Here is the Gui [link](http://pastebin.com/y8JRRXAR) and here the ButtonListener [link](http://pastebin.com/muWAheu9) – Kai Stümer Sep 14 '15 at 10:23
  • Thank you for your answer. Now i can take the Object out of the list. I confirmed this by printing a toString of the Object. But still cant use my delete method. I get a nullpointer exeption at go.removeOceanOjectFromOcean(oceanObject);. Here is the class go: [link](http://pastebin.com/Ay74sbrX) the method it calls is in my class ocean and is: public void removeOceanObject(OceanObject oceanObject) { synchronized (oceanObjects) { this.oceanObjects.remove(oceanObject); } } – Kai Stümer Sep 19 '15 at 21:24
  • Thank you for your answer. Now i can take the Object out of the list. I confirmed this by printing a toString of the Object. But still cant use my delete method. I get a nullpointer exeption at go.removeOceanOjectFromOcean(oceanObject);. Here is the class go: link the method it calls is in my class ocean and is: `public void removeOceanObject``(OceanObject oceanObject)`{` `synchronized (oceanObjects) {` `this.oceanObjects.remove(oceanObject); } }` – Kai Stümer Sep 19 '15 at 21:31
  • @KaiStümer I'm sorry, but we can't go over every NPE like this. Please read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it and http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors - that should help you find the problem. Also you can `System.out.println()` various objects in the problematic places to see if they are `null`. – Cinnam Sep 19 '15 at 21:41
  • Be sure i tried to find the error. The obeject is grabbed correct from the box and using a if statement i can determine that it is an instance of OceanObject. This works for all types of that class objects. So the problem must be during invoking the delete method. – Kai Stümer Sep 20 '15 at 12:13
  • @KaiStümer Using the information from the links above and some debugging, you should be able to pinpoint exactly which reference is `null`. Is it `go`? Is the method `removeOceanOjectFromOcean()` even called? If not, then maybe you initialize `go` in the class `Gui` *after* you create the listener. In that case you can make it `public` and access it from the `ButtonListener` class by `gui.go`. – Cinnam Sep 20 '15 at 12:28
  • Ok, i was looking at the wrong point. Yes go is null, so not defined. – Kai Stümer Sep 20 '15 at 12:53
  • Solved the Problem. It now works with the gui.go Method and initializing go after creating the Button listener. Thank you for your help. And sry if some questions sounded stupid, but this is my first java project. – Kai Stümer Sep 20 '15 at 12:57
  • Just out of curriosity do you maybe have an idea how it comes that gui is defined, but go not, they are both declared in the same manner – Kai Stümer Sep 20 '15 at 13:02
  • @KaiStümer According to the `Gui` code you posted, you initialize `go` by calling `setGo()` - but the listener is created in the `Gui` constructor, before `setGo()` could be called. So you are passing `null` to the `ButtonListener` constructor, and some time after that `setGo()` is called and sets the correct `go` value to `gui`. – Cinnam Sep 20 '15 at 13:08
  • Ok that makes sense. So to solve the Problem i would jsut need to change the order when Go go is called. – Kai Stümer Sep 20 '15 at 13:29
  • @KaiStümer `Gui::setGo()` would need to be called before you create the listener, yes. But since the listener is created in `Gui` constructor, there is no way to call `Gui::setGo()` before that. So you just pass `gui` to the listener and it obtains a current `go` reference from `gui.go`. Alternatively, you could initialize `go` in the `Gui` constructor (directly, without relying on `setGo()`), before creating the listener. – Cinnam Sep 20 '15 at 13:57