-1

everyone for some reason on execute I am getting null pointer exception, but I cant find reason why, I think its something with setOnAction method but not sure how I can fix it.

In View Part I got

public void addButtonListener(EventHandler<ActionEvent> x)
    {
        login.setOnAction(x);
    }

and in Controller

public Controller(View theView,Model theModel)
    {
        this.theView = theView;
        this.theModel = theModel;

        this.theView.addButtonListener(new SolListener());
    }

inner class in controller:

class SolListener implements EventHandler<ActionEvent>
    {
            @Override
            public void handle(ActionEvent event) 
            {
                String u,p;
                try
                {
                    u = theView.getUsername();
                    p = theView.getPassword();

                    theModel.zhoda(u, p);   
                    theView.Solution(theModel.getSolution());

                }catch (NullPointerException f)
                {

                }

            }

    }

and for some reason I still getting NullPointerException on

public void addButtonListener(EventHandler<ActionEvent> x)

Any ideas ?

LouGi
  • 34
  • 4

1 Answers1

0

It might be that your login field is not initialized. I don't see it initialized anywhere in the code you provided.

cyfrostan
  • 16
  • 1
  • It shouldn't mater, problem is that the method setOnAction(x) is getting nullpointerex. But as you can see I am creating obj action event and its pass to method. But for some reason I am still getting exception – LouGi Jun 04 '16 at 21:36
  • 1
    this should be a comment instead of an answer. – QoP Jun 04 '16 at 21:39
  • Can you include your stack trace in your post. Or at least the line where it says where you're getting the NullPointerException at. – cyfrostan Jun 04 '16 at 21:44
  • @QoP I suppose you're right. Sorry – cyfrostan Jun 04 '16 at 21:46
  • Exception in thread "main" java.lang.NullPointerException at application.View.addButtonListener(View.java:107) at application.Controller.(Controller.java:16) at application.Demo.main(Demo.java:9) – LouGi Jun 04 '16 at 21:50
  • I tryed to recreate it with lamba expressions and anonymous class but it didnt help. – LouGi Jun 04 '16 at 21:56
  • You only have one line of code in the `addButtonListener` method: `login.setOnAction(x);`. So that line is throwing the exception. The only way that line throws a null pointer exception is if `login` is null. And @LouGi how can it possibly not matter if `login` is null, since you're invoking a method on it? – James_D Jun 04 '16 at 22:13
  • I found it I have switch creating of obj in Main method. OMG :D – LouGi Jun 04 '16 at 22:25