0
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

NPE creates my frame


class NPE {  

    private static Values m_Val;
    
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new Draw(m_Val));
        frame.setSize(400,400);
        frame.setVisible(true);
        
    }
}

The Draw class paints my Ellipse (should paint)

class Draw extends JPanel{
    private Values m_Val;
    Draw(Values val){
        m_Val = val;
    }
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        Ellipse2D myOval = new Ellipse2D.Double(m_Val.OVAL_X, m_Val.OVAL_Y, 
                m_Val.OVAL_W, m_Val.OVAL_H);
        Color c = Color.red;
        g2.setColor(c);
        g2.fill(myOval);
    }
    
}

The Values class provides the x,y,widht,height of the ellipse.

class Values{
    int OVAL_X = new Random().nextInt(100);
    int OVAL_Y = new Random().nextInt(100);
    int OVAL_W = 100;
    int OVAL_H = 100;
}

I want to draw an Ellipse by getting its x,y,widht,height from the another class Values. I read many posts and questions about NPE but I can not manage to solve why m_Val is null here. Any ideas?

Exception trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot read field "OVAL_X" because "this.m_Val" is null
    at questions.Draw.paintComponent(NPE.java:31)
Bunsekanse
  • 25
  • 6
  • 2
    `m_val` is `null`. And in any case, if you get an exception, then please include the full exception stacktrace in your question. That said, your question is likely going to get closed to the generic duplicate [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) – Mark Rotteveel Jun 17 '22 at 16:17
  • Its null in `Draw` because its null in `NPE`. Its never set to anything. – tkausl Jun 17 '22 at 16:19
  • Also, don't use prefixed like `m_`, they are generally not used in Java, and in addition, given it is a static field instead of an instance field, it is dubious/confusing to even mark it as a "member". – Mark Rotteveel Jun 17 '22 at 16:20
  • Exception trace added. – Bunsekanse Jun 17 '22 at 16:20
  • Yeah I am very new to this so I will take every knowledge I can :^) – Bunsekanse Jun 17 '22 at 16:21
  • The exception even tells you exactly what is wrong, so I find it confusing why you even asked the question. Did you maybe intend to ask a different question (e.g. "Why is this.m_Val null?"? – Mark Rotteveel Jun 17 '22 at 16:23
  • Yes, your right. – Bunsekanse Jun 17 '22 at 16:24
  • So, Why is this.m_Val null? – Bunsekanse Jun 17 '22 at 16:24
  • 1
    It's null in class `NPE` because you never assign anything to it... you never create an instance of class `Values`. Do you see something like `new Values()` in your code? – printf Jun 17 '22 at 16:30
  • Can you elaborate on that please? – Bunsekanse Jun 17 '22 at 16:32
  • 3
    `private static Values m_Val;` creates a static field with name `m_Val` that is initialized to `null`. If you want it to have a non-null value, you should use `private static Values m_Val = new Values();`. – Mark Rotteveel Jun 17 '22 at 16:33
  • 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) – vanje Jun 18 '22 at 18:01

0 Answers0