0

I'm a Java beginner. I want to call a Method in a running Java Thread Object. It always throws this exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "Graphic_handler.next()" because "this.this$0.grap" is null

public class Graphic_handler extends Thread {

    int off = 0;


    int columns = 7;
    int rows = 7;
    public Graphic_handler(Instagram_bot bot) {
    }

         

    public void run() {
   
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                //TODO: handle exception
            }
            set_time();
        }

    }


    private void set_time() {
        Date date = Calendar.getInstance().getTime();
        DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy    hh:mm:ss");
        String strDate = dateFormat.format(date);
        bot.date.setText(strDate);

    }

    public void change_center(int offset) {
    }

    public synchronized void next() {
        off++;
        notify();
    }

    public synchronized void last() {
        off--;
        notify();
    }
}

(Code is simplified)

Here is the part of the Code where I call the Method:

ActionListener right_ear = new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        grap.next();
    };
};
ActionListener left_ear = new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        grap.last();
    };
};


public static void main(String[] args) {

    Graphic_handler grap = new Graphic_handler(bot);
    grap.start();
    
}

Im trying to call the Methods next() and last() here.

T.Stark
  • 3
  • 3
  • 2
    You probably oversimplified your code. What is `.grap` in the exception message? Also please provide the code you are calling `next()` from. – akarnokd Jun 21 '22 at 15:51
  • I think you removed the code that might tell us what is going on. Try adding some of the code back in so we can see what might be happening. Also can we see the code that calls this instance of `Graphic_handler`? – markspace Jun 21 '22 at 15:53
  • grab is the name of the Thread Object in my Main Class. My Code for calling it is in a ActionListener: – T.Stark Jun 21 '22 at 15:54
  • ActionListener right_ear = new ActionListener() { public void actionPerformed(ActionEvent e) { grap.next(); }; }; ActionListener left_ear = new ActionListener() { public void actionPerformed(ActionEvent e) { grap.last(); }; }; – T.Stark Jun 21 '22 at 15:54
  • 4
    Please don't post code in comments but update your question. – akarnokd Jun 21 '22 at 15:55
  • OK then, `grab` is being called before you got a chance to assign it a value. In other words you haven't called `grab = new Graphic_handler();` yet. Show us your initialization code for that object. (Yes, PLEASE edit your question and add the code there, not in a comment.) – markspace Jun 21 '22 at 15:55
  • 1
    And where do you initialize `grab`? (While you *should* trim your code I think at this point it would be easier to just dump the whole class on us.) – markspace Jun 21 '22 at 15:58
  • 1
    `Graphic_handler grap = new Graphic_handler(bot);` inside `main` makes it a local variable. the (probably existing, didn't see it) field is not initialized there. – Johannes Kuhn Jun 21 '22 at 16:00
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Johannes Kuhn Jun 21 '22 at 16:00
  • I initialised grap in my main method in the main Method of my Main Class – T.Stark Jun 21 '22 at 16:00
  • By the way, the terrible `Date` and `Calendar` classes were years ago supplanted by the modern *java.time* classes defined in JSR 310. – Basil Bourque Jun 21 '22 at 17:00
  • Oh ok. thx. But I just couldnt implement them. im very happy that anything in my code is working. Im to scared to touch anything. But really thanks for your help. (: – T.Stark Jun 22 '22 at 18:08

1 Answers1

2

What's going on is you have a local variable that is hiding the instance variable.

class SomeClass {

   Graphic_handler grap;

   public static void main(String[] args) {
      Graphic_handler grap = new Graphic_handler(bot);
                      ^^^^ this *hides* the other grap
      grap.start();
   }
    
}

To fix this just remove the declaration in front of grap in the method.

class SomeClass {

   Graphic_handler grap;

   public static void main(String[] args) {
      grap = new Graphic_handler(bot);
      grap.start();
   }
    
}
markspace
  • 10,621
  • 3
  • 25
  • 39