-2

I'm kind of new to Java and I'm struggling with calling a method from another class.

Class 1

public void SetColourCheck()
    {
        this.Tick = ColourCheck;
    }

    public JLabel GetColourCheck()
    {
        return this.Tick;
    }

Class 2

public void TickCheck()
    {
        JLabel Tick = GUI.GetColourCheck();

        ColourState();
        Tick.setEnabled(true);
    }

On Class 2, I'm getting the error non-static method GetColourCheck() cannot be referenced from a static context on JLabel Tick.

Edited: Is this what you mean when you say Constructor code?

public class GUI extends javax.swing.JFrame {

    GUI TestGUI;
    public JLabel Tick;

    public GUI(GUI aThis) {
        initComponents();
        TestGUI = aThis;
    }

All this appears in the console.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Scoring.ColourPicker.ColourState(ColourPicker.java:345)
    at Scoring.ColourPicker.BlueSliderStateChanged(ColourPicker.java:326)
    at Scoring.ColourPicker.access$500(ColourPicker.java:13)
    at Scoring.ColourPicker$6.stateChanged(ColourPicker.java:197)
    at java.desktop/javax.swing.JSlider.fireStateChanged(JSlider.java:439)
    at java.desktop/javax.swing.JSlider$ModelListener.stateChanged(JSlider.java:354)
    at java.desktop/javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:371)
    at java.desktop/javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:309)
    at java.desktop/javax.swing.DefaultBoundedRangeModel.setValueIsAdjusting(DefaultBoundedRangeModel.java:238)
    at java.desktop/javax.swing.JSlider.setValueIsAdjusting(JSlider.java:652)
    at java.desktop/javax.swing.plaf.basic.BasicSliderUI$TrackListener.mouseDragged(BasicSliderUI.java:2081)
    at java.desktop/java.awt.Component.processMouseMotionEvent(Component.java:6680)
    at java.desktop/javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3360)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4564)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
Nat
  • 1
  • 2
  • 2
    Does this answer your question? [What is the reason behind "non-static method cannot be referenced from a static context"?](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – QBrute Feb 28 '20 at 09:35

1 Answers1

0

public void SetColourCheck() is an instance method, which can be called using an object alone. Assuming the class name is GUI , the call you did GUI.GetColourCheck() is not using the object of GUI.

So it must be like new GUI().SetColourCheck() or use the object if you have created one.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • Thank you, makes sense now. – Nat Feb 28 '20 at 09:43
  • It works but doesn't, I'm now getting this error in the console, followed by loads more text. `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException` – Nat Feb 28 '20 at 09:53
  • From these lines of code you posted, its hard to say what caused the NPE – Kris Feb 28 '20 at 10:22
  • I have only received that error from `new GUI().SetColourCheck()`. If that helps? I would make this a separate question itself but It will not allow me to. – Nat Feb 28 '20 at 10:25
  • Post the entire stack trace and constructor code of GUI – Kris Feb 28 '20 at 10:26
  • I'll edit the main question – Nat Feb 28 '20 at 10:50